Magento - How can I run code when my order is canceled or refunded

Magento - How can I run code when my order is canceled or refunded

August 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How can I run code when my order is canceled or refunded

Handling Magento Order Cancellation and Refund Events

Hello developers! Today, we’re going to dive into Magento’s event system and explore how you can trigger code execution when an order is canceled or refunded in your store.

Understanding Magento’s Event System

Magento uses the observer pattern for its event-driven architecture. When certain events occur (like an order being saved or a customer logging in), Magento fires off these events, giving other parts of the system a chance to react. This allows us to hook into these events and run our custom logic.

Catching Order Cancellation Events

Let’s start with catching when an order is canceled. In version 2.x, you can observe the sales_order_cancel event:

// app/code/Vendor/Payment/Observer/SalesOrderCancelObserver.php
public function execute(\Magento\Framework\Event\Observer $observer)
{
    // Get the order object from the observer
    $order = $observer->getEvent()->getOrder();

    // Your custom logic here to handle the canceled order
    // ...

    return $this;
}

To register this observer, you’ll need a configuration file:

<!-- app/code/Vendor/Payment/etc/events.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_cancel">
        <observer name="vendor_payment_sales_order_cancel_observer" instance="Vendor\Payment\Observer\SalesOrderCancelObserver"/>
    </event>
</config>

Remember, your module must be enabled for the observer to work. You can find more details in the official Magento guide on Event observers.

Handling Refunds via Credit Memo

For handling refunds (via credit memos), you’ll need to observe the sales_model_service_quote_refund_save_after event:

// app/code/Vendor/Payment/Observer/SalesCreditMemoSaveAfterObserver.php
public function execute(\Magento\Framework\Event\Observer $observer)
{
    // Get the credit memo object from the observer
    $creditMemo = $observer->getEvent()->getCreditmemo();

    if ($creditMemo->getState() == \Magento\Sales\Model\Order\Invoice::STATE_REFUNDED) {
        // Your custom logic here to handle the refunded order
        // ...
    }

    return $this;
}

The configuration for this observer would be similar, observing sales_model_service_quote_refund_save_after.

Troubleshooting: Why My Code Didn’t Run

You mentioned that your code in the cancel() method of your payment method model didn’t run. This is likely because Magento doesn’t fire any events when you manually call these methods. The events are only fired when Magento itself triggers them, like when an admin user cancels or refunds an order via the backend interface.

Best Practices and Tips

When dealing with Magento’s event system:

  • Always use events for triggering external actions. Direct method calls can lead to unexpected behavior.
  • Be mindful of performance. If your observer logic is complex, consider doing it asynchronously.
  • Test thoroughly in a development environment before deploying to production.
  • Check the official documentation at Laravel Company for more details.

Conclusion

Catching Magento events like order cancellation and refund allows you to react to important changes in your store’s data flow. Whether you need to send notifications, update third-party services, or perform complex calculations, the event observer pattern provides a clean way to handle these requirements.

Remember to keep your code organized, test thoroughly, and follow best practices for performance and security.

Happy coding! If you have any questions or other topics you’d like me to cover, let me know in the comments.

Extensions Configuration Orders Customers

Our network