Hi,
the checkout process could be modified in more ways, usually it involves changing some checkout step's ascx control (
E-commerce guide article).
In this case however, we need further integration into system, so that the taxes could appear in order and invoice and also during checkout.
I'm very sorry for the delay with bringing this solution.
Below please find sample code of involved methods and all needed changes.
Important notes:
The code works fine in my setup, however it is not thoroughly tested yet.
It doesn't count with subjects freed from paying taxes (supplied Tax ID), but this can be done quite easily.
It also doesn't count with flat fees (absolute tax value), but I suppose this is never used...
For the future usage, some optimalizations should be done.here it goes:
1. Calculate tax in CalculateShipping and
2. Store tax in Hashtable steps are in the
CalculateShipping method
(the code is in CustomShippingOptionInfoProvider.cs from the custom ecommerce providers project.)
...
public double CalculateShipping(object cartObj, string siteName)
{
CMS.Ecommerce.ShoppingCartInfo cart = cartObj as CMS.Ecommerce.ShoppingCartInfo;
if (cart != null)
{
// Get shipping address details
CMS.Ecommerce.AddressInfo address = CMS.Ecommerce.AddressInfoProvider.GetAddressInfo(cart.ShoppingCartShippingAddressID);
if (address != null)
{
// Get shipping address country and state
CountryInfo country = CountryInfoProvider.GetCountryInfo(address.AddressCountryID);
StateInfo state = StateInfoProvider.GetStateInfo(address.AddressStateID);
// Add tax to standard shipping price for countries/states with specified "ShippingTax"
CMS.Ecommerce.TaxClassInfo shippingTaxClassInfo = TaxClassInfoProvider.GetTaxClassInfo("ShippingTax");
if (shippingTaxClassInfo != null)
{
// tax will be zero if no value found for state/country
double shippingTax = 0;
if (state != null)
{
CMS.Ecommerce.TaxClassStateInfo tcsi = CMS.Ecommerce.TaxClassStateInfoProvider.GetTaxClassStateInfo(shippingTaxClassInfo.TaxClassID, state.StateID);
if (tcsi != null)
{
shippingTax = tcsi.TaxValue;
}
}
else
{
CMS.Ecommerce.TaxClassCountryInfo tcci = CMS.Ecommerce.TaxClassCountryInfoProvider.GetTaxClassCountryInfo(country.CountryID, shippingTaxClassInfo.TaxClassID);
if (tcci != null)
{
shippingTax = tcci.TaxValue;
}
}
// Perform calculation of shipping charge for current shopping cart
double shippingWithoutTax = CMS.CMSEcommerce.ShippingOptionInfoProvider.CalculateShipping(cart, siteName);
// Calculate tax amount
double calculatedShippingTax = TaxClassInfoProvider.GetTaxValue(shippingWithoutTax, shippingTax, false);
// Store calculated tax amount into shop.cart custom data
cart.ShoppingCartCustomData["ShippingTax"] = calculatedShippingTax.ToString();
cart.UpdateShoppingCartCustomData();
// Return shipping charge including calculated tax
return shippingWithoutTax + calculatedShippingTax;
}
}
}
// Get standard shipping price in other cases
return CMS.CMSEcommerce.ShippingOptionInfoProvider.CalculateShipping(cart, siteName);
}
...
The next step -
3. Get tax into tax summary in EvaluateShoppingCartContent (in the CustomShoppingCartInfoProvider.cs from the custom ecommerce providers project)
...
public void EvaluateShoppingCartContent(object shoppingCart, bool evaluateForInvoice)
{
//ShoppingCartInfoProvider.EvaluateShoppingCartContent((CMS.Ecommerce.ShoppingCartInfo)shoppingCart, evaluateForInvoice);
CMS.Ecommerce.ShoppingCartInfo cart = shoppingCart as CMS.Ecommerce.ShoppingCartInfo;
if (cart != null)
{
ShoppingCartInfoProvider.EvaluateShoppingCartContent(cart, evaluateForInvoice);
CMS.Ecommerce.TaxClassInfo tci = TaxClassInfoProvider.GetTaxClassInfo("ShippingTax");
if (cart.ShoppingCartCustomData["ShippingTax"] != null)
{
DataRow dr = cart.ShoppingCartTaxTable.NewRow();
dr["TaxClassID"] = tci.TaxClassID;
dr["TaxClassDisplayName"] = tci.TaxClassDisplayName;
// Get the added tax value from cart custom data hashtable
double taxValue = double.Parse(cart.ShoppingCartCustomData["ShippingTax"]);
// Add the shipping tax summary into the tax table
CMS.Ecommerce.ShoppingCartInfo.AddTaxSummary(cart.ShoppingCartTaxTable, dr, taxValue);
}
}
}
...
Additional step: add call of ShoppingCartInfoProvider.EvaluateShoppingCartContent(ShoppingCartInfoObj) method before "return true;" command within "try" statement in ProcessStep() method in ShoppingCartPaymentShipping.ascx.cs file. (line approx. #170). It's located in ~\CMSModules\Ecommerce\Controls\ShoppingCart\ web project folder.
Finally, you will need to
add the tax class in CMSDesk / Tools / Ecommerce / Configuration / TaxClasses with codename "ShippingTax" (it must be the same string as in the code) and define values (in %) for the countries (states) where it is to be applied.
I suppose this will be turned to knowledgebase article soon after further verifications. Hopefully in the future this will be implemented into the e-comm module by default.
Thank you in advance for any further comments, suggestions and questions about this solution.
Regards,
Zdenek C.