2

I want to enable/disable free shipping based on my custom quote conditions, it works fine when free shipping is enabled from admin but I want it to work also when free shipping is disabled from admin. I tried the following solutions but none of them worked for me - How do I enable free shipping in backend only (for order entry)

How to programatically disable shipping method in magento

https://stackoverflow.com/questions/26403751/how-to-get-enable-freeshipping-for-special-customers-in-magento-according-to-a

2
  • If you only provide shipping with an amount of 0, you can use shopping cart rules to achieve this. However it's applied to another shipping method than "free shipping".
    – luemic
    Commented May 11, 2016 at 6:44
  • I have added a custom column in quote table and I want to allow free shipping condition on that column's value.
    – Piyush
    Commented May 11, 2016 at 6:48

1 Answer 1

1

You need to rewrite the following class:

Mage_Shipping_Model_Shipping

and modify the following method to:

 /**
 * Get carrier by its code
 *
 * @param string $carrierCode
 * @param null|int $storeId
 * @return bool|Mage_Core_Model_Abstract
 */
public function getCarrierByCode($carrierCode, $storeId = null)
{
    if (($carrierCode == 'freeshipping' && $customCondition) || !Mage::getStoreConfigFlag('carriers/'.$carrierCode.'/'.$this->_availabilityConfigField, $storeId)) {
        return false;
    }
    $className = Mage::getStoreConfig('carriers/'.$carrierCode.'/model', $storeId);
    if (!$className) {
        return false;
    }
    $obj = Mage::getModel($className);
    if ($storeId) {
        $obj->setStore($storeId);
    }
    return $obj;
}

and you need to rewrite

Mage_Shipping_Model_Carrier_Freeshipping

(or better create a new carrier based on freeshipping)

and make the following changes:

/**
 * FreeShipping Rates Collector
 *
 * @param Mage_Shipping_Model_Rate_Request $request
 * @return Mage_Shipping_Model_Rate_Result
 */
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    if (!$customCondition && !$this->getConfigFlag('active')) {
        return false;
    }

    $result = Mage::getModel('shipping/rate_result');

    $this->_updateFreeMethodQuote($request);

    if (($request->getFreeShipping())
        || ($request->getBaseSubtotalInclTax() >=
            $this->getConfigData('free_shipping_subtotal'))
    ) {
        $method = Mage::getModel('shipping/rate_result_method');

        $method->setCarrier('freeshipping');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('freeshipping');
        $method->setMethodTitle($this->getConfigData('name'));

        $method->setPrice('0.00');
        $method->setCost('0.00');

        $result->append($method);
    }

    return $result;
}
4
  • Can You please explain what changes I have to made in rewrite files?
    – Piyush
    Commented May 11, 2016 at 9:37
  • In Mage_Shipping_Model_Shipping the first if condition is extended: if (($carrierCode == 'freeshipping' && $customCondition) ||.. which ignores the active flag if your custom condition is met. The same is applied to Mage_Shipping_Model_Carrier_Freeshipping in the first if if (!$customCondition &&.. which ignores the active flag if your custom condition is met. Please note that you need to implement the calculation of the condition for yourself, but that's another question.
    – luemic
    Commented May 11, 2016 at 11:09
  • Please let me know if there's some confusion left or mark this issue as solved. Thanks.
    – luemic
    Commented May 12, 2016 at 9:54
  • Thank You Michael , I Created a custom shipping method to meet the requirements.
    – Piyush
    Commented May 12, 2016 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.