0

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!

7
  • What is the output of Mage::log('My log entry'.$Url, null, 'mylogfile.log');? Is your config the same as the example you link to? Oct 26, 2014 at 12:41
  • Good question, actually the log file is never generated. (Yes I have uncommented the top section as well.)
    – SR_Magento
    Oct 26, 2014 at 12:44
  • Is the observer getting executed? Oct 26, 2014 at 12:45
  • can you add your config.xml? What happen when you save a product from admin? Oct 26, 2014 at 12:48
  • updated with config.xml - when I open a product in the admin panel and hit "save" it then updates the key. So now I just need it to update when I re-index all unless this isn't the correct way to do it?
    – SR_Magento
    Oct 26, 2014 at 12:57

1 Answer 1

2

With the code below you will manually have to re-save all existing products for the url to change (catalog_product_save_before only executed when that product is save/created).

You can either resave all existing product manually to update there existing url or create a one time script to update all existing products and then this script will take care of future add or update products.

<?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>
    <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> 

Observer.php

class MageStack_24869_Model_Observer{
  public function updateurl($observer){
    //Mage::log('My log entry', null, 'mylogfile.log');
    $Product = $observer->getEvent()->getProduct()
    if($Product){
        $Url='';
        if(!is_null($Product->getData('name'))){
           $Url =. $Product->getData('name').'-';
        }

        if(!is_null($Product->getData('sku'))){
           $Url =. $Product->getData('sku');
        }

        Mage::log('My log entry'.$Url, null, 'mylogfile.log');
        $Product->setData('url_key', rtrim($Url, '-');      
    }
  }
}
2
  • I'm going to give this a try a little later and report back. For some reason when I update my URL keys, they aren't reflected. ie if the key is changed, the old url is still active, doesn't redirect and the new url doens't work.....
    – SR_Magento
    Oct 26, 2014 at 13:21
  • Got it working, turns out that it had to do with store views. Thanks for the help!
    – SR_Magento
    Oct 30, 2014 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.