Magento observer goes in endless loop

2019-08-03 04:53发布

问题:

Thanks in advance

I have created one observer i need to set the attribute values on fly using the observer please check the following config and the observer files when i click on the save button the observer goes into the endless . i just want to set the attribute value using this observer

<catalog_product_save_after>
                <observers>
                    <zaptech_save_product_data>
                        <type>singleton</type>
                        <class>upload/observer_product</class>
                        <method>saveTabData</method>
                    </zaptech_save_product_data>
                </observers>
            </catalog_product_save_after>

and my observer handler code is here please check

 public function saveTabData(Varien_Event_Observer $observer)
    { 

        $productModel=Mage::registry('current_product')                
                      ->setTestid('1')
                      ->setTestname('Jitendra')
                      ->save();

    }

the problem with this code is that the observe goes in endless loop

please help

Thanks again,

Jitendra Dhobi.

Here is the answe of my own question i replaced the event name from catalog_product_save_after to catalog_product_save_before..

<catalog_product_save_before>
                <observers>
                    <zaptech_save_product_data>
                        <type>singleton</type>
                        <class>upload/observer_product</class>
                        <method>saveTabData</method>
                    </zaptech_save_product_data>
                </observers>
</catalog_product_save_before>

and also remove the save() method from the observer file same below

public function saveTabData(Varien_Event_Observer $observer)
        { 

            $productModel=Mage::registry('current_product');                
            $productModel->setTestid('1');
            $productModel->setTestname('Jitendra');


        }

cheers!!!...

回答1:

You can use a registry to determinate your custom product save and prevent loop.

public function saveTabData(Varien_Event_Observer $observer)
{
    if(Mage::registry('customUpdate')) return;
    Mage::register('customUpdate', true);

    $productModel=Mage::registry('current_product')
        ->setTestid('1')
        ->setTestname('Jitendra')
        ->save();

}


标签: magento