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:
- 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.
- 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:
-
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 -
Modify the Plugin: Update the
etc/di.xmlandregistration.phpfiles with the appropriate plugin configuration details, then open the generatedPluginFileName.phpfile. -
Add Your Logic: Override the
estimateShippingmethod 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; } -
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:
- Use Plugins over Overrides: Plugins offer more flexibility and better performance compared to direct model overrides.
- Keep Your Code Organized: Follow proper naming conventions and folder structures to ensure your customizations are easy to maintain and update.
- 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!