In magento sometimes we are required to change the attribute values of the products.
To do so we can work upon the event catalog_product_load_after.
Lets see how we can do it:
1. Create a module with namespace as Custom and module as ProductInfo.
2. Then Create a file config.xml in etc folder in our module.
3. Afterwards create a file Observer.php in Model folder.
Now, in our config.xml file at the path Custom/ProductInfo/etc/config.xml lest change the product name for the same write the below code in it :
<?xml version="1.0"?>
<config>
<modules>
<Custom_ProductInfo>
<version>0.1.0</version>
</Custom_ProductInfo>
</modules>
<global>
<events>
<catalog_product_load_after>
<observers>
<Custom_ProductInfo_Model_Observer>
<class>custom_productInfo/observer</class>
<method>ProductInfo</method>
</Custom_ProductInfo_Model_Observer>
</observers>
</Custom_ProductInfo_load_after>
</events>
</global>
</config>
In the above code we have defined our module with the version 0.1.0 and
then in global we have called our Observer at the event catalog_product_load_after
Now in our Observer at the path Custom/ProductInfo/Model/Observer.php write the below code:
<?php
class Custom_ProductInfo_Model_Observer {
public function ProductInfo($observer) {
$product = $observer->getProduct();
$product = $observer->getData('product');
$product->setName($product->getName().' New Name !');
}
}
In the above code we are loading the product first and then with the setName() magento method we have set up the new name as " New Name ! ". That's it we are done.
In the same way we can change any attribute value of products.
0 Comment(s)