Displaying “Free Shipping” Text in Magento: A Definitive Guide
If you’re a Magento store owner or developer attempting to display “Free shipping” text instead of the price for free shipping methods, this guide is for you. I’ll walk you through the exact steps required to achieve this, leveraging my extensive experience with e-commerce development and Magento.
Understanding the Free Shipping Method
In Magento, the free shipping method is defined in the Mage_Shipping_Model_Carrier_Freeshipping model. This model is responsible for setting the price of the free shipping method to 0.00 and calculating its cost.
The Common Misconception: Changing the Price Doesn’t Work
Many developers, including yourself, might have attempted to change the setPrice('0.00') line to setPrice("Free shipping"). While it seems logical, this approach doesn’t work because the price in Magento is expected to be a numeric value.
The Correct Approach: Overriding the Model
Instead of directly modifying the core file (which is highly discouraged due to version control issues and possible data loss), we’ll create an extension that overrides the Mage_Shipping_Model_Carrier_Freeshipping model. This ensures our changes are preserved across Magento upgrades.
Step-by-Step Implementation
- Create a module configuration file: In your project’s
app/etc/modulesfolder, create a new file namedYour_Company_Freeshipping.xml. Replace “Your Company” with your actual company name.
<?xml version="1.0"?>
<config>
<modules>
<Your_Company_Freeshipping>
<active>true</active>
<codePool>local</codePool>
<depends><Mage_Shipping /></depends>
</Your_Company_Freeshipping>
</modules>
</config>
- Create the override model: In your project’s
app/code/local/Your/Company/Freeshipping/etc/config.xmlfile, define the model override.
<?xml version="1.0"?>
<config>
<global>
<models>
<shipping>
<rewrite>
<carrier_freeshipping>Your_Company_Freeshipping_Model_Carrier_Freeshipping</carrier_freeshipping>
</rewrite>
</shipping>
</models>
</global>
</config>
- Create the overridden model: In your project’s
app/code/local/Your/Company/Freeshipping/Model/Carrierfolder, create a new file namedFreeshipping.php. This is where we’ll implement our custom logic.
<?php
class Your_Company_Freeshipping_Model_Carrier_Freeshipping extends Mage_Shipping_Model_Carrier_Freeshipping
{
public function getName()
{
return 'Free shipping';
}
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!$this->getConfigFlag('active', $request->getStore())) {
return false;
}
$result = Mage::getModel('shipping/rate_result');
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
// Set the rate label to "Free shipping" instead of a price
$method->setMethod($this->_code);
$method->setMethodTitle('Free shipping');
$method->setPrice(0.00);
$result->append($method);
return $result;
}
}
Conclusion and Further Reading
In this guide, we’ve explored the intricacies of displaying “Free shipping” text in Magento 1.7. By understanding how to override core models correctly and following the steps outlined above, you should now be able to achieve your desired outcome.
Remember, always strive for a clean codebase by avoiding direct modifications to core files. Overriding is the recommended approach for maintaining flexibility and upgradeability.
For more advanced Magento customizations and tips on improving e-commerce performance, I recommend exploring our comprehensive guides at Laravel Company. We cover topics ranging from SEO optimization to high-performance caching strategies tailored for Magento stores.