Tutorial :Why does my Magento observer get stuck in and endless loop?



Question:

My observer gets stuck in and endless loop. Why does it happen and how can I fix it?

config.xml:

<?xml version="1.0"?>  <config>      <global>          <models>              <shipmentsave>                  <class>Company_Shipmentsave_Model</class>              </shipmentsave>          </models>      </global>      <adminhtml>          <events>              <sales_order_shipment_track_save_after>                  <observers>                      <shipmentsave>                          <type>model</type>                          <class>shipmentsave/observer</class>                          <method>salesOrderShipmentTrackSaveAfter</method>                      </shipmentsave>                  </observers>              </sales_order_shipment_track_save_after>              <sales_order_shipment_save_after>                  <observers>                      <shipmentsave>                          <type>model</type>                          <class>shipmentsave/observer</class>                          <method>salesOrderShipmentSaveAfter</method>                      </shipmentsave>                  </observers>              </sales_order_shipment_save_after>          </events>      </adminhtml>      <frontend>          <events>              <sales_order_shipment_save_after>                  <observers>                      <shipmentsave>                          <type>singleton</type>                          <class>shipmentsave/observer</class>                          <method>salesOrderShipmentSaveAfter</method>                      </shipmentsave>                  </observers>              </sales_order_shipment_save_after>          </events>      </frontend>  </config>  

Observer.php:

class Company_Shipmentsave_Model_Observer  {      public function salesOrderShipmentSaveAfter(Varien_Event_Observer $observer)      {          error_log("My observer called ....",0);          $shipment = $observer->getEvent()->getShipment();          $order = $shipment->getOrder();          $track = Mage::getModel('sales/order_shipment_track')              ->setNumber('1231354564')              ->setCarrierCode('localdelivery')              ->setTitle('Aramex');          $shipment->addTrack($track);          $shipment->save();          return;      }  }  


Solution:1

Your observer waits for a shipment to get saved, then saves a shipment, which causes it to receive another event (yadda yadda ad nauseum). You will need a way to either escape the loop or not have to save a shipment.

Can you move your event onto sales_order_shipment_save_before and then allow the normal shipment saving to take effect, or do you need the shipment to already have been saved to do your part of the logic?

If so, change the following line on your observer so that Magento uses it as a singleton:

<type>singleton</type> // changed from model  

Then, create a variable in your class to track whether you've recursed yet. If so, then just return.

Alternatively, you could check to see if there are any tracking numbers already on the shipment and only save if there are not (and you add one). That will kill the recursion as well.

Let me know if one of those works for you.

Thanks, Joe


Solution:2

I used

sales_order_shipment_save_after  

and its working for me

Thanks :)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »