Hiding Shipping Method on Cart Page in Magento
When it comes to e-commerce platforms, Magento offers robust flexibility but sometimes requires custom tweaks for specific business requirements. One common need is the ability to hide or remove shipping methods from the cart page under certain conditions. In this post, we’ll explore how to achieve this and resolve an issue related to free delivery based on order value.
Understanding the Problem
The scenario you’re facing involves Magento saving the user’s chosen shipping method in session even when the order amount qualifies for free shipping. This results in unnecessary displays of shipping charges that confuse shoppers.
The Root Cause: Magento’s Shipping Method Logic
Magento saves the selected shipping method as a session variable after the customer proceeds to checkout. When the cart is empty or the order total doesn’t meet your free shipping threshold, it displays the saved method along with its associated fees.
To fix this, we need to override this behavior and ensure that no shipping methods are displayed when the criteria for free delivery are met.
The Solution: Customizing Magento Core Functionality
Step 1: Create a Custom Shipping Method
First, let’s create a custom shipping method that will handle our specific logic. In your app/code/local directory (or use Vendor/Module if you’re using composer), create a new module following the Magento naming conventions:
app/code/local/Namespace/ShippingMethod
Inside this module, create the necessary files and folders for your shipping method:
- app/code/local/Namespace/ShippingMethod/etc/config.xml
- app/code/local/Namespace/ShippingMethod/Model/Carrier.php
- app/code/local/Namespace/ShippingMethod/Block/Form.php
Step 2: Custom Shipping Method Configuration
In config.xml, configure your new shipping method:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_ShippingMethod>
<active>true</active>
<codePool>local</codePool>
</Namespace_ShippingMethod>
</modules>
<global>
<models>
<shippingmethod>
<class>Namespace_ShippingMethod_Model</class>
</shippingmethod>
</models>
</global>
</config>
Step 3: Implementing Custom Shipping Logic
In Carrier.php, override the core shipping method behavior:
namespace Namespace\ShippingMethod\Model;
class Carrier extends \Mage_Core_Model_Abstract
{
public function getAllowedMethods()
{
$allowed_methods = array(
'custom_shipping' => 'Custom Shipping Method'
);
// Check if order amount qualifies for free shipping
$session = Mage::getSingleton('checkout/session');
$quote = $session->getQuote();
$order_amount = $quote->getGrandTotal();
if ($order_amount > 15) {
return array(); // Return empty array to hide all methods
}
return $allowed_methods;
}
}
Step 4: Enabling the Custom Shipping Method
Finally, enable your custom shipping method in System > Configuration > Sales > Delivery Methods.
Conclusion
By following this approach, you can effectively override Magento’s default shipping method behavior. This allows you to hide all shipping methods when a customer’s order amount qualifies for free delivery based on your specific business rules.
Remember that while this solution resolves the immediate issue of displaying unnecessary shipping charges, it doesn’t alter the core saving mechanism for selected shipping methods in the session. To completely eliminate this, consider implementing a custom checkout workflow or using a third-party extension designed for advanced shipping logic.
For more detailed guides and best practices on extending Magento functionality, feel free to explore our Laravel Company blog. Happy coding!