Magento create custom event (order_status_changed)

Magento create custom event (order_status_changed)

October 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento create custom event (order_status_changed)

Creating Custom Events for Magento Order Status Changes

As an experienced developer working with Magento, I often encounter situations where clients require specific actions to be triggered based on certain events, such as when the status of an order changes. In this post, we’ll walk through a detailed guide on how to create a custom event in Magento and observe it using an observer class.

Understanding the Problem

The issue at hand involves creating an event for exporting inventory whenever the order status is changed to ‘completed’ within the Magento admin panel. The developer has created two modules: one for generating the event when the order status changes, and another for observing this event in a separate module.

However, there seems to be a problem with the event generation, as the observer class cannot print anything or exit when observing the custom event (sales_order_status_change). The same observer works fine when observing built-in Magento events like catalog_controller_product_view.

Let’s dive into the provided code and analyze the issue step by step.

Analyzing the Event Generator Module

The developer has created a basic configuration for the event generator module:

<modules>
  <Gol_Eventgenerator>
    <active>true</active>
    <codePool>local</codePool>
  </Gol_Eventgenerator>
</modules>

They have also configured the models to rewrite the sales_order model and extend it with their custom class:

<models>
  <sales>
    <rewrite>
      <order>Gol_Eventgenerator_Model_Order</order>
    </rewrite>
  </sales>
</models>

In the Gol_Eventgenerator\Model\Order.php file, they have overridden the setState() method to dispatch a custom event:

class Gol_Eventgenerator_Model_Order extends Mage_Sales_Model_Order
{
    public function setState($state, $status = false, $comment = '', $isCustomerNotified = null)
    {
        //if I print something here and exit, it does will.
        Mage::dispatchEvent('sales_order_status_change', array(
            'order' => $this,
            'state' => $state,
            'status' => $status,
            'comment' => $comment,
            'isCustomerNotified' => $isCustomerNotified
        ));
        return parent::setState($state, $status, $comment, $isCustomerNotified);
    }
}

The issue seems to be with the event generation itself. Although the developer has overridden the setState() method and attempted to dispatch the custom event, there is no indication that the event is actually being generated.

Analyzing the Observer Module

The observer module appears to be correctly configured:

<events>
  <sales_order_status_change>
    <observers>
      <Gol_Inventory_observer>
        <type>singleton</type>
        <class>Gol_Inventory_Model_Observer</class>
        <method>exportEnventory</method>
      </Gol_Inventory_observer>
    </observers>
  </sales_order_status_change>
</events>

The observer class (Gol_Inventory\Model\Observer.php) is also set up correctly:

class Gol_Inventory_Model_Observer
{
  public function exportEnventory($observer)
  {
      echo "Inside exportEvnentory method";
      exit;
    //$order = $observer->getEvent()->getOrder();
    //$state = $observer->getEvent()->getState();
    //$status = $observer->getEvent()->getStatus();
  }
}

However, when observing the custom event (sales_order_status_change), this observer class is unable to print anything or exit. This suggests that either the event is not being generated correctly in the first place, or there might be a configuration issue preventing it from being observed.

Potential Causes and Solutions

There could be several reasons why the custom event is not being triggered:

  1. Incorrect event name: Ensure the event name ‘sales_order_status_change’ matches exactly with the one dispatched in the setState() method of the Gol_Eventgenerator_Model_Order class.

  2. Check the observer configuration: Verify that the observer module is enabled and that the event configuration is correct in the app\code\local\Gol\Inventory\etc\config.xml file.

  3. Event generation timing: Make sure the custom event is being dispatched at the appropriate time, i.e., when the order status actually changes within the admin panel.

  4. Observer priority: Check if there are other observers with a higher priority that might be preventing this observer from executing.

Frontend vs Admin Events

It’s essential to note that in Magento, events for frontend and admin operations may have some differences:

  1. Event generation timing: In the case of frontend events, they are typically generated during controller actions or block rendering processes. For admin events, they are usually triggered by controller actions performed within the admin panel.

  2. Event configuration: The event configuration might differ between frontend and admin modules. Ensure that the event is configured correctly for the specific context (frontend or admin).

  3. Observer priority: The order of observers and their priorities can also affect how they process events in different contexts.

Conclusion

Creating custom events in Magento involves several steps, including configuring observer modules, defining events in the config.xml file, and ensuring proper event generation within the relevant model methods.

In this case, it appears that either the event is not being generated correctly or there might be a configuration issue preventing the observer from executing. To resolve the problem, carefully review the provided code, ensure the event name matches exactly, check the observer configuration, and verify that the event is dispatched at the appropriate time.

Additionally, consider exploring other resources like the official Magento documentation or online forums for further insights into custom event creation and observation in Magento.

Remember to keep your code well-organized, follow best practices, and refer to reliable sources for accurate guidance when working with Magento development tasks.

Catalog Extensions Admin Configuration

Our network