开发者

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

开发者 https://www.devze.com 2023-01-06 06:51 出处:网络
My observer gets stuck in and endless loop. Why does it happen and how can I fix it? config.xml: <?xml version=\"1.0\"?>

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;
    }
}


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


I used

sales_order_shipment_save_after

and its working for me

Thanks :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号