Trigger observer on Magento order status change events

Trigger observer on Magento order status change events

August 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Trigger observer on Magento order status change events

Triggering Magento Order Status Change Events for Extension

As a senior developer specializing in eCommerce platforms like Magento, I often encounter scenarios where clients need to automate certain actions based on order status changes. One common requirement is to trigger invoicing when an order reaches the ‘processing’ status.

Identifying the Relevant Event Observer

In Magento, events are triggered at various stages of an order’s lifecycle, including when it is saved (after the order has been updated). The sales_order_save_after event indicates that an order has just been modified and can be used to implement business logic based on its status.

The relevant XML configuration for this event observer looks like this:

<events>
    <sales_order_save_after>
        <observers>
            <auto_invoice_order>
                <type>singleton</type>
                <class>Atwix_Orderhook_Model_Observer</class>
                <method>implementOrderStatus</method>
            </auto_invoice_order>
        </observers>
    </sales_order_save_after>
</events>

This tells Magento to call the implementOrderStatus method of the observer model (in this case, Atwix_Orderhook_Model) whenever an order is saved.

Implementing the Observer Logic

The observer method needs to check if the order’s status has changed to ‘processing’ and then proceed with creating the invoice. Here’s a basic implementation:

class Atwix_Orderhook_Model_Observer 
{
    public function implementOrderStatus($event)
    {
        $order = $event->getOrder();

        if ($order->getState() == Mage_Sales_Model_Order::STATE_PROCESSING) {
            if ($order->canInvoice())
                $this->_processOrderStatus($order);
        }
        return $this;
    }

    private function _processOrderStatus($order)
    {
        $invoice = $order->prepareInvoice();

        $invoice->register();
        Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder())
            ->save();

        $invoice->sendEmail(false, '');
        return true;
    }
}

The implementOrderStatus method first retrieves the order object and checks its state. If it’s in the ‘processing’ state (represented by Mage_Sales_Model_Order::STATE_PROCESSING), it proceeds to create the invoice using the _processOrderStatus method.

What Went Wrong With Your Implementation

Your provided code seems to be missing some key components:

  1. The observer model isn’t defined correctly in your config.xml. You need to specify both the frontend and backend models within the <models> tag.

  2. In your observer class, you’re checking if the order is ‘processing’, but Magento uses states rather than statuses. You should check for the Mage_Sales_Model_Order::STATE_PROCESSING constant instead of the string ‘processing’.

  3. The _processOrderStatus method lacks error handling and logging. It’s important to add try-catch blocks around critical operations like invoice creation and sending emails.

  4. Your extension.xml file doesn’t contain any version information, which is a requirement for Magento extensions.

Conclusion

Triggering an event observer in Magento based on order status changes requires identifying the right event (sales_order_save_after), creating an observer model with the appropriate method, and implementing the necessary business logic within that method.

Remember to handle edge cases like failed invoice creation gracefully. You can use Magento’s logging system or a third-party logging library to capture any errors during this process.

For more advanced implementations, consider using the Magento Enterprise Edition’s Order Management Service which provides more sophisticated order processing capabilities.

Don’t hesitate to reach out if you have further questions about Magento development or need assistance with other aspects of your eCommerce platform.

Extensions Configuration Orders CLI

Our network