1

I am trying to hook catalog_product_save_after event. Here is config.xml

<events>
   <catalog_product_save_after>
     <observers>
       <observer_name_here>
         <class>My_Class_Model_Observer</class>
         <method>methodToCall</method>
         <type>singleton</type>
       </observer_name_here>
     </observers>
   </catalog_product_save_after>
</events>

And below is the code that runs in methodToCall():

$product = Mage::getModel('catalog/product')->load(1);
$product->setName('TESTING 123');
$product->save();


Issue Is:
When the event catalog_product_save_after is fired. The code written in methodToCall() fire the catalog_product_save_after again. And according Magento EDA system methodToCall() called again which fires the catalog_product_save_after once again. So the system stuck in series of firing and listening the same event.

My Questions:

  1. How to avoid this situation ?
  2. Is there any way to disable to Magento event dispatch functionality for temporary purposes (without re-writing dispatchEvent method of Mage_Core_Model_App if possible).
  3. How to prevent infinite looping, if the observer fires the same event that instantiated the observer. Like in case above.
ROBIN
  • 502
  • 6
  • 17
  • Why don't you use catalog_product_save_before event? Can you explain more what are you trying to achive in your method, maybe we can offer an alternative solution rather than trying to disabling event dispatches. – muhammedv Apr 18 '16 at 12:37
  • @muhammedv If I use `catalog_product_save_before` the same thing gonna happen. The issue is EDA system, What if the observer fires the same event. How to prevent system to get into the looping. – ROBIN Apr 18 '16 at 12:47
  • If you use catalog_product_save_before, you won't call $product->save(); It will be saved after. Just get product with $product = $observer->getEvent()->getProduct(); in the event observer method and call setName() and don't call save(). It will be saved. Test it. – muhammedv Apr 18 '16 at 12:50
  • That's cool. But i have to use the new values and using those values I have to update another product. That's why I can't use the **catalog_product_save_before** event. – ROBIN Apr 18 '16 at 12:52
  • You can get new values in catalog_product_save_before – muhammedv Apr 18 '16 at 12:53
  • Also you can get old values by calling $product->getOrigData(); – muhammedv Apr 18 '16 at 12:54
  • Also you can check if there is a data change in product model by calling $product->hasDataChanges() – muhammedv Apr 18 '16 at 12:54
  • Agree! What I seeking for solution for this looping situation. Can we built something in Magento that can disable all the dispatching functionality temporary ? I am trying with re-writing the dispatchEvent function. – ROBIN Apr 18 '16 at 12:57
  • If you dont call save() it won't loop – muhammedv Apr 18 '16 at 12:58
  • Yeah that I can see :) But I am not avoiding this situation. I am looking for a solid solution for this. – ROBIN Apr 18 '16 at 13:01
  • ROBIN, calling save function in catalog product save observer is wrong. You need to find another way to achive what you want, not a way to disable event dispatching functionality temporarily. – muhammedv Apr 18 '16 at 13:02

2 Answers2

0

If you have to use the new values and using those values you have to update another product you can observe catalog_product_save_before event. Your config looks OK, here you are an example methodToCall function:

public function methodToCall(Varien_Event_Observer $observer)
{
    $event    = $observer->getEvent();
    $product  = $event->getProduct();

    if($product->hasDataChanges()) { //somethings changed

        $newName = $product->getName();
        $oldName = $product->getOrigData('name');

        //do stuff here
        //don't call $product->save() It WILL be saved. If you call save() you will create a loop
    }
}
muhammedv
  • 879
  • 8
  • 18
0

I believe fundamentally, this is probably the wrong approach for your use case, I think you should probably be looking at save_before. However, I've seen people use the Mage::registry for this in the past.

if(!Mage::Registry("somekey")) {
    Mage::Register("somekey", true);
    //LOGIC
}

Now, in this instance, "somekey" should be something specific, and probably specific to this product too so that if you're saving multiple products, it doesn't affect them.

Douglas Radburn
  • 808
  • 4
  • 14