magentomagento-1.4

How to toggle Shipping Methods Based on Products?


I want to be able to toggle what shipping method is used based upon the items that are in the cart. Where is the best place to "check" this and grab the right shipping method?

The way it will work is that there will be a standard shipping method that is used, and then, if there are certain items in the cart another method will override that other method.

I think I could do this by hacking around in the individual shipping modules, but I'd like to do this the "right" way.


Solution

  • Try and Try as I did to implement a custom override, I was only able to find success when I copied the entire Tablerates.php file to local/Mage/Shipping/Model/Carrier/Tablerates.php

    isActive() was still not "the way" at that point. I had to introduce some code in the collectRates() function like so:

           // check the store 
            if (Mage::app()->getStore()->getId() == 2){
    
                // check for free shipping
                $packageValue = $request->getPackageValueWithDiscount();
                $freeShipping = ($request->getFreeShipping()) || ($packageValue >=  Mage::getStoreConfig("carriers/freeshipping/free_shipping_subtotal", $this->getStore()));
                if($freeShipping)
                    return false;
    
                $foundFlag = false;
                foreach ($request->getAllItems() as $item) {
                    $org_product = Mage::getModel('catalog/product')->load($item->getProductId());
                    if($org_product->getDeliveryFlag() == "workstationmats")
                    {
                        $foundFlag = true;
                    }   
                }
    
                if ($foundFlag == false)
                    return false;
            }
            // end shpping mod
    

    This was placed right at the beginning of the collectRates function.