2011-11-24 50 views
4

好的,我創建了自定義的Total類,用於添加特殊折扣,並且一切看起來都正常,除了由於某種原因我無法找到,我的總計算了兩次!這會導致雙倍的折扣,並導致總計不正確。現在,這發生在購物車頁面和結帳頁面上......但是......當我完成訂單時總數很好,只計算一次,總計很好。Magento:自定義總數計數兩次?

奇怪的是,這就像收集方法被稱爲購物車頁面的兩次,但只有一次完成訂單,但我可以'追蹤所有這些發生的地方,爲什麼。

要跳過垃圾代碼,我就只粘貼重要

 <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; 
} 

編輯:還有一兩件事,我覺得很奇怪 - 我在我的collect方法中執行setValue,而不是addValue,所以即使方法被調用了t它不應該是雙重值,它應該簡單地將它設置爲正確的值兩次。

+0

我用Andrey的評論作爲出發點,從[這裏](http://stackoverflow.com/questions/4363876/how-to-set-custom-grand-total-before-checkout-process-in-magento ) – Relja

+0

我經歷過這一次,但現在不能找到我所做的修復它。我相信我複製了一個Mage類,明確地檢查了總數是否已經被使用了。嘗試在每個收集和提取函數中添加斷點或「Mage :: log(__ METHOD __)」,以查看兩次調用的內容 - 這是我上次調試的方式。 – clockworkgeek

回答

15

難道問題是總的對象屬於地址對象,而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; 
} 

你也必須做的獲取方法,以及類似的東西...

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功能可能是更漂亮,但希望你的想法呢。

試試看看您的總計是否在前端和管理區域正確合計。

+0

不能相信什麼****謝謝,不會發現它在一百萬年:) – Relja

+0

嗨,我添加了'if($ address-> getAddressType()=='shipping'){',總計在結帳頁面中是正確的。但完成訂單後錯誤,看來我的自定義總數不算。任何人都知道什麼是錯的? – Wakanina

+0

@Wakanina我自己從來沒有像前一樣將前臺總共作爲後端,我需要將總數轉換爲後端的附加產品,我在「checkout_type_onepage_save_order」事件中對觀察員進行了收集總量的操作,並將此值的產品添加到訂單中。這個博客看起來像是在正確的軌道上:http://www.classyllama.com/blog/unravelling-magentos-collecttotals-orders-and-caveats –

0

是否有可能在購物車塊中添加自己的佈局xml代碼?如果是的話,塊被調用兩次的機會很大(一個來自基本代碼,另一個來自代碼 - 即使你只是擴展它),從而重複價格總額。如果是這種情況,您需要刪除(破壞性地使用<remove>標籤)該塊的基本佈局xml,然後應該落實到位並開始工作。

+0

不幸的是,事實並非如此。我的佈局中有兩個購物車塊 - 一個用於常規購物車頁面,另一個用於購物車/側邊欄。但我試圖禁用所有我自己的佈局,並使用基本佈局,問題仍然存在。 是否有方法可以查看我的收集方法被調用的方法/塊? 感謝您的回答。 – Relja

1

周圍搜索後,這裏是另一種解決方案

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->getAddressType() == 'shipping') {