在Magento的购物车和/或结帐中获取运费 [英] Accessing shipping cost in Magento's cart and/or checkout

查看:143
本文介绍了在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.

shipping_tablerate数据库表包含一个cost字段,该字段在collectRates方法期间填充在Mage_Shipping_Model_Carrier_Tablerate对象中.但是,该字段在堆栈中的其他任何地方都无法访问,例如从报价的地址.

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.

我需要在购物车页面上访问该值,除了实例化Mage_Shipping_Model_Rate_Request对象以传递到collectRates()之外,我都无法找到实现该值的方法.鉴于数据已经从表中加载并且应该可以访问,因此这似乎不必要地效率低下.

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.

我尝试了观察<shipping_carrier_tablerate_load/>事件,但是似乎没有为该模型引发_load事件.

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');

我可以看到计算出的price,但是该模型中没有cost.

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!

谢谢, 乔纳森

推荐答案

首先,在您看到某个实际瓶颈之前,不要过多地担心性能.对众多缓存系统抱有信心.讽刺的是,Magento已经有点像SQL了,所以如果您对商店进行了很好的调整,那么一些额外的查询也不会受到影响.

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.

第二,数据库命中甚至可能不是问题. shipping/rate_request模型似乎没有数据库支持.如果您两次查看它在核心代码中的使用情况

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

您可以看到正在实例化shipping/rate_request模型,然后从已经加载的字段中进行填充.此外,Mage_Shipping_Model_Carrier_Tablerate::collectRates中使用的所有模型都不会从数据库中加载任何内容,它们只是进行计算.

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.

令人钦佩的是,您希望在初次尝试时就构建出性能尽可能高的东西,但是现代OO系统中存在太多复杂的交互作用,无法神奇地知道执行操作的最高性能方式.做必要的事情以获取所需的信息,并在维护版本(或者如果您没有足够的精力进行维护版本)进行性能调整(如果需要)时,当组织中有能力的人对某个地方的速度感到不满时)

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.

这篇关于在Magento的购物车和/或结帐中获取运费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆