How to get the tax amount in the thank you page in magento

How to get the tax amount in the thank you page in magento

September 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get the tax amount in the thank you page in magento

Retrieving Tax Amount on Magento Thank You Page

When trying to retrieve the tax amount on the final thank you page in Magento after a customer clicks “Place Order”, you might encounter challenges in extracting the pure subtotal or calculating the tax separately. This post will guide you through two common approaches and their limitations based on my experience as a senior developer.

Approach 1: Using Object Manager

The first method involves using the object manager to retrieve the totals collection from the checkout session:

<?php
$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();
if (isset($totals['tax']) && $totals['tax']->getValue()) {
    $taxAmount = round($totals['tax']->getValue());
} else {
    $taxAmount = 0;
}
echo "Tax Amount: $" . $taxAmount;
?>

While this seems straightforward, it has a critical flaw. The tax object in the totals collection includes both tax amount and tax calculation type (fixed or percentage). Therefore, the value you get is not just the plain tax amount but also includes metadata. This makes it difficult to extract a clean tax figure.

Approach 2: Calculating Tax Manually

The second approach involves retrieving all items in the cart, calculating subtotal manually, and then calculating tax based on store configuration:

<?php
$quote = Mage::getSingleton('checkout/session')->getQuote();
$subtotal = 0;
foreach ($quote->getAllItems() as $item) {
    $subtotal += $item->getRowTotal();
}

$taxConfigModel = Mage::getSingleton('tax/config');
$request = new Varien_Object();
$request->setTotalAmount($subtotal);
$request->setBaseTotalAmount($subtotal);

if ($quote->isVirtual()) {
    $request->setTotalAmountMixed(0);
} else {
    $request->addTaxableAmount(0, 0);
}

try {
    $tax = $taxConfigModel->getRates($request)->getCalculatedTax();
    echo "Tax Amount: $" . round($tax);
} catch (Exception $e) {
    Mage::logException($e);
}
?>

This approach gives you more control over the tax calculation process. However, it is significantly more complex and may require extensive testing to ensure accurate results across different store configurations.

Best Practices

  1. Use Magento’s Built-in Functions: Whenever possible, use Magento’s built-in functions like Mage::getSingleton('tax/config')->getTaxAmount($order) for retrieving tax amounts associated with an order.

  2. Cache Your Tax Calculation: If your site has a high volume of orders, consider caching the tax calculation results to improve performance.

  3. Test Different Scenarios: Ensure you test your tax retrieval method across various product types, discounts, and shipping methods to ensure accuracy in all cases.

Conclusion

Retrieving the tax amount on the Magento thank you page requires a deep understanding of how taxes are calculated and stored within the Magento system. While both approaches described above can be used as starting points, they have significant limitations that may affect your implementation’s accuracy and maintainability.

For a more robust and secure solution, consider using Magento’s built-in tax calculation methods provided by the tax/config model or the Mage_Sales_Model_Quote_Tax class. These Magento core classes are designed to handle complex tax calculations and ensure compliance with sales tax regulations.

Remember, any custom tax calculation should be thoroughly tested in a staging environment before deploying it to your live production site to avoid unexpected errors or discrepancies in customer orders.

For more advanced customizations in Magento, I recommend checking out LaravelCompany’s Magento Development services, where our team of experienced developers can guide you through complex tax calculations and ensure a smooth checkout process for your customers.

Checkout Configuration Orders Customers

Our network