Magento observer - Which event to use when a coupon code is canceled

Magento observer - Which event to use when a coupon code is canceled

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento observer - Which event to use when a coupon code is canceled

Managing Coupon Code Cancellation Events in Magento

In your Magento project, you’re currently using an event observer to add free products to the cart when a specific coupon code is applied. This approach works well for the initial application of the coupon. However, you’ve encountered a challenge: the current event does not fire when the customer cancels or removes the coupon.

To solve this, we need to identify the appropriate event that triggers when a coupon is removed from the cart. Let’s explore the Magento events related to coupon management and find the best fit for your requirement.

Understanding Magento Coupon Events

Magento provides several events related to coupon management. Here are some key events you could consider:

  1. salesrule_validator_apply - Fires when a new coupon code is applied.
  2. checkout_controller_onepage_save_order_after - Fires after an order is saved in the checkout process, which may include coupons being removed.

The Ideal Event for Removing Coupon Codes

The event that best suits your needs is checkout_controller_onepage_save_order_after. This event fires after an order has been processed and saved during the checkout flow. It captures all changes made to the cart, including coupon code removal.

By observing this event, you can check if any coupons have been removed from the order data and then take appropriate actions, such as removing the free product added earlier.

Implementing the Coupon Removal Observer

To create an observer for the checkout_controller_onepage_save_order_after event, follow these steps:

  1. Define your event in the config file (events.xml):
<events>
    <checkout_controller_onepage_save_order_after>
        <observers>
            <removeFreeProductObserver>
                <type>singleton</type>
                <class>Your_Namespace_Model_Observer</class>
                <method>removeFreeProduct</method>
            </removeFreeProductObserver>
        </observers>
    </checkout_controller_onepage_save_order_after>
</events>

Replace Your_Namespace_Model_Observer with the actual namespace of your module.

  1. Create the observer class (Observer.php):
class Your_Namespace_Model_Observer
{
    public function removeFreeProduct($observer)
    {
        $event = $observer->getEvent();
        $order = $event->getOrder();

        // Check if any coupons are removed from the order data
        foreach ($order->getCouponCodes() as $coupon) {
            if (!$coupon->isObjectNew()) { // This checks if the coupon is being deleted
                // Logic to remove the free product added earlier, e.g.:
                Mage::helper('core')->log("Coupon removed: " . $coupon->getCode());
            }
        }
    }
}

In this observer method, we iterate through the order’s coupon codes and check if a coupon is being deleted (i.e., the isObjectNew() method returns false). If so, you can proceed to remove the free product added earlier.

Conclusion

To handle the removal of coupon codes in Magento, observe the checkout_controller_onepage_save_order_after event. This event covers all changes made during the checkout process, including coupon code deletion.

By implementing an observer for this event and checking if a coupon is being deleted within the order data, you can effectively remove any free products added earlier when the customer cancels or removes the corresponding coupon.

Remember to replace the placeholder names with your actual namespace and module details. This approach provides a reliable way to manage changes in coupon codes during the Magento checkout process.

For more advanced techniques and best practices for Magento event observers, consider exploring our resources at Laravel Company.

Checkout Catalog Extensions Orders

Our network