Magento: Table Rates by Weight, edit the Calculation

烂漫一生 提交于 2019-12-12 17:15:45

问题


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.


回答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



来源:https://stackoverflow.com/questions/5325663/magento-table-rates-by-weight-edit-the-calculation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!