I am using Commerce version 12.2, this is wrt Tax Calculation. We have our custom tax cacluator which was using ITaxCalculator previously, but now it implements IOrderGroupCalculator. We are able to calculate the tax correctly, but now I see that the total does not add the tax which was not the case previously.
I wrote the below code to overcome this issue:
var TotalAmount = ServiceLocator.Current.GetInstance().GetTotal(Cart).Amount + ServiceLocator.Current.GetInstance().GetTaxTotal(Cart).Amount;
Also, wouldnt it call both GetTotal and GetTaxTotal everytime even if there is no change (which is an unwanted back and forth)
Is there a better way to implement this?
I guess you just want to get the total of order with tax, then you only need to use IOrderGroupCalculator.GetTotal(Cart). This method will includes GetTaxTotal(Cart) as well, and it doesn't calculate the tax if there's no change.
That means your code should be:
var TotalAmount = ServiceLocator.Current.GetInstance<IOrderGroupCalculator>().GetTotal(Cart).Amount
I just want to add up more information. In Commerce 12, we've introduced a new setting on IMarket, PricesIncludeTax, indicating whether the prices of the market includes tax or not. In the order we have that setting as well, got from the market when order was created. So if it is true, it means the prices (item prices, shipping costs,..) already include tax, therefore the tax total got from the tax calculator was not added up to the order total, since the order already includes tax. So please check that setting as well.
var TotalAmount = ServiceLocator.Current.GetInstance().GetTotal(Cart).Amount
@Tuan: I actually tried that first but it doesnt return me the correct value (cart total + tax), hence I manually added the tax myself. Also, I can see that in the Orderform table in SQL the TaxTotal column is 0.00 for all orders.
Hi @Bien, the PricesIncludeTax is false for the market. Below is the code:Submit
bool taxIncluded = ServiceLocator.Current.GetInstance<ICurrentMarket>().GetCurrentMarket().PricesIncludeTax;
You said that your custom tax calculator was using ITaxCalculator but now it implements IOrderGroupCaclculator. Could you show us your custome codes? Not need the detail implementation but I want to know what methods that you customized, so that I could have a better guess.
@Bein Nguyen: Below is the code snippet for your reference:
public class CustomTaxCalculator : DefaultOrderGroupCalculator, IOrderGroupCalculator
public readonly IOrderFormCalculator _orderFormCalculator;
public readonly IReturnOrderFormCalculator _returnOrderFormCalculator;
public readonly IMarketService _marketservice;
public CustomTaxCalculator(IOrderFormCalculator orderFormCalculator, IReturnOrderFormCalculator returnOrderFormCalculator, IMarketService marketservice)
: base(orderFormCalculator, returnOrderFormCalculator, marketservice)
this._orderFormCalculator = orderFormCalculator;
this._returnOrderFormCalculator = returnOrderFormCalculator;
this._marketservice = marketservice;
public new Money GetTaxTotal(IOrderGroup orderGroup)
public new Money GetTotal(IOrderGroup orderGroup)
Money total = this.CalculateTotal(orderGroup);
protected override Money CalculateTaxTotal(IOrderGroup orderGroup)
decimal taxTotal = 0.0M;
var orderGroupObj = orderGroup as OrderGroup;
if (orderGroupObj == null)
return new Money(0, Currency.USD);
orderGroupObj.TaxTotal = 0;
if (orderGroup.GetFirstForm().Shipments.Count > 0)
foreach (IShipment shipment in orderGroup.GetFirstForm().Shipments)
IOrderAddress address = shipment.ShippingAddress;
if (address != null)
taxTotal = CalculateTaxUsingCustomService(orderGroup);
return new Money(taxTotal, Currency.USD);
protected override Money CalculateTotal(IOrderGroup orderGroup)
Currency currency = orderGroup.Currency;
Money money = this.GetSubTotal(orderGroup) + this.GetHandlingTotal(orderGroup) + this.GetShippingSubTotal(orderGroup) - this.GetOrderDiscountTotal(orderGroup) - orderGroup.GetShippingDiscountTotal();
money += this.GetTaxTotal(orderGroup);
Also, now is see the tax total being computed, but it is not getting stored in the database. For example: The tax total is being displayed on the UI, but the TaxTotal columns in the database is always 0.00 (OrderForm, OrderGroup).
I would suggest you to override the following method as well, and update the tax total and order total values with your custom tax calculation:
public new OrderGroupTotals GetOrderGroupTotals(IOrderGroup orderGroup)
// your custom code here...
@Bien: That did the trick for me, thank you!