访问 Magento 购物车和/或结帐中的运费
请注意,这个问题是关于运费,而不是价格。有一个重要的区别,即运输方式为店主支付的费用是多少,而不是客户支付的费用。
shipping_tablerate
数据库表包含一个 cost
字段,该字段在 collectRates
方法期间填充到 Mage_Shipping_Model_Carrier_Tablerate
对象中。然而,该字段无法在堆栈中的其他任何地方访问,例如从引用的地址。
我需要在购物车页面上访问该值,但除了实例化一个 Mage_Shipping_Model_Rate_Request
对象以传递到 collectRates()
之外,我找不到任何方法来实现它。鉴于数据已经从表中加载并且应该可以访问,这似乎效率低下。
我尝试过观察
事件,但似乎该模型没有引发 _load
事件。
我还尝试从报价中访问费率:
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$address = $quote->getShippingAddress();
$rate = $address->getShippingRateByCode($code ='tablerate_bestway');
我可以看到计算出的价格
,但是该模型中不存在成本
。
在这个阶段,我已经没有想法了。如有任何建议,不胜感激!
谢谢, 乔纳森
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,尽量不要太担心性能,直到您在某个地方看到实际的瓶颈。对众多的缓存系统有信心。更讽刺的是,Magento 已经有点像 SQL 野兽了,所以如果你有一个调优好的商店,一些额外的查询不会有什么坏处。
其次,数据库命中甚至可能不是问题。
shipping/rate_request
模型似乎不受数据库支持。如果您查看核心代码中使用它的两次,您可以看到
shipping/rate_request
模型正在被实例化,然后从已加载的字段中填充。此外,Mage_Shipping_Model_Carrier_Tablerate::collectRates
中使用的所有模型不会从数据库加载任何内容,它们只是进行计算。令人钦佩的是,您希望构建在第一次运行时就尽可能高性能的东西,但是现代面向对象系统中有太多复杂的交互,无法神奇地知道执行某些操作的最高性能方式。做你需要做的事情来获取你需要的信息,并在维护版本期间处理性能调整(如果需要)(或者如果你不够幸运有维护版本,当你组织中有权势的人抱怨某处的速度时) )
第三,当系统不提供对您需要的东西的访问时,这就是类覆盖系统的用途。就像
您基本上调用与以前相同的代码,但将结果存放在某处以供以后访问。与覆盖所能达到的安全程度差不多。
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 codeyou can see the
shipping/rate_request
model is being instantiated, and then populated from already loaded fields. Additionally, all the models used inMage_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
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.