Magento: Table Rates by Weight, edit the Calculation

I’m currently using Table Rates by Weight for an online store, but I need to customize how the calculation is done.

At the moment, the postage fee is determined by the total weight of orders, but I’d like the calculation to just determine the cost of the post by the heaviest item in the shopping cart.

Do you know how I could override the calculations made by this module? Assistance would make me really grateful.

Thanks in advance.

One thought on “Magento: Table Rates by Weight, edit the Calculation”

  1. Override the Magento class used to calculate these rates Mage_Shipping_Model_Shipping. When a rate request is made, this class’ collectRates method is called using a Mage_Shipping_Model_Rate_Request object, which has the items and weight pre-populated. However, by subclassing that class with our override, we can be tricksy and get the rate we want.

    class MyNamespace_MyModule_Model_Shipping extends Mage_Shipping_Model_Shipping {
    
        public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
            $maxWeight = 0;
            foreach($request->getAllItems() as $item) {
                $maxWeight = max($maxWeight, $item->getRowWeight());
            }
            $request->setPackageWeight($maxWeight);
    
            return parent::collectRates($request);
        }
    }
    

    Notice that here we’ve minimized the amount of changed code and our module can be cleanly removed (and we can even upgrade Magento!) with low risk.

    Hope that helps!

    Thanks,
    Joe

Leave a Reply

Your email address will not be published. Required fields are marked *