Get SubTotal for each tax value in Magento
I'm setting up a tracking code for an affiliate programs. Now we give different commissions. One for Food related products and one for Non-Food related products. These are also the tax classes we have (Food, Non Food).
I need to display the sub total for food products and a subtotal for non food products.
I use the following code but tha开发者_JAVA百科t doesn't work:
<?php
//Get Order Number & Order Total
$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$grandamount = number_format($order->getGrandTotal(),2);
$coupon = $order->getCouponCode();
$amountfood = number_format($order->getSubtotal('Food'), 2);
$amountnonfood = number_format($order->getSubtotal('Non_Food'), 2);
$discount = number_format(0.00 - $order->getDiscountAmount(), 2);
?>
If I use $amountfood = number_format($order->getSubtotal(), 2);
it does work for the subtotal including both the food and non food values.
Could someone please help me with that.
Thanks, Daniel
I don't think this information is available directly: magento stores the subtotal and taxes in a global way, only the total, no detailed information.
What you could do, is fetch the ordered products, for each one get his tax class and store in an array the sale value.
Something like this:
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$subtotals = array();
foreach ($items as $_item) {
if (array_key_exists($subtotals[$_item->getTaxClassId()])) {
$subtotals[$_item->getTaxClassId()] += $_item->getRowTotal();
} else {
$subtotals[$_item->getTaxClassId()] = $_item->getRowTotal();
}
}
not sure if the "if" is needed though.
hope that helps
精彩评论