21

I want auto generation of meta description for my products by create an new observer on the event catalog_product_save_before with this code :

public function autoMetaDescription($observer) {
     $product = $observer->getEvent()->getProduct();
     $metaDescription = "Buy ". $product->getName()." for Rs ".number_format($product->getFinalPrice(), 2)." at sitename.com | New Book | Authorised seller for ". $product->getAttributeText('publisher');
    $product->setMetaDescription($metaDescription);
} 

In which folder/files I have to write the code?

  • In this method, make sure there is no issue for csv products upload. – zus Jan 31 '18 at 10:50
49

You need to create a new module.
Let's call it Easylife_Meta.
app/etc/modules/Easylife_Meta.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </Easylife_Meta>
    </modules>
</config>

app/code/local/Easylife/Meta/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <version>0.0.1</version>
        </Easylife_Meta>
    </modules>
    <global>
        <models>
            <easylife_meta>
                <class>Easylife_Meta_Model</class>
            </easylife_meta>
        </models>
    </global>
    <adminhtml>
        <events>
            <catalog_product_save_before><!-- observe the event -->
                <observers>
                    <easylife_meta>
                        <class>easylife_meta/observer</class>
                        <method>autoMetaDescription</method>
                    </easylife_meta>
                </observers>
            </catalog_product_save_before>
        </events>
    </adminhtml>
</config>

app/code/local/Easylife/Meta/Model/Observer.php - the observe class

<?php 
class Easylife_Meta_Model_Observer {
    public function autoMetaDescription($observer) {
        $product = $observer->getEvent()->getProduct();
        $metaDescription = "Buy ". $product->getName()." for Rs ".number_format($product->getFinalPrice(), 2)." at sitename.com | New Book | Authorised seller for ". $product->getAttributeText('publisher');
        $product->setMetaDescription($metaDescription);
    } 
}

Clear the cache when you are done.

  • 2
    Shouldn't it be <class>easylife_meta_Model_Observer</class> instead of <class>easylife_meta/observer</class> in config.xml ? – kashish Oct 31 '17 at 5:44
  • 2
    @kashish NO it shouldn't. – Marius Oct 31 '17 at 7:25
  • @Marius I'm using Magento 1.9, changed class to the full class name helped me – TheKitMurkit Sep 12 '18 at 14:15
  • @TheKitMurkit It should work both ways. Using the alias allows proper execution in case your observer is overwritten. – Marius Sep 12 '18 at 14:54

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.