Magento 1.9 Shipping Method – Setting from Observer Based on Subtotal

free-shippingmagento-1.9shippingshipping-methods

I am trying set shipping method based on subtotal from cart page. If subtotal is less than certain amount then customer will have Flat rate as shipping method else it will be free shipping.

Best Answer

One approach can be filtering of shipping methods based on sub-total. This way you can activate or deactivate required shipping methods.

For filtering shipping methods:

1> Rewrite the shipping model class: Mage_Shipping_Model_Shipping

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

Code:

...
<global>
    ...
    <models>
        <shipping>
            <rewrite>
                <shipping>MagePsycho_Shipmentfilter_Model_Shipping</shipping>
            </rewrite>
        </shipping>
    </models>
    ...
</global>

2> Override the method: collectCarrierRates()

File: app/code/local/MagePsycho/Shipmentfilter/Model/Shipping.php

Code:

<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Shipmentfilter
 * @author     magepsycho@gmail.com
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class MagePsycho_Shipmentfilter_Model_Shipping extends Mage_Shipping_Model_Shipping
{
    public function collectCarrierRates($carrierCode, $request)
    {
        if (!$this->_checkCarrierAvailability($carrierCode, $request)) {
            return $this;
        }
        return parent::collectCarrierRates($carrierCode, $request);
    }

    protected function _checkCarrierAvailability($carrierCode, $request = null)
    {
        $subtotal = Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
        $amountToCheck = 250; //Edit this amount
        if ($subtotal > $amountToCheck) {
            if($carrierCode == 'flatrate'){ #Hide Flat Rate 
                return false;
            }
        }
        return true;
    }
}
Related Topic