8

I registered an observer for the sales_order_save_commit_after event to be notified when a new order comes in. This works fine but the event is triggered two times in a row with the same order.

My config.xml

        <sales_order_save_commit_after>
            <observers>
                <unique_sales_order_save_commit_after>
                    <type>singleton</type>
                    <class>mymodule/observer</class>
                    <method>export</method>
                </unique_sales_order_save_commit_after>
            </observers>
        </sales_order_save_commit_after>

How can I check if it's the first time or which other event would be better?

Update

I also tried the sales_order_invoice_pay event (found on SO) but that is not triggered for me.

9

Try using sales_order_save_after instead, it's triggered right after an order is saved in the database and returns the full order object

Why this event is triggered twice, I'm not sure. It's an an event triggered from Core/Model/Abstract.php method afterCommitCallback, seems that Magento is saving / committing 2 different data sets on the the Order model. Perhaps once the order itself and once the status history.

Is there any difference between the data being parsed on the 2 events that might give a clue to where it's called from?

  • 1
    Thank you sales_order_save_after works but I will use sales_order_place_after as it is not triggered again by back end changes. – PiTheNumber Sep 9 '13 at 13:17
7

If anyone is still having problem with this, I found out how magento deals with this.

In the called method you can take the order and set a flag on it.

Ex:

public function export(Varien_Event_Observer $observer) {
    $order = $observer->getEvent()->getOrder();
    if($order->getExportProcessed()){ //check if flag is already set.
        return;
    }

    // your part of code

    //"setExportProcessed" can be called anything you want as it's getting set magically by magento on our $order object.
    $order->setExportProcessed(true); 
}

In app/code/core/Mage/Cataloginventory/Model/Observer.php

function subtractQuoteInventory(Varien_Event_Observer $observer)

is an example of how magento deals with this.

5

I changed the event to sales_order_place_after. This works fine.

0
public function sendEmail(Varien_Event_Observer $observer) {
    if(!Mage::registry('varien_event')){

        //your code here...

        Mage::register('varien_event',true);

    }
}

The Bove code is working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.