Get SubTotal for each tax value in Magento

Get SubTotal for each tax value in Magento

October 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get SubTotal for each tax value in Magento

Solving Magento Tax Category Subtotal Calculation

Hello! In this post, we’ll tackle an issue many Magento developers face - calculating subtotals for specific tax categories within a single order. Daniel has encountered this problem while setting up affiliate tracking codes based on product tax classes.

Understanding the Issue

Daniel wants to display separate subtotals for “Food” and “Non_Food” tax categories in his orders. He’s using getSubtotal('TaxCategoryCode'), which should return the total amount of items belonging to that specific tax category.

However, the provided code snippet doesn’t work as expected:

<?php
    //Get Order Number &amp; 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);
?>

The key problem lies in the tax category parameter passed to getSubtotal(). This method requires a tax class ID, not a category name.

Resolving the Subtotal Calculation

To fix this, we need to map the tax category names to their corresponding IDs. Here’s an updated snippet:

<?php
    // Get Order Number &amp; Order Total
    $order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
    $grandamount = number_format($order->getGrandTotal(), 2);
    $coupon = $order->getCouponCode();

    // Get Food Tax Category ID
    $foodTaxClass = Mage::helper('tax')->getTaxClassByName('Food');
    $amountfood = number_format($order->getSubtotal($foodTaxClass), 2);

    // Get Non-Food Tax Category ID
    $nonFoodTaxClass = Mage::helper('tax')->getTaxClassByName('Non_Food');
    $amountnonfood = number_format($order->getSubtotal($nonFoodTaxClass), 2);

    $discount = number_format(0.00 - $order->getDiscountAmount(), 2);
?>

Explanation and Best Practices

  • We use Mage::helper('tax')->getTaxClassByName('TaxCategoryName') to retrieve the tax class ID corresponding to a given category name.
  • Passing this ID as an argument to getSubtotal() correctly calculates the subtotal for that specific tax category within the order.

Conclusion

In summary, retrieving subtotals based on Magento’s tax categories requires mapping the category names to their respective IDs. By utilizing Mage::helper('tax')->getTaxClassByName(), we can accurately calculate and display separate subtotals for different product groups within a single order.

Remember, it’s crucial to keep your code clean and readable by using helper functions and avoiding hardcoded values whenever possible.

I hope this solution helps you, Daniel! If you have any further questions or need more assistance with Magento development, feel free to reach out. You can also explore our Magento Development Services for professional help with your e-commerce projects.

Happy coding!

Checkout Catalog Configuration Orders

Our network