Magento event on stock change?

Magento event on stock change?

August 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento event on stock change?

Magento Stock Change Events: A Comprehensive Guide

Hello everyone! In this post, we’re going to dive into the world of Magento events related to stock changes. Matthias raised an excellent question about capturing changes in product stock quantities due to sales or order cancellations.

Understanding Magento’s Default Event

Let’s first clarify what happens with Magento’s default cataloginventory_stock_item_save_after event.

This event is triggered whenever a product stock item is saved, regardless of the reason for the change (manual update, order placement, or cancelation). However, it only gets fired when the save action occurs—it doesn’t capture real-time changes like automatic updates related to orders.

As Matthias mentioned, this event doesn’t fire correctly for changes triggered by sales or order cancellations because the stock isn’t actually being “saved” in those cases; it’s adjusted dynamically based on order information.

Capturing Real-Time Stock Changes

To address Matthias’ requirement of tracking real-time adjustments caused by order placements and cancellations, we need to look at Magento’s order events.

The sales_order_save_after event is fired whenever an order is saved or updated in the system. This includes when orders are placed, edited, invoiced, shipped, or even canceled. Within this event observer, you can access the order data and check for changes in stock quantities using the following code snippet:

public function execute($observer)
{
    $event = $observer->getEvent();
    $order = $event->getOrder();

    // Check if an item's stock has changed due to this order event
    foreach ($order->getItems() as $item) {
        if ($item->getIsInStock() != $item->getProduct()->getStockItem()->getQty()) {
            // Stock has been changed by this order. Perform your logic here.
        }
    }

    return $this;
}

This approach allows you to track changes in stock quantities caused by any order-related action, providing a more robust solution than relying on product saving events.

Conclusion

In summary, while Magento’s core cataloginventory_stock_item_save_after event can be useful for manual stock updates, it falls short when capturing dynamic adjustments related to orders.

The sales_order_save_after event, combined with checking the differences between order items and their corresponding stock data, offers a more comprehensive way to track real-time changes in product stock quantities due to sales or cancellations.

I encourage you to explore this approach further and let us know if you have any other Magento-related questions. For more detailed tutorials on customizing your Magento events and observers, I recommend checking out the resources available at https://laravelcompany.com/magento-development.

Stay tuned for more Magento insights!

Catalog Extensions Themes Orders

Our network