Hide other shipping methods when free shipping is enabled

March 19, 2013  |  1 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

When free shipping method is enabled, it is shown along with other available shipping methods unlike free payment method ‘Zero Subtotal Checkout’.
There is no harm in showing other payment methods along with free shipping method. Nevertheless some merchants wants to hide rest of the methods when it is enabled.

There are many ways to do it. One of the way is to override the method:
Mage_Checkout_Block_Onepage_Shipping_Method_Available::getShippingRates()

1. Rewrite the block class

File: app/code/local/MagePsycho/Shipmentfilter/etc/config.xml:
Code:

...
<blocks>
	...
	<checkout>
		<rewrite>
			<onepage_shipping_method_available>MagePsycho_Shipmentfilter_Block_Onepage_Shipping_Method_Available</onepage_shipping_method_available>
		</rewrite>
	</checkout>
	...
</blocks>
...

2. Override the getShippingRates() method

File: app/code/local/MagePsycho/Shipmentfilter/Block/Onepage/Shipping/Method/Available.php:
Code:

<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Shipmentfilter
 * @author     magepsycho@gmail.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class MagePsycho_Shipmentfilter_Block_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
	public function getShippingRates()
	{
		$rates = parent::getShippingRates();
		if (array_key_exists('freeshipping', $rates)) {
			$rates = array('freeshipping' => $rates['freeshipping']);
		}

		return $rates;
	}
}

3. Refresh the cache. Bingo!