1

I want to calculate the base price of all products in an order. The price for each (1 quantity) should include any discount/bonuses but NOT tax. The total of all product prices times their quantity + tax should be exactly the same as $order->getGrandTotal().

I’ve managed to get the individual prices including shipping fee with only a small precision error. Of course, this is not acceptable when dealing with currency. Also I’ve not taken into account bundled products and such.

So I ask you to help me, I need to make the exact same calculation as Magento do, and still get out the values that I need (each product/shipping with discounts but without tax).

Thanks in advance

3
  • What is wrong with $order->getSubtotal() + $order->getShippingAmount()? getSubtotal() returns sum of all items in order without tax. If you want include price with tax you can use $order->getSubtotalWithTax() Dec 12, 2010 at 18:32
  • I need to get the price for each individual item (calculated as if there was only 1 quantity) as well. Price + discount - (minus) tax. Dec 12, 2010 at 19:56
  • So I'm currently doing foreach($order->getAllVisibleItems() as $item) { /* calculate with variables on the $item object such as getPrice() and getDiscountAmount() */ } Dec 15, 2010 at 9:01

1 Answer 1

5

I've done this, and it gives me exactly the information I need. But I'm not sure if this is the right way to do it. Also my $total seem to be different from $grand_total with a few decimals such as 0.005 or similar sometimes.

$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;
1
  • This worked for me with a little adaptation which I believe depends on your configuration / tax configuration. I found the count($children) section was not required and the discount_amount needed to multipled by the tax_amount to get the correct figure. Thank you "Troubled Magento user"
    – Flipmedia
    Sep 19, 2012 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.