0

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Chris
  • 1,731
  • 3
  • 24
  • 38

2 Answers2

3

shipping methods have built in method in Mage_Shipping_Model_Carrier_Abstract that they all extend:

 public function isActive();

extend your shipping methods and add your logic to that method and don't forget to call parent::isActive(); first

Anton S
  • 12,750
  • 2
  • 35
  • 37
  • @Anton-S Are you sure that shipping modules can be overridden.. I've got all my syntax correct, but for some reason it's not overridding the isActive() function in my override. – Chris Dec 17 '10 at 20:12
  • yes they are if the shipping method does not respect parent::isActive(); you have to override its class directly – Anton S Dec 18 '10 at 12:03
  • sorry to be dense about this but what do you mean by "override the class directly" I think that's what I'm doing now. You can see my code here: http://stackoverflow.com/questions/4474502/overridding-a-shipping-method-what-am-i-missing – Chris Dec 19 '10 at 16:35
  • make sure you are really overriding it , clear cache check for typos and add print_r(get_included_files()); to the end of index.ph and see what files are included and if your class is one of those – Anton S Dec 19 '10 at 19:26
0

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.

Chris
  • 1,731
  • 3
  • 24
  • 38