Remove taxes from shopping cart items and recalculate all totals

Chris Paynter asked on September 18, 2014 09:04

Hi. I am using Kentico 8.

I have the ShoppingCartInfo object in a method that takes it, converts it into an order and then processes payment. My main currency is AUD and the prices are calculated (including taxes) already by the time I receive the shopping cart.

The ShoppingCartItemInfo items in the shopping cart all have a TaxClass attached to them.

If the purchaser is not from Australia, before I process the payment, I need to remove all the taxes from each item and recalculate the totals . Here is my first attempt, that does not work. I'm new to the ecommerce in Kentico.

public static void RemoveGstFromShoppingCartIfOrderNotFromAustralia(string billingCountry, ShoppingCartInfo shoppingCart)
        {
            var billingCountryInfo = CountryInfoProvider.GetCountryInfo(billingCountry);
            var australia = CountryInfoProvider.GetCountryInfo("australia");

            if (billingCountryInfo.CountryID != australia.CountryID)
            {
                foreach (var item in shoppingCart.CartItems)
                {
                    item.TaxesTable.Clear();
                }
            }

            ShoppingCartInfoProvider.EvaluateShoppingCart(shoppingCart);
        }

The ultimate goal is that if the purchase is not from Australia, then the order is generated and the user is charged as though the tax was never there. Can anyone point out the best way to do this?

Thanks a lot everyone.

Chris

Correct Answer

Jakub Oravec answered on September 18, 2014 14:29

Hi Chris.

The reason is that EvaluateShoppingCart method in your code populates item.TaxesTable again.

If the goal is to calculate taxes only for customers having billing address in Australia, you don't need customization. Simply go to Store configuration application -> Store settings -> General and make sure that Apply taxes based on is set to Billing address. Then switch to Tax classes tab, edit your tax class and make sure that only value for Australia is entered. The system will now calculate taxes only for customers with billing address in Australia.

However, if you really need to do some magic with tax calculation, you can customize TaxClassInfoProvider.GetTaxesInternal method. It takes cart item and returns taxes for that item. Cart item has a reference to shopping cart with addresses. You can generate taxes based on your custom logic here.

See https://docs.kentico.com/display/K81/Customizing+providers for more information on customizing providers.

Jakub Oravec

0 votesVote for this answer Unmark Correct answer

Recent Answers


PB S answered on October 6, 2014 20:56

Hi Jakub

My requirement is to do a similar stuff I would need to apply sales tax for each shopping cart item I am not configuring this state tax in Kentico but getting it from external web service when i pass the state code.

Say the NJ tax percent returned from web service is 6.00% How can i add it to the shopping cart . do you have any sample code.

0 votesVote for this answer Mark as a Correct answer

Jakub Oravec answered on October 7, 2014 08:29

Hi,

I think that customization of TaxClassInfoProvider.GetTaxesInternal(ShoppingCartItemInfo item) I've mentioned above should work for you. It returns list of taxes for given item. You can contact your web service, get tax for NJ, create empty list of IItemTax with one ItemTax object representing tax obtained from web service.

Code example for illustration (null-checks and error handling missing):

var address = item.ShoppingCart.ShoppingCartBillingAddress; // Or ShoppingCartShippingAddress
var taxValue = GetTaxFromWebService(address.AddressCity, address.AddressZip, ...);

var result = new List<IItemTax>
             {
                 new ItemTax()
                 {
                     ItemTaxID = "0",
                     ItemTaxDisplayName = "My Tax",
                     ItemTaxValue = taxValue,
                  }
              };

return result;

Also, some kind of caching should be considered not to contact web service too often - have a look at CacheHelper class.

0 votesVote for this answer Mark as a Correct answer

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