Removing Shipping & Handling Fees for Free Shipping in Magento
As an experienced Magento developer, I often encounter clients facing this common issue: they want to completely eliminate the “Shipping & Handling” subtotal line from their checkout and invoices when free shipping is applied. In this post, we’ll explore a practical solution to achieve exactly that.
Understanding the Challenge
Before diving into the code, let’s clarify the problem:
- You have two shipping methods: one with a $10 fee for orders under $200, and another default free shipping method for orders over $200.
- When the free shipping method is applied, you see a “Shipping & Handling” subtotal of $0.00 in both the checkout and invoices.
- You want to hide this line entirely when free shipping is used.
Why It’s Worth Removing This Line
While it might seem like a minor aesthetic issue, removing the “Shipping & Handling” line has several benefits:
- Improved Checkout Experience: A cleaner, less-cluttered checkout can increase conversion rates.
- Consistency in Invoices: Having a consistent structure across all invoices enhances professionalism.
The Solution: Override the Sales Model
To solve this issue, we’ll override the core Magento model Mage_Sales_Model_Order and modify its collection to exclude the “Shipping & Handling” fee when free shipping is involved. Here’s how:
-
Create a custom module: If you haven’t already, create a new module called ‘RemoveHandlingFee’. For this example, let’s assume it’s located at
app/code/local/YourCompany/RemoveHandlingFee. -
Override the Order Model: In your module, navigate to
Modeldirectory and create an override for the order model.
<config>
<modules>
<YourCompany_RemoveHandlingFee>
<version>1.0</version>
</YourCompany_RemoveHandlingFee>
</modules>
<global>
<models>
<sales>
<rewrite>
<order>YourCompany_RemoveHandlingFee_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
- Create the Overridden Order Model: In your
app/code/local/YourCompany/RemoveHandlingFee/Modeldirectory, create a new file namedSales.php:
<?php
class YourCompany_RemoveHandlingFee_Model_Sales_Order extends Mage_Core_Model_Abstract
{
protected function _beforeSave()
{
if ($this->getShippingAmount() == 0 && $this->getFreeShippingAmount() > 0) {
$this->setShippingAmount(0);
$this->setShippingDescription('');
}
parent::_beforeSave();
}
}
- Clear Cache and Reindex: After implementing this override, remember to clear your cache (
Mage::app()->cleanCache()in a controller orphp bin/magento c:ffor Magento 2) and reindex the data if you’re using Magento 2.
Conclusion
By overriding the core order model with our custom logic, we can effectively remove the “Shipping & Handling” line from both the checkout process and invoices when free shipping is applied. This approach ensures a cleaner user experience without any significant performance overhead.
Remember that coding in Magento requires careful consideration of version compatibilityâwhat works on 1.7 might not work on 2.3 as-is. Always test your changes thoroughly before deploying them to a live site.
If you’re looking for more advanced techniques or need help optimizing your Magento store, feel free to reach out to our team at Laravel Company. We specialize in providing high-quality, efficient solutions tailored to your business needs.