Magento cart: how to calculate tax when applying a discount?

Magento cart: how to calculate tax when applying a discount?

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento cart: how to calculate tax when applying a discount?

Magento Cart: Calculating Tax When Applying a Discount

Introduction

In this post, we’ll address a common challenge faced by many Magento developers - calculating taxes correctly when applying discounts to the cart. We’ll dive into a specific scenario where you want to give a fixed discount (e.g., 50 EUR) to every x-th visitor, with the discount applied only to items and not shipping.

Understanding Your Current Approach

You’ve already implemented some clever logic for this task:

  1. You’re calculating the grand total of all items in the quote.
  2. You’re applying the discount based on a ratio between the item’s row total and the overall discountable amount.
  3. You’re manually adjusting the totals for both shipping addresses and the main quote.

While your approach shows a solid understanding of Magento’s architecture, there are some issues that need to be addressed.

The Problem: Incorrect Tax Calculation

The primary issue is that after applying the discount, the tax calculation doesn’t consider the discounted prices. Instead, it continues to base the tax on the original, pre-discounted values. This leads to an incorrect display of the tax amount in the cart summary and checkout.

The Root Cause: Magento’s Discount Calculation Logic

To understand why this happens, let’s briefly look at how Magento calculates taxes:

  1. When a discount is applied, Magento first applies it to the item prices after removing any previously applied discounts.
  2. If the “apply customer tax” setting is set to “after discount,” as in your case, Magento calculates taxes based on the pre-discounted prices.

The Solution: Recalculating Taxes After Applying Discounts

To fix this issue, you need to ensure that Magento recalculates the taxes after applying your custom discount. Here’s a step-by-step solution:

  1. Collect Totals and Save Quotes: After applying your discount, explicitly call $quote->collectTotals() on the main quote object. This forces Magento to recalculate all totals based on the current state of the items.

  2. Clear Previous Tax Amounts: Before collecting totals, you may want to clear any previously calculated tax amounts by setting them to 0:

    $quote->setTaxAmount(0);
    $quote->setBaseTaxAmount(0);
    
  3. Save the Quote Again: After collecting totals and recalculating taxes, save the quote one more time:

    $quote->save();
    

The Complete Code Snippet

Here’s how your code should look after incorporating these changes:

// Apply discount logic...

$grandTotal = 0;
foreach ($items as $item) {
    $grandTotal += $item->getRowTotal();
}
$discount = 50;

// ... (rest of the discount application logic)

$quote->setSubtotal($quote->getSubtotal() + $address->getSubtotal());
$quote->setSubtotalWithDiscount($quote->getSubtotal() - $discount);
$quote->setGrandTotal($quote->getGrandTotal() + $address->getGrandTotal());

// Clear previous tax amounts
$quote->setTaxAmount(0);
$quote->setBaseTaxAmount(0);

// Collect totals to recalculate taxes
$quote->collectTotals();

// Save the quote with updated totals and tax calculations
$quote->save();

Conclusion

In this post, we’ve explored a common Magento scenario - calculating taxes correctly when applying discounts. By understanding how Magento’s discount calculation logic works and following our recommended steps, you can ensure that your custom discounts are applied correctly and that taxes are calculated based on the discounted item prices.

Remember to always test your code thoroughly after making such changes. If you’re looking for more advanced Magento development tips and best practices, feel free to explore our Magento Development Guide.

Happy coding!

Checkout Database Pricing Errors

Our network