Created
April 27, 2014 18:03
-
-
Save alexsancho/11351664 to your computer and use it in GitHub Desktop.
Custom magento module that removes stop words from url before save
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0"?> | |
| <config> | |
| <modules> | |
| <Module_Catalog> | |
| <version>1.0</version> | |
| </Module_Catalog> | |
| </modules> | |
| <global> | |
| <models> | |
| <catalog> | |
| <rewrite> | |
| <product_attribute_backend_urlkey>Module_Catalog_Model_Product_Attribute_Backend_Urlkey</product_attribute_backend_urlkey> | |
| </rewrite> | |
| </catalog> | |
| </models> | |
| </global> | |
| </config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Product url key attribute backend | |
| * | |
| * @category Mage | |
| * @package Mage_Catalog | |
| * @author Magento Core Team <core@magentocommerce.com> | |
| */ | |
| class Module_Catalog_Model_Product_Attribute_Backend_Urlkey extends Mage_Catalog_Model_Product_Attribute_Backend_Urlkey | |
| { | |
| // false words list separated for commas | |
| const STOP_WORDS = 'a,alguna,algunas,alguno,algunos,algún,ambos,ante,antes,aquel,aquellas,aquellos,aquí,arriba,atrás,bajo,bastante,bien,bueno,cabe,cada,cierta,ciertas,cierto,ciertos,como,con,conseguimos,conseguir,consigo,consigue,consiguen,consigues,contra,cual,cuando,de,dentro,desde,donde,dos,durante,el,ellas,ellos,empleamos,emplean,emplear,empleas,empleo,empleáis,en,encima,entonces,entre,era,éramos,eran,eras,eres,es,esta,estaba,estado,estamos,estoy,estáis,están,fin,fue,fueron,fui,fuimos,ha,hace,hacemos,hacen,hacer,haces,hacia,hacéis,hago,hasta,incluso,intenta,intentamos,intentan,intentar,intentas,intento,intentáis,ir,la,largo,las,lo,los,mediante,mientras,modo,muchos,muy,mío,nos,nosotros,otro,para,pero,podemos,poder,podría,podríais,podríamos,podrían,podrías,podéis,por,por qué,porque,primero,puede,pueden,puedo,quien,sabe,sabemos,saben,saber,sabes,sabéis,según,ser,si,siendo,sin,sobre,sois,solamente,solo,somos,soy,su,sus,también,tenemos,tener,tengo,tenéis,tiempo,tiene,tienen,todo,trabaja,trabajamos,trabajan,trabajar,trabajas,trabajo,trabajáis,tras,tuyo,ultimo,un,una,unas,uno,unos,usa,usamos,usan,usar,usas,uso,usáis,va,vais,valor,vamos,van,vaya,verdad,verdadera,verdadero,vosotras,vosotros,voy,y,yo'; | |
| /** | |
| * Format url key attribute before save, also use product name as url key if it empty | |
| * | |
| * @param Varien_Object $object | |
| * @return Mage_Catalog_Model_Category_Attribute_Backend_Urlkey | |
| */ | |
| public function beforeSave( $object ) { | |
| $attributeName = $this->getAttribute()->getName(); | |
| $urlKey = $object->getData( $attributeName ); | |
| if ( $urlKey === false ) | |
| return $this; | |
| if ( $urlKey == '' ) { | |
| $urlKey = $object->getName(); | |
| } | |
| $slug = $this->removeFalseWords( $object->formatUrlKey( $urlKey ) ); | |
| $object->setData( $attributeName, $slug ); | |
| return $this; | |
| } | |
| private function removeFalseWords( $slug ) { | |
| $slug = explode('-', $slug); | |
| $keys = explode( ',', self::STOP_WORDS ); | |
| foreach ( $slug as $k => $word ) { | |
| foreach ( $keys as $l => $wordfalse ) { | |
| if ( $word == $wordfalse ) { | |
| unset( $slug[$k] ); | |
| } | |
| } | |
| } | |
| return implode( '-', $slug ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment