【发布时间】:2011-11-24 18:11:36
【问题描述】:
好的,我创建了自定义 Total 类来添加特殊折扣,一切似乎都运行良好,但由于某种原因我找不到,我的总数被计算了两次!这会导致双倍折扣和不正确的总计。现在,这发生在购物车页面和结帐页面上......但是......当我完成订单时,总数很好,只计算一次,总计很好。
这很奇怪,就像购物车页面调用了两次collect方法,但在完成订单时只调用了一次,但我无法追踪这一切发生在哪里,以及为什么。
为了跳过垃圾代码,我将只粘贴重要的
<sales>
<quote>
<totals>
<mydiscount>
<class>ucon_mydiscount/total_mydiscount</class>
<before>subtotal</before>
</mydiscount>
</totals>
</quote>
</sales>
以及收集器的方法
public function collect(Mage_Sales_Model_Quote_Address $address)
{
parent::collect($address);
$quote = $address->getQuote();
$quoteId = $quote->getEntityId();
$items = $quote->getAllItems();
if (!count($items)) {
return $this;
}
$discount = 0;
$productId = 2556;
foreach($items as $item)
{
if($item->getProduct()->getId() == $productId)
{
$qty = $item->getQty();
$totalPrice = round(($item->getRowTotal()+$item->getTaxAmount()),2);
//discount 10%
$discount = round($totalPrice * 0.1,2);
$discount = 0 - $discount;
}
}
if($discount == 0)
return $this;
$this->_setAmount($discount);
$this->_setBaseAmount($discount);
return $this;
}
和提取器
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
$amount = $address->getMydiscountAmount();
if ($amount != 0) {
$title = Mage::helper('ucon_mydiscount')->__('My discount');
$address->addTotal(array(
'code' => $this->getCode(),
'title' => $title,
'value' => $amount
));
}
return $this;
}
编辑:还有一件事我觉得很奇怪 - 我在我的收集方法中执行 setValue,而不是 addValue,所以即使该方法被调用两次,它也不应该是双值,它应该简单地设置它两次到正确的值。
【问题讨论】:
-
我以Andrey的评论为起点,来自here
-
我曾经遇到过这种情况,但现在找不到我做了什么来解决它。我相信我复制了其中一个明确检查总数是否已经以某种方式使用的 Mage 类。尝试在每个 collect 和 fetch 函数中添加断点或
Mage::log(__METHOD__)以查看两次调用的内容 - 这就是我上次调试它的方式。
标签: magento custom-action