Remove free shipping method after apply coupon code

Remove free shipping method after apply coupon code

October 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Remove free shipping method after apply coupon code

Removing Free Shipping Method After Applying Coupon Code

In Magento 2, managing shipping methods based on applied discounts is a common scenario that often arises when you want to offer free shipping for certain promotional periods or minimum order amounts. In this guide, we’ll walk through the process of removing the free shipping method once a coupon code has been applied.

Understanding Coupon Code Application

Before diving into the code, let’s first understand how Magento handles coupon code application:

  1. Cart Page: When you add an item to the cart and apply a coupon code, Magento calculates the new subtotal, tax, discounts, and shipping rates.
  2. Onepage Checkout: Similar to the cart page, once you enter your address details, Magento recalculates everything based on the applied coupon.

Removing Free Shipping Method

To remove the free shipping method after a coupon code is applied, you’ll need to modify the estimateShipping method in the Magento\Quote\Model\Shipping::class.

Here’s how you can achieve this:

  1. Create a Plugin: Open your terminal and run the following command to create a new plugin file in your custom theme or module.

    php bin/magento setup:plugin:create --module=Vendor_Module --plugin-name=Magento\Quote\Model\Shipping::estimateShipping
    
  2. Modify the Plugin: Update the etc/di.xml and registration.php files with the appropriate plugin configuration details, then open the generated PluginFileName.php file.

  3. Add Your Logic: Override the estimateShipping method and add your custom conditions to remove the free shipping method. Here’s an example of how you can do this:

    public function afterEstimateShipping($subject, $result, array $carrierCode = null)
    {
        if ($subject->getQuote()->hasAppliedRule()) { // Check if any coupon code is applied
            foreach ($result->getAllRates() as $rate) {
                if ('freeshipping_fixed' === $rate->getMethod()) { // Free Shipping method code
                    unset($result->getAllRates()[$rate->getId()]);
                    break;
                }
            }
        }
    
        return $result;
    }
    
  4. Clear Cache and Recompile: After updating the plugin, run the following commands to clear your cache and recompile:

    php bin/magento cache:clean
    php bin/magento setup:upgrade
    php bin/magento setup:static-content:deploy -f
    

Verifying Your Changes

Once you’ve made these modifications, apply a coupon code in the cart or onepage checkout pages. You should notice that the free shipping method is automatically removed if a coupon code has been applied.

Remember to test this change thoroughly across different scenarios, including edge cases and varying amounts of products in the cart.

Best Practices

Here are some best practices to keep in mind while working with Magento 2 plugins:

  1. Use Plugins over Overrides: Plugins offer more flexibility and better performance compared to direct model overrides.
  2. Keep Your Code Organized: Follow proper naming conventions and folder structures to ensure your customizations are easy to maintain and update.
  3. Test Before Deployment: Always test your changes in a staging environment before deploying them to the live site.

Conclusion

Removing free shipping methods after applying coupon codes requires understanding Magento’s event system and plugin architecture. By following the steps outlined above, you can create a clean and efficient solution for managing shipping options based on applied discounts.

If you’re looking for more advanced techniques or encounter any issues during this process, feel free to explore our comprehensive guides at Laravel Company. We provide in-depth tutorials and best practices for Magento 2 development that can help you optimize your e-commerce platform.

Happy coding!

Magento 2 Checkout Extensions Themes

Our network