Magento observer goes in endless loop

醉酒当歌 提交于 2020-02-25 05:52:11

问题


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();

}


来源:https://stackoverflow.com/questions/7204374/magento-observer-goes-in-endless-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!