在本系列中,我们’重新看Magento’用于计算和显示购物车总计的过程。在介绍中,我们简要介绍了“total” or “total 搜集or”是,看看如何在config.xml中定义一个。现在它’s time to take a closer look at the models and data structure 在volved. 的job of a 总 搜集or model can be divided 在to two processes: Calculation and display.
您’强烈建议您打开Magento代码库,并遵循引用的代码。
As mentioned 在 the previous 文章, the calculation of cart 总s is kicked off with a call to Mage_Sales_Model_Quote::collectTotals. Before taking a look at the code, there are a few things to take note of about the underlying data structure for 总s:
有了它,它’s time to examine the general flow of execution for 搜集Totals. 搜集Totals is run at various points, almost always immediately 之前 the quote is saved. Examples would 在clude during a visit to the cart page, upon the submission of each step during the checkout, and immediately 之前 final order placement. Here’是collectTotals的截短版本,主要代码区域是’re concerned with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
上市 功能 搜集Totals() { / ** * Protect double 总s 搜集ion * / 如果 ($ this->getTotalsCollectedFlag()) { 返回 $ this; } . . . 前言 ($ this->getAllAddresses() 如 $ address) { $ address->setSubtotal(0); $ address->setBaseSubtotal(0); $ address->setGrandTotal(0); $ address->setBaseGrandTotal(0); $ address->搜集Totals(); $ this->setSubtotal((浮动) $ this->getSubtotal() + $ address->getSubtotal()); $ this->setBaseSubtotal((浮动) $ this->getBaseSubtotal() + $ address->getBaseSubtotal()); $ this->setSubtotalWithDiscount( (浮动) $ this->getSubtotalWithDiscount() + $ address->getSubtotalWithDiscount() ); $ this->setBaseSubtotalWithDiscount( (浮动) $ this->getBaseSubtotalWithDiscount() + $ address->getBaseSubtotalWithDiscount() ); $ this->setGrandTotal((浮动) $ this->getGrandTotal() + $ address->getGrandTotal()); $ this->setBaseGrandTotal((浮动) $ this->getBaseGrandTotal() + $ address->getBaseGrandTotal()); } . . . $ this->setTotalsCollectedFlag(真正); 返回 $ this; } |
的parts I’ve truncated deal with dispatching events, zeroing out values 之前 the process begins, validation, and setting item quantities 上 the quote. Here’s an explanation of the main 搜集ion process:
To re-emphasize, no 价钱 在formation exists 上 the quote at all until this process has run the first time. When a product is added to the cart, it is added without 价钱 在fo, and the appropriate 价钱 is looked up and applied 在 上e of the 总 搜集ors.
在报价单上同时收集运输地址和账单地址,然后将其总计,这似乎很奇怪。首先,它’s appropriate for calculations to be done 在 the context of a specific address, because address 在formation may affect things like 税 and 运输. But 如果 the two addresses’总计相加,结果如何正确?密钥位于整个模型的_getAddressItems中,它将返回项目 只要 for the relevant address. (Typically the 运输 address, or the billing address 上 a virtual quote.) Thus you end up with $0 总s 上 the address for which no items are 返回ed. And for multi-shipping, quote items are explicitly 如sociated with 上e address or another, which makes the general 搜集Totals process work out nicely.
计算报价总额的过程似乎仍然有些模糊,但是如果您花一些时间检查核心中定义的各种总量模型,则应该可以点击常规功能。那里’但是,它是总体模型的另一个关键组成部分:在购物车或结帐处(或任何地方)显示总计。它’总计不需要显示,但总计如下“subtotal,” “discount,” “tax” and “grand_total”应该在页面上逐项列出。
您’我看到报价模型上的collectTotals和“collect” 上 a 总 model are the key methods 在volved with calculating and saving 在fo. For displaying, getTotals 上 the quote model and “fetch” 上 the 总 model comprise the main event. Here’s the basic process:
Exactly which block 类 is used to render a specific 总 is determined 如 follows:
在那里,您拥有了!收集和显示过程就这么简单。好吧“simple”可能不是用来形容它们的单词。但是,通过上面的概述,您应该有能力检查所涉及的各种模型的代码,并了解执行流程。
All of the native 总 搜集ors have their own quirks, complexities, and “exceptions”遵守规则,因此找到一个简单明了的例子是一个挑战。为了尽可能简化,我’ll present the following pseudo example of how the two key methods 上 a 总 work. Most 总s follow the same basic process 在 some fashion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
类 MySite_MyModule_Model_Sales_Quote_Address_Total_Mytotal 延伸 Mage_Sales_Model_Quote_Address_Total_Abstract { 上市 功能 __构造() { $ this->setCode('mytotal'); } 上市 功能 搜集(Mage_Sales_Model_Quote_Address $ address) { 父母::搜集($ address); 前言 ($ this->_getAddressItems($ address) 如 $项目) { //这两行代表您所使用的任何逻辑 //用于计算这些金额 $ baseAmt = ... $ amt = ... // Set the item's 总 $项目->setBaseMytotalAmount($ baseAmt); $项目->setMytotalAmount($ amt); //这些方法自动进行求和 //报价地址上的“ mytotal_amount” $ this->_addBaseAmount($ baseAmt); $ this->_addAmount($ amt); } 返回 $ this; } 上市 功能 取(Mage_Sales_Model_Quote_Address $ address) { // Naturally, this exists 上 the quote address because "搜集" ran already $ amt = $ address->getMytotalAmount(); 如果 ($ amt!=0) { $ address->addTotal(数组( '码' => $ this->getCode(), '标题' => 法师::帮手('mysite_mymodule')->__(“我的总计”), '值' => $ amt )); } 返回 $ this; } } |
在下一部分中,我们’ll discuss some final points that are worth consideration 之前 Magento 总s can be considered well and truly conquered.
5 评论s
$ address-如何>getMytotalAmount();获得由$ item-设置的值>setMytotalAmount($ amt);
不会’t您需要从商品中获取值或在地址对象上设置值,例如$ address->getMyTotalAmount();
你好
首先,感谢您的精彩系列!它写得很好,很好地涵盖了一个复杂的主题,将其分解为易于处理的内容。
虽然有一个小音符(可以’不能帮助但要聪明 – sorry). It is not required to set the 总 code within the 总 model 在 the __构造() method of the example. 的总 code is set 上 the model 后 在stantiation by Mage_Sales_Model_Quote_Address_Total_Collector::_initModelInstance().
在构造函数中设置代码赢了’不会造成任何伤害,它将再次被覆盖。如果XML中指定的代码与构造函数中指定的代码之间存在差异,这可能会造成混淆。
再次感谢您的贡献!
If memory serves, $ this->_addAmount 在 the “fetch” method takes care of setting the 总 上 the address.
很棒的文章集!
我有一个问题,如果你不愿意’t mind helping.
I have a shopping cart rule set up for a certain product to get discounted 100%. In the Discount 小计 it DOUBLES the 折扣, but 在 the Grand Total, it calculates the discount correctly.
知道为什么吗?我可以’似乎找不到为什么折扣可能在前端加倍。太奇怪了。
是的,它’s billing and 运输.
所以。
如果($地址->getAddressType() == ‘shipping’){
}
在集合函数中
该网站使用Akismet减少垃圾邮件。 了解如何处理您的评论数据.