Magento function to remove coupon code from a users cart?

Magento function to remove coupon code from a users cart?

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento function to remove coupon code from a users cart?

Removing Coupon Codes During Checkout Process in Magento

In Magento, removing coupon codes during the checkout process requires a combination of core modifications and custom scripting to handle the rules around country-specific discounts. The Mage_Sales_Model_Quote class is the key component for managing cart contents and applied coupons.

Identifying the Coupon Code to Remove

Firstly, you need to identify the specific coupon code that needs to be removed:

$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach ($quote->getAllItems() as $item) {
    if($item->getDiscountDescription() == "Free Shipping"){
        // This is our free shipping coupon, replace it with the correct one.
    }
}

Removing the Coupon Code During Checkout

To remove a coupon code from a user’s cart during checkout, you can use the removeCoupon() method provided by Magento:

// Assuming 'FREESHIP20' is the free shipping coupon to be removed
$quote->removeCoupon('FREESHIP20');

// If you want to replace it with a new one:
$code = new Mage_SalesRule_Model_Rule();
$code->setCode('INTERNALSHIP15'); // Set the code of your international shipping discount coupon here.
$code->apply($quote);

Updating Coupon Based on Customer’s Shipping Country

To apply this logic based on the customer’s selected country during checkout, you can use the Mage_Checkout_Model_Session to access the selected shipping address and its country:

// Get the current quote from the session
$quote = Mage::getSingleton('checkout/session')->getQuote();

// Get the selected shipping address details
$address = $quote->getShippingAddress();
$countryId = $address->getCountryId();

// Check if the customer is from a non-USA country
if($countryId != 'US'){
    // Remove the free shipping coupon
    $quote->removeCoupon('FREESHIP20');

    // Apply the international shipping discount coupon
    $code = new Mage_SalesRule_Model_Rule();
    $code->setCode('INTERNALSHIP15'); // Set your international shipping discount code here.
    $code->apply($quote);
}

Optimizing Performance

Remember to clear any cached data after modifying the quote:

Mage::app()->getCacheInstance()->cleanType('config');
Mage::app()->getCacheInstance()->cleanType('layout');

This ensures that Magento re-fetches the updated data from the database.

Conclusion

Removing coupon codes during checkout in Magento requires a combination of core class modifications and custom scripting to handle country-specific logic. By using the removeCoupon() method, you can effectively remove existing coupons and apply new ones based on customer location.

For more advanced coupon management features like creating exclusive or tiered discounts, I recommend exploring Laravel Company’s Magento Development Services. Our experienced team specializes in optimizing checkout processes to improve conversion rates for e-commerce businesses worldwide.

Checkout Customers Pricing

Our network