Hiding Flat Rate Shipping When Free Shipping Is Applied in Magento
In Magento, when you configure free shipping based on order totals, it’s common to still see the flat rate shipping option displayed alongside the free shipping message. This can be confusing for customers who expect all options to be free once they qualify. In this post, we’ll walk through how to automatically disable the flat rate shipping method when free shipping is active.
Understanding the Problem
The issue arises because Magento doesn’t natively handle the logic of disabling paid shipping methods when free shipping is available. This leads to an awkward presentation where both options are listed, even though only one (free) should be visible.
How to Disable Flat Rate Shipping When Free Is Active
To resolve this, we’ll create a custom observer that checks for free shipping availability and disables the flat rate method accordingly. Here’s how:
- Create a Custom Observer Module
First, create a new module in your Magento setup. If you haven’t done this before, follow our guide on Creating Custom Modules for step-by-step instructions.
- Create the Observer Class
In your newly created module’s etc directory, create an XML file named events.xml. Inside this file, define a new observer that listens to Magento’s controller_action_predispatch_checkout_cart_index event:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_checkout_cart_index">
<observer name="disable_flat_rate_shipping_if_free_is_active" instance="Vendor\ModuleName\Model\Observer\DisableFlatRateShipping"/>
</event>
</config>
- Implement the Observer Logic
Create a new file DisableFlatRateShipping.php under Model/Observer. In this file, implement the observer class as follows:
namespace Vendor\ModuleName\Model\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProviderInterface;
use Magento\Quote\Model\ShippingAddressManagementInterface;
use Magento\Checkout\Helper\Data as CheckoutHelper;
class DisableFlatRateShipping implements ObserverInterface
{
private $checkoutSession;
private $registry;
public function __construct(
\Magento\Framework\Session\SessionManagerInterface $checkoutSession,
\Magento\Framework\Registry $registry
) {
$this->checkoutSession = $checkoutSession;
$this->registry = $registry;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$quote = $this->checkoutSession->getQuote();
if ($quote->getItemsCount() && $quote->getShippingAddress()) {
// Check for free shipping availability
$freeShippingAvailable = false;
foreach ($quote->getAllAddresses() as $address) {
$request = [
'cart_id' => $quote->getId(),
'address_id' => $address->getId(),
'shipping_method' => null,
];
$result = Mage::getSingleton('shipping/session')->fetch ShippingAssignmentProviderInterface->getShippingRates($request);
if (isset($result['rates']['free']) && count($result['rates']['free'])) {
$freeShippingAvailable = true;
}
}
// If free shipping is available, disable flat rate
if ($freeShippingAvailable) {
Mage::unregister('shipping_methods');
unset(Mage::registry('is_shipping_available'));
}
}
}
}
This observer checks for the availability of free shipping methods and, if found, removes the shipping methods registry and clears the session data.
- Flush the Cache
After implementing this change, don’t forget to flush your Magento cache under System > Cache Management > Flush Magento Cache.
Conclusion
By following these steps, you’ll be able to automatically hide the flat rate (or standard) shipping option when free shipping is active in Magento. This improves the user experience by displaying only relevant shipping options during checkout.
Remember, customizing Magento’s core files should be approached with caution. Always create a backup of your Magento setup before making any changes. If you’re not comfortable implementing this yourself, consider contacting our expert Magento development team for professional assistance.
Happy coding!