問題描述
自定義 Mage_Sales_Model_Quote_Address_Total_Abstract 計算兩次:帳單和送貨地址 (Custom Mage_Sales_Model_Quote_Address_Total_Abstract calculate twice: billing&shipping address)
I intended to make a custom credit module which can use our store's credits for discount. After looking into some examples, I successfully added a checkout step to the onepage checkout. And then I extends Mage_Sales_Model_Quote_Address_Total_Abstract to have a custom collector to calculate total. Currently I hard coded some discount value to see how it works:
public function collect(Mage_Sales_Model_Quote_Address $address) {
parent::collect ( $address );
//if($address‑>getData('address_type')=='billing') return $this;
try {
$this‑>_setAmount ( ‑10 )‑>_setBaseAmount ( ‑10 );
} catch ( Exception $e ) {
Mage::throwException ( $e‑>getMessage () );
//do nothing.
}
return $this;
}
public function fetch(Mage_Sales_Model_Quote_Address $address) {
parent::fetch ( $address );
//if($address‑>getData('address_type')=='billing') return $this;
$title = Mage::helper ( 'sales' )‑>__ ( 'Credittest' );
$address‑>addTotal ( array ('code' => $this‑>getCode (), 'title' => $title, 'value' => ‑10 ) );
return $this;
}
The section in config.xml looks like this:
<sales>
<quote>
<totals>
<credittest>
<class>sales/quote_address_total_credit</class>
<after>tax_subtotal,subtotal,freeshipping</after>
<before>grand_total</before>
</credittest>
</totals>
</quote>
</sales>
However the result comes out with ‑20 deducted. after some debug tracing, my custom collector are called twice, once the address type is "billing" and another is "shipping". So I added the commented code above to calculate only when shipping address comes in. But i'm not sure if this is the right way to go.
Why other class in Mage_Sales_Model_Quote_Address won't calculate twice? They are actually invoked twice according to my tracing. And what's right way solve my issue?
Thank you in advance.
‑‑‑‑‑
參考解法
方法 1:
Take a look @ Magento Add Fee or Discount to Order Totals
(by HooYao、MagePal Extensions)