【问题标题】:Magento: Custom totals counted twice?Magento:自定义总数计算了两次?
【发布时间】: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


【解决方案1】:

问题可能是一个总对象属于一个地址对象,而 Magento 订单通常有两个地址 - 一个用于运输,一个用于计费?

因此,您的总数将被调用两次 - 一次是帐单地址,一次是送货地址,并且每个地址的金额总计。您可以尝试检查您收到了哪个地址,然后只对其中一个应用一个值,就像这样;

public function collect(Mage_Sales_Model_Quote_Address $address) {

  $this->_setAddress($address);
  $this->_setAmount(0);
  $this->_setBaseAmount(0);

  if ($address->getAddressType() == 'shipping') { 
    //only apply an actual value to the shipping address

    //... Do your calculation here as above ...

  } 

  return $this;
}

您还必须在 fetch 方法中执行类似的操作...

public function fetch(Mage_Sales_Model_Quote_Address $address) {

  $amount = $address->getMydiscountAmount();

  if ($amount != 0 && $address->getAddressType() == 'shipping') {

    $title = Mage::helper('ucon_mydiscount')->__('My discount');

    $address->addTotal(array(
        'code' => $this->getCode(),
        'title' => $title,
        'value' => $amount
    ));

  }

  return $this;
}

我承认collect 函数可能更漂亮,但还是希望你能明白。

尝试一下,看看您的总数是否在前端和管理区域中正确加起来。

【讨论】:

  • 简直不敢相信,谢谢,一百万年都找不到了 :)
  • 嗨,我添加了if ($address-&gt;getAddressType() == 'shipping') { ,结帐页面中的总数是正确的。但完成订单后出错,似乎我的自定义总数不计。有谁知道怎么回事?
  • @Wakanina 我自己从来没有像这样将前端总数变成后端总数;我需要在后端将总数转换为其他产品,我在checkout_type_onepage_save_order 事件上与观察者一起收集了总数,就像这样,并将具有该值的产品添加到订单中。这个博客看起来是在正确的轨道上:classyllama.com/blog/…
  • 是的,我也跟着那个教程。我添加了自己的答案...谢谢:)
【解决方案2】:

四处寻找,这里有另一个解决方案

public function collect(Mage_Sales_Model_Quote_Address $address) {
    parent::collect($address);

    //Pay attention to this code
    $items = $this->_getAddressItems($address);
    if (!count($items)) {
        return $this; //this makes only address type shipping to come through
    }

    //Do whatever you want here to add discount or fee...

    return $this;
}

通过这样做,折扣或费用只会添加到送货地址,并且会计算一次。所以我们甚至不需要在fetch函数中添加if ($address-&gt;getAddressType() == 'shipping') {。

【讨论】:

    【解决方案3】:

    您是否可以为购物车块添加自己的布局 xml 代码?如果你是,那么很有可能会调用两次块(一个来自基本代码,另一个来自你的代码——即使你只是扩展它),从而复制总价格。如果是这种情况,您将需要删除(破坏性地使用 &lt;remove&gt; 标记)该块的基本布局 xml,然后事情应该就位并正常工作。

    【讨论】:

    • 不幸的是,不是这样。我的布局中有两个购物车块调用 - 一个用于常规购物车页面,一个用于购物车/侧边栏。但是我尝试禁用我自己的所有布局,只使用基本布局,问题仍然存在。有没有办法从哪些方法/块中查看我的 collect 方法被调用?感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    相关资源
    最近更新 更多