26

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?

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

1 Answer 1

53

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.

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

Your Answer

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

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