2010-12-12 3 views
1

주문한 모든 제품의 기본 가격을 계산하고 싶습니다. 각 (1 수량)의 가격에는 할인/보너스가 포함되어야하지만 세금은 포함되지 않아야합니다. 모든 제품 가격의 합계 + 수량 + 세금은 $ order-> getGrandTotal()과 정확히 같아야합니다.Magento의 주문으로 각 제품의 가격을 계산하십시오.

적은 정밀도 오류로 배송비를 포함한 개별 가격을받을 수있었습니다. 물론, 이는 통화를 취급 할 때 허용되지 않습니다. 또한 번들 제품과 같은 것을 고려하지 않았습니다.

Magento와 똑같은 계산을하고 필요로하는 가치 (각 제품/할인은 있지만 세금 제외)는 나와야합니다.

미리 감사

+0

'$ order-> getSubtotal()'+'$ order-> getShippingAmount()'의 문제점은 무엇입니까? 'getSubtotal()'은 세금없이 모든 항목의 합계를 반환합니다. 세금 포함 가격을 원한다면'$ order-> getSubtotalWithTax()'를 사용할 수 있습니다. –

+0

각 항목의 가격을 알아야합니다 (수량이 하나 인 것처럼 계산). 가격 + 할인 - (마이너스) 세금. –

+0

그래서 저는 현재 foreach ($ order-> getAllVisibleItems()를 $ item로 사용하고 있습니다) {/ * getPrice() 및 getDiscountAmount() * /}와 같은 $ item 객체의 변수를 계산합니다. –

답변

4

내가이 짓을했는지, 그리고 내게 필요한 정확한 정보를 제공합니다. 그러나 이것이 올바른 방법인지 확실하지 않습니다. 또한 $ $ total은 $ grand_total과 약간의 차이가 있습니다.

$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

관련 문제