Ok... after going about it all wrong for two days, I finally figured out how to do this.
All you have to do it figure out what the discount percent is in relation to the order total (i.e. 20%, 30%) and multiply that by the tax total.
This fix requires minor changes to TaxManager.cs, ShoppingCartManager.cs, OrderTotals.ascx.cs, and OrderManager.cs.
The first thing you need to do is add the order subtotal to the GetTaxTotal method in TaxManager (around line 180).
public static decimal GetTaxTotal(ShoppingCart cart, int paymentMethodId, Customer customer, decimal OrderSubTotal, ref String error)
{
...
}
I use "OrderSubTotal", but you can use whatever you like.
This change will require fixing the broken method calls in other pages:
OrderManger.cs line 3095
paymentInfo.PaymentMethodId, customer, orderSubTotalExclTax, ref taxerror);
OrderTotals.ascx.cs line 143
decimal shoppingCartTaxBase = TaxManager.GetTaxTotal(cart, paymentMethodId, NopContext.Current.User, subtotalBaseWithoutPromo, ref TaxError);
ShoppingCartManager.cs line 342
decimal shoppingCartTax = TaxManager.GetTotalTax(cart, paymentMentodId, customer, subtotalBaseWithoutPromo, ref taxerror);
Next we go back to TaxManager and add this to the using/imports section:
using NopSolutions.NopCommerce.BusinessLogic.Promo.Discounts;
For the last step we go down to the end of GetTaxTotal (around line 276) and find the lines:
taxTotal = itemsTaxTotal + checkoutAttributesTax + shippingTax + paymentMethodAdditionalFeeTax;
taxTotal = Math.Round(taxTotal, 2);
return taxTotal;
and add a few lines in between them like this:
taxTotal = itemsTaxTotal + checkoutAttributesTax + shippingTax + paymentMethodAdditionalFeeTax;
Discount orderDiscount = null;
Decimal appliedDiscount = ShoppingCartManager.GetOrderDiscount(NopContext.Current.User, OrderSubTotal, out orderDiscount);
taxTotal = taxTotal - (taxTotal * (appliedDiscount / OrderSubTotal));
taxTotal = Math.Round(taxTotal, 2);
return taxTotal;
I'm using 1.70 by the way.
This fix presupposes that no matter what the total or tax or discount amount is, the discount CAN be broken down into a percent of the total. This discount percent is then subtracted from the tax total leaving you with the proper tax.
I know of one alternative way to get OrderSubTotal without having to add it to the GetTaxTotal method, but it requires totalling up the order myself inside GetTaxTotal and I didn't like that way. Does anyone know of a way to get the order subtotal without having to import it in using GetTaxTotal? I'd be happy to know that... otherwise this should work just fine.
I have it working correctly with a discount on product variants in a cart that has a discount code applied to it with two different taxes on two different items as well as and item that has no tax on it. And to the best of my knowledge, when I figure out what the tax should be with a calulator, it matches up.
Any problems with this, let me know.