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
}