I'm trying to recreate URL keys for all my products to include the product name and then the SKU. I found this helpful post that claims to resolve the issue, however I just can't seem to get it to work.
The main difference is that I do not want to include the manufacturer name (most of my products have this in the product name) and I've changed the order in which the link is generated (www.mysite.com/product_name-SKU):
<?php
class MageStack_24869_Model_Observer{
public function updateurl($observer){
//Mage::log('My log entry', null, 'mylogfile.log');
if($observer->getEvent()->getProduct()){
$Product=$observer->getEvent()->getProduct();
$Url='';
// if(!is_null($Product->getData('country_of_manufacture'))):
// $Url=$Url.$Product->getAttributeText('country_of_manufacture').'-';
// endif;
if(!is_null($Product->getData('name'))):
$Url=$Url.$Product->getData('name').'-';
endif;
if(!is_null($Product->getData('sku'))):
$Url=$Url.$Product->getData('sku');
endif;
Mage::log('My log entry'.$Url, null, 'mylogfile.log');
$Product->setData('url_key',$Url);
}
}
}
config.xml
<?xml version="1.0"?>
<config>
<modules>
<MageStack_24869>
<version>0.1.0</version>
</MageStack_24869>
</modules>
<global>
<models>
<mageStack24869>
<class>MageStack_24869_Model</class>
</mageStack24869>
</models>
</global>
<global>
<events>
<catalog_product_save_before>
<observers>
<mageStack24869>
<type>singleton</type>
<class>mageStack24869/observer</class>
<method>updateurl</method>
</mageStack24869>
</observers>
</catalog_product_save_before>
</events>
</global>
</config>
The only thing I can think of is that there is a reference to the url being "null" - so I don't know if because I already have url keys that they are not updated.
Is there something wrong with the above logic and/or solution to rewrite all url keys?
Thanks!
Mage::log('My log entry'.$Url, null, 'mylogfile.log');
? Is your config the same as the example you link to?