Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Tax calculator

Describes TaxCalculator, which calculates a line item's tax value and tax amount, based on tax value in Optimizely Commerce Connect.

Sales tax

  • For a single line item – Calculates sales tax based on the market, the shipping address, and the base price.
    public Money CalculateSalesTax(ILineItem lineItem, IMarket market, IOrderAddress shippingAddress, Money basePrice, ITaxCalculator taxCalculator) {
      return taxCalculator.GetSalesTax(lineItem, market, shippingAddress, basePrice);
    }
  • For a collection of line items - Calculates sales tax based on the market, the shipping address, and the currency.
    public Money CalculateSalesTax(IEnumerable<ILineItem> lineItems, IMarket market, IOrderAddress shippingAddress, Currency currency, ITaxCalculator taxCalculator) {
      return taxCalculator.GetSalesTax(lineItems, market, shippingAddress, currency);
    }

Change the default calculation

Inherit from DefaultTaxCalculator, the default implementation of ITaxCalculator, and override one or more of its methods to change the calculation behavior. For example, override GetTaxValues to change how tax values are looked up, or GetTaxCategoryNameById to change how a tax category name is resolved from its ID.

public class TaxCalculatorOverridingDefault: DefaultTaxCalculator {
  public TaxCalculatorOverridingDefault(IContentRepository contentRepository, ReferenceConverter referenceConverter): base(contentRepository, referenceConverter) {}
  protected override string GetTaxCategoryNameById(int taxCategoryId) {
    return string.Empty;
  }
  protected override IEnumerable<ITaxValue> GetTaxValues(string taxCategory, string languageCode, IOrderAddress orderAddress) {
    return new List<ITaxValue>() {
      new TaxValue(10, "SalesTax", "clothing", TaxType.SalesTax)
    };
  }
}