douxiegan6468 2010-10-03 05:26
浏览 17
已采纳

访问Magento购物车和/或结账时的运费

Please note, this question is regarding the shipping cost, not price. There is an important difference, i.e. what $$ charge the shipping method incurs for the store owner, as opposed to what is $$ charge is paid by the customer.

The shipping_tablerate database table includes a cost field, which is populated in the Mage_Shipping_Model_Carrier_Tablerate object during the collectRates method. However, that field is not accessible anywhere else in the stack, e.g. from a quote's address.

I need to access that value on the cart page, and I can't find anyway to achieve it, other than to instantiate a Mage_Shipping_Model_Rate_Request object to pass into collectRates(). That seems unnecessarily inefficient given that the data is already loaded from the table and should be accessible.

I have tried Observing the <shipping_carrier_tablerate_load/> event, but it seems that the _load event is not thrown for that model.

I have also tried accessing the rate from the quote:

$quote = Mage::getSingleton('checkout/cart')->getQuote();
$address = $quote->getShippingAddress();
$rate = $address->getShippingRateByCode($code ='tablerate_bestway');

I can see the calculated price, however cost is not present in that model.

At this stage, I'm running out of ideas. Any suggestions gratefully received!

Thanks, Jonathan

  • 写回答

1条回答 默认 最新

  • douliang1900 2010-10-03 19:13
    关注

    First, try not to worry too much about performance until you see an actual bottleneck somewhere. Have faith in the multitude of caching systems. Put more cynically, Magento's already a bit of a SQL beast, so if you have a store tuned well a few extra queries won't hurt.

    Second, the database hit might not even be a problem. The shipping/rate_request Model doesn't appear to be backed by a database. If you look at the two times it's used in the core code

    Mage_Shipping_Model_Shipping::collectRatesByAddress
    Mage_Sales_Model_Quote_Address::requestShippingRates
    

    you can see the shipping/rate_request model is being instantiated, and then populated from already loaded fields. Additionally, all the models used in Mage_Shipping_Model_Carrier_Tablerate::collectRates don't load anything from a database, they just do calculations.

    It's admirable that you want to build something that's as performant as possible in the first go around, but there's too many complex interactions in a modern OO system to magically know the most performant way to do something. Do what you need to to get the information you need, and handle performance tuning (if needed) during a maintenance release (or if you're not lucky enough to have maintenance release, when someone with power in your organization grumbles about the speed somewhere)

    Third, when the system doesn't provide access to something yo need, that's what the class override system is for. Something like

    class Package_Module_Model_Carriertablerate extends
    Mage_Shipping_Model_Carrier_Tablerate
    {
        public function getRate(Mage_Shipping_Model_Rate_Request $request)
        {
            $rate = parent::getRate($request);  
            Mage::register('package_module_carriertablerates', $rate);
            return $rate;
        }
    
    }
    
    ...
    //later, retrieve the rate
    $rates = Mage::registry('package_module_carriertablerates');
    

    You're basically calling the same code as before, but stowing the results away somewhere for later access. About as safe as an override can get.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序