2010-12-12 19 views
1

すべての製品の基本価格を注文したいと思っています。それぞれの価格(1つの数量)には、割引/ボーナスは含まれていますが、税は含まれていない必要があります。すべての商品価格の合計とその数量+税額は、$ order-> getGrandTotal()とまったく同じにする必要があります。Magentoで各製品の価格を計算する

私は、わずかな精度誤差で出荷料を含む個別の価格を得ることができました。もちろん、これは通貨を扱うときは受け入れられません。また、バンドルされた製品などを考慮していません。

私は、Magentoとまったく同じ計算をして、必要な値を引き出す必要があります(各製品/割引は税金を払っていません)。

事前のおかげ

+0

'$ order-> getSubtotal()' + '$ order-> getShippingAmount()'の何が問題になっていますか? 'getSubtotal()'は、税なしのすべてのアイテムの合計を順番に返します。税を含む価格を含める場合は、 '$ order-> getSubtotalWithTax()'を使用することができます –

+0

個々の商品の価格を取得する必要があります(1つの数量があるかのように計算されます)。価格+割引 - (マイナス)税。 –

+0

私は現在、foreach($ order-> getAllVisibleItems()を$ itemとして扱っています){/ * getPrice()やgetDiscountAmount()などの$ itemオブジェクトの変数を計算します* /} –

答えて

4

に私はこれをやった、それは私が必要とする正確な情報を提供します。しかし、これが正しいかどうかは分かりません。また、私の$ totalは$ grand_totalとは異なっているようですが、時々0.005などのような少数の小数があります。

$store = Mage::app()->getStore($order->getStoreId()); 

$customer = Mage::getModel('customer/customer') 
    ->load($order->getCustomerId()); 

$tax_calc = Mage::getSingleton('tax/calculation'); 

$tax_rate_req = $tax_calc->getRateRequest(
    $order->getShippingAddress(), 
    $order->getBillingAddress(), 
    $customer->getTaxClassId(), 
    $store); 

$args = array(); 
$total = 0; 

// Calculate price of each item in the order 
foreach($order->getAllVisibleItems() as $item) 
{ 
    $product = Mage::getModel('catalog/product') 
     ->load($item->getProductId()); 

    $children = $item->getChildrenItems(); 

    if(count($children) && ($product->getData('price_type') != 1)) 
    { 
     foreach($children as $child) 
     { 
      $product = Mage::getModel('catalog/product') 
       ->load($child->getProductId()); 

      /* If tax_percent is not set? 
      Mage::getSingleton('tax/calculation')->getRate(
       $tax_rate_req->setProductClassId($product->getTaxClassId())) 
      */ 
      $tax_mod = (float)$child->getData('tax_percent'); 
      $tax_mod /= 100; 

      $qty = (float)$child->getData('qty_ordered'); 

      $price = (float)$child->getData('row_total_incl_tax'); 
      $price -= (float)$child->getData('discount_amount'); 

      $base_price = (($price/(1 + $tax_mod))/$qty); 
      $base_price = $store->roundPrice($base_price); 

      $total += (($base_price * (1 + $tax_mod)) * $qty); 

      $args[] = array 
       (
        'name'   => $product->getData('name'), 
        'sku'   => $child->getData('sku'), 
        'tax_mod'  => $tax_mod, 
        'qty'   => $qty, 
        'price'   => $price, 
        'base_price' => $base_price 
       ); 
     } 
    } 
    else 
    { 
     /* If tax_percent is not set? 
     Mage::getSingleton('tax/calculation')->getRate(
      $tax_rate_req->setProductClassId($product->getTaxClassId())) 
     */ 
     $tax_mod = (float)$item->getData('tax_percent'); 
     $tax_mod /= 100; 

     $qty = (float)$item->getData('qty_ordered'); 

     $price = (float)$item->getData('row_total_incl_tax'); 
     $price -= (float)$item->getData('discount_amount'); 

     $base_price = (($price/(1 + $tax_mod))/$qty); 
     $base_price = $store->roundPrice($base_price); 

     $total += (($base_price * (1 + $tax_mod)) * $qty); 

     $args[] = array 
      (
       'name'   => $product->getData('name'), 
       'sku'   => $item->getData('sku'), 
       'tax_mod'  => $tax_mod, 
       'qty'   => $qty, 
       'price'   => $price, 
       'base_price' => $base_price 
      ); 
    } 
} 

// Calculate price for shipping 
if(($price = (float)$order->getData('shipping_incl_tax')) > 0) 
{ 
    $tax_mod = $tax_calc->getRate($tax_rate_req->setProductClassId(
     Mage::getStoreConfig('tax/classes/shipping_tax_class'))); 
    $tax_mod /= 100; 

    $price -= (float)$order->getData('shipping_discount_amount'); 

    $base_price = ($price/(1 + $tax_mod)); 

    $base_price = $store->roundPrice($base_price); 

    $total += ($base_price * (1 + $tax_mod)); 

    $args[] = array 
     (
      'name'   => $order->getData('shipping_description'), 
      'sku'   => $order->getData('shipping_method'), 
      'tax_mod'  => $tax_mod, 
      'qty'   => 1, 
      'price'   => $price, 
      'base_price' => $base_price 
     ); 
} 

$total = $store->roundPrice($total); 

echo('<pre>'); 
print_r($args); 
//print_r($order->getData()); 
echo('</pre>'); 

$grand_total = (float)$order->getData('grand_total'); 
//$grand_total = $store->roundPrice($grand_total); 

echo('<p><strong>My total</strong>: ' . $total . '</p>'); 
echo('<p><strong>Grand total</strong>: ' . $grand_total . '</p>'); 

exit; 
+0

これは、私が信じている少しの適応はあなたの設定/税の設定に依存します。私はcount($ children)セクションは必要ではなく、discount_amountはtax_amountを乗算して正しい数字を得る必要があることを発見しました。ありがとうございました "困ったMagentoユーザー" – Flipmedia

関連する問題