Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento event observer (magento1.7)

i am trying to implement an simple observer in my module (app/code/local/Foo/Bar). My objective is to set the product names to '[product name] is cool' when a product is loaded in the frontend.

These are my files: app/code/local/Foo/Bar/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <global>
        <models>
            <foo_bar>       
                    <class>Foo_Bar_Model</class>
            </foo_bar>  
        </models>
    </global>
    <frontend>
        <events>
            <catalog_product_load_after>
                <observers>
                    <foo_bar>
                        <type>model</type>
                        <class>foo_bar/observer</class>
                        <method>catalogProductLoadAfter</method>
                    </foo_bar>
                </observers>
            </catalog_product_load_after>
        </events>
    </frontend>
</config>

app/code/local/Foo/Bar/Model/Observer.php

<?php 
class Foo_Bar_Model_Observer{

public function catalogProductLoadAfter(Varien_Event_Observer $observer)

{
    $product = $observer ->getProduct();

    $product ->setName($product.getName().' '.'is cool');
} 
}
?>

And i have also configured the module in app/etc/modules/Foo_Bar.xml

<?xml version="1.0" encoding="UTF-8"?>
 <config>
   <modules>
     <Foo_Bar>
        <active>true</active>
        <codePool>local</codePool>
     </Foo_Bar>
  </modules>
</config>

But it doenst work, does anyone have any suggestion?

like image 761
Armand Ndizigiye Avatar asked Feb 03 '26 01:02

Armand Ndizigiye


1 Answers

You only need to module name for the model:

<foo_bar>
     <type>model</type>
     <class>bar/observer</class>
     <method>catalogProductLoadAfter</method>
</foo_bar>

you don't need to specify Foo_Bar

also have you an error in your PHP

$product ->setName($product.getName().' '.'is cool');

should be

$product->setName($product->getName().' '.'is cool');
like image 85
Andrew Avatar answered Feb 04 '26 15:02

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!