3

I am trying to have all product url keys automatically generated this way:

site.com/manufacturer-productname-sku.html

I managed to add the SKU adding to the file /app/code/local/Mage/Catalog/Model/Product/Attribute/Backend/Urlkey.php:

public function beforeSave($object)
    {
        $attributeName = $this->getAttribute()->getName();

        $urlKey = $object->getData($attributeName);
        if ($urlKey === false) {
            return $this;
        }
        if ($urlKey == '') {
            $urlKey = $object->getName().'-'.$object->getSku();
        }


      $object->setData($attributeName, $object->formatUrlKey($urlKey));

        return $this;
    }

Adding getManufacturer() or getData('manufacturer') are not working, so the $_product->getAttributeText

Any suggestion?

It would also be better if the SKU kept the capital letters, but that's an option.

4

You can do this by using catalog_product_save_befoe event .Here details below

code MageStack_24869.xml of app\etc\modules\

<?xml version="1.0" ?>
<config>
    <modules>
    <MageStack_24869>
        <codePool>local</codePool>
        <active>true</active>
    </MageStack_24869>
    </modules>
</config>

And config.xml path app\code\local\MageStack\24869\etc\ and code is

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

Also Observer.php code path (app\code\local\MageStack\24869\Model)

<?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('sku'))):
            $Url=$Url.$Product->getData('sku').'-';
            endif;
            if(!is_null($Product->getData('name'))):
            $Url=$Url.$Product->getData('name');
            endif;
            //Mage::log('My log entry'.$Url, null, 'mylogfile.log');
            $Product->setData('url_key',$Url);      

        }
    }
}

Also you can do using below code

https://stackoverflow.com/questions/23987445/how-to-customize-product-url

  • Thanks, it worked! I only had to edit country_of_manufacture with manufacturer – Stefano Piva Jun 25 '14 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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