Search code examples
magento

remove all other shipping method if there's a method having shipping amount zero


Could it be possible to remove all the shipping methods except the one having shipping amount 0?

There may be a promotion rule which will return the amount 0 for shipping, so it must not be removed.

conditional free shipping


Solution

  • Display only shipping methods with price 0 (zero) if any

    In my module I'd overridden the Mage_Checkout_Block_Onepage_Shipping_Method_Available block as:

    config.xml

    <global>
        <blocks>
            <checkout>
                <rewrite>
                    <onepage_shipping_method_available>Excellence_Subscription_Block_Checkout_Onepage_Shipping_Method_Available</onepage_shipping_method_available>
                </rewrite>
            </checkout>
        </blocks>
    </global>
    

    Excellence_Subscription_Block_Checkout_Onepage_Shipping_Method_Available

    <?php
    
    class Excellence_Subscription_Block_Checkout_Onepage_Shipping_Method_Available extends Mage_Checkout_Block_Onepage_Shipping_Method_Available {
    
        public function getShippingRates() {
            $groups = parent::getShippingRates();
            $free = array();
            foreach ($groups as $code => $_rates) {
                foreach ($_rates as $_rate) {
                    if (!$_rate->getPrice() > 0) {
                        $free[$code] = $_rates;
                    }
                }
            }
            if (!empty($free)) {
                return $this->_rates = $free;
            }
            return $groups;
        }
    
    }