2011-09-16 72 views
1

我想要獲取購物車物品對象並將其存儲在新變量中,循環遍歷它們,更改它們的價格,然後我想重新計算新總計的稅額。Magento:如何輕鬆地重新計算給定總計稅額

如何以儘可能少的步驟實現這一目標。

這是我做的代碼,但稅收理解不被重新計算爲我沒能做到這一點:

  $items=$address ;// add is of type Mage_Sales_Model_Quote_Address $address 

      foreach ($items->getQuote()->getAllVisibleItems() as $item){ 
       if ($items->getParentItem()) { 
        continue; 
       } 
       try{ 
        if ($item){ 
         $qty  = $item->getTotalQty(); 
         $total  = $item->getCalculationPrice()*$qty*2; 
         $baseTotal = $item->getBaseCalculationPrice()*$qty*2; 

         $item->setRowTotal(Mage::app()->getStore()->roundPrice($total)); 
         $item->setBaseRowTotal(Mage::app()->getStore()->roundPrice($baseTotal)); 
         $item->save(); 
        } 
       }catch (Exception $e){ 

       } 

      } 

      $baseTax = ($items->getTaxBeforeDiscount() ? $items->getTaxBeforeDiscount() : ($items->getTaxAmount() ? $items->getTaxAmount() : 0)); 
      $tax = ($items->getBaseTaxBeforeDiscount() ? $items->getBaseTaxBeforeDiscount() : ($items->getBaseTaxAmount() ? $items->getBaseTaxAmount() : 0)); 
+0

你可以試試這個對象調用collectTotals() –

+0

感謝,但沒有不幸的工作,可能是因爲我在做這個調用的函數裏面取擴展quote_address_total –

回答

0

你可以計算出稅/增值稅率,使用地址稅額和累計。更新地址之後(總計使用稅率計算新稅額)。

看看下面的例子

/* calculate ratio */ 
$ratio = ($address->getTaxAmount()/$address->getGrandTotal()); 
$baseRatio = ($address->getBaseTaxAmount()/$address->getBaseGrandTotal()); 

/* do price changes */ 
$address->setGrandTotal(...); 
$address->setBaseGrandTotal(...); 
/* ... */ 

/* set new tax amount */ 
$address->setTaxAmount($address->getGrandTotal() * $ratio); 
$address->setBaseTaxAmount($address->getBaseGrandTotal() * $baseRatio); 

$address->save();