Search code examples
magentoobservers

Can't able to set the product attribute programmatically in magento through observer


I want to update the products description attribute after the product save.so for this i am using the observer called catalog_product_save_after and depending on some condition i create the description for the product and i will save the description of the products by following code

product->setDescription();
product->save();

the problem is when i am calling the product->save(); the site is loading and loading later i found that product->save(); this function is again calling the catalog_product_save_after. that's why it is going into the infinite loop.

Please help me to set the description for the product.


Solution

  • Option 1:
    You can use catalog_product_save_before and just use $product->setDescription('something') (without the save).
    Option 2
    Make your observer run only once.

    function doSomething($observer) {
        //some code here
        $id = $product->getId();
        if (!Mage::registry('observer_already_executed_'.$id)) {
            //do your magic here
            Mage::register('observer_already_executed_'.$id, 1);
        }
    }