Please help me how can we override kentico out of box Tax calculation API i checkout process

Kuntesh Thakker asked on June 8, 2016 15:58

I want to override Kentico custom tax calculations with Real time taxation . Could please anyone help me for the steps needed

Correct Answer

Brenden Kehren answered on June 8, 2016 21:57

To add to the link Virgil shared, here's what I've done before. The var tax = "0.90"; is an example. Where that value is, is where you'd call your custom real-time method to calculate tax for that line item.

using CMS;
using CMS.Ecommerce;
using CMS.Helpers;
using System;
using System.Data;


[assembly: RegisterCustomProvider(typeof(CustomTaxInfoProvider))]
/// <summary>
/// Summary description for CustomTaxClassInfoProider 
/// </summary>
public class CustomTaxInfoProvider : TaxClassInfoProvider
{
    protected override DataSet GetTaxesInternal(ShoppingCartInfo cart)
    {
        // Create an empty taxes table
        DataTable table = GetNewTaxesTable();
        DataSet ds = new DataSet();

        if (cart.ShoppingCartBillingAddress != null)
        {
            foreach (ShoppingCartItemInfo item in cart.CartItems)
            {
                // your custom call to get tax info
                var tax = "0.90";

                AddTaxRow(table, item.SKUID, String.Format("Ship to {0}", cart.ShoppingCartBillingAddress.GetStateCode()), ValidationHelper.GetDouble(tax, 0.0));
            }
        }

        ds.Tables.Add(table);
        return ds;
    }

    #region "Private methods"

    /// <summary>
    /// Creates an empty taxes table.
    /// </summary>    
    private DataTable GetNewTaxesTable()
    {
        DataTable table = new DataTable();

        // Add required columns
        table.Columns.Add("SKUID", typeof(int));
        table.Columns.Add("TaxClassDisplayName", typeof(string));
        table.Columns.Add("TaxValue", typeof(double));
        table.Columns.Add("TaxIsFlat", typeof(bool));
        return table;
    }


    /// <summary>
    /// Creates tax row which holds the data of the tax which should be applied to the given SKU.
    /// </summary>
    /// <param name="taxTable">Tax table the row should be added to.</param>
    /// <param name="skuId">SKU ID</param>
    /// <param name="taxName">Tax name</param>
    /// <param name="taxValue">Tax value</param>
    private void AddTaxRow(DataTable taxTable, int skuId, string taxName, double taxValue)
    {
        DataRow row = taxTable.NewRow();

        // Set required columns
        row["SKUID"] = skuId;
        row["TaxClassDisplayName"] = taxName;
        row["TaxValue"] = taxValue;
        row["TaxIsFlat"] = true;
        taxTable.Rows.Add(row);
    }

    #endregion
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Virgil Carroll answered on June 8, 2016 18:23

You need to reflect on the Ecommerce and EcommerceProvider dlls. There will be a tax class and functions to work with the tax. It is usually the best practice to override the internal function. I am not in front of my computer so cannot share the specific info, but check out this StackOverflow thread for help http://stackoverflow.com/questions/23023318/extending-shoppingcartinfo-object-to-add-a-new-tax-class-in-kentico-cms

0 votesVote for this answer Mark as a Correct answer

Kuntesh Thakker answered on June 10, 2016 08:00

Thank you both for your reply . It works well for me

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.