Custom TaxClassInfoProvider

Delford Chaffin asked on September 11, 2014 20:09

I have copied nearly verbatim (aside from changing the class names) the sample code to create a custom TaxClassInfoProvider and is not working. Here is my code:

CMSECommerceModuleLoader.cs

using CMS.TreeEngine;
using CMS.SettingsProvider;
using CMS.DocumentEngine;
using CMS.EventLog;
using System.Text.RegularExpressions;
using System.Linq;
using System.Xml.XPath;
using HtmlAgilityPack;
using CMS.SiteProvider;
using CMS.CMSHelper;

[ECommerceModuleLoader]
public partial class CMSECommerceModuleLoader
{
    private class ECommerceModuleLoader : CMSLoaderAttribute
    {
        public ECommerceModuleLoader()
        {
            // Require E-commerce module to load properly
            RequiredModules = new string[] { ModuleEntry.ECOMMERCE };
        }

        public override void Init()
        {
            ClassHelper.OnGetCustomClass += GetCustomClass;
        }

        private static void GetCustomClass(object sender, ClassEventArgs e)
        {
            System.Web.HttpContext.Current.Response.Write("GetCustomClass");
            if (e.Object == null)
            {
                // Provide your custom classes
                switch (e.ClassName)
                {
                    case "FranchiseTaxClassInfoProvider":
                        e.Object = new FranchiseTaxClassInfoProvider();
                        break;
                }
            }
        }
    }
}

FranchiseTaxClassInfoProvider.cs

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;

using CMS.DataEngine;
using CMS.Ecommerce;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.GlobalHelper;
using CMS.CMSHelper;

public class FranchiseTaxClassInfoProvider : TaxClassInfoProvider
{
    protected override DataSet GetTaxesInternal(ShoppingCartInfo cart)
    {
        System.Web.HttpContext.Current.Response.Write("GetTaxesInternal");
        DataSet ds = new DataSet();

        // Create an empty taxes table
        DataTable table = GetNewTaxesTable(); 

        foreach (ShoppingCartItemInfo item in cart.CartItems)
        {           
            AddTaxRow(table, item.SKUID, "Franchise Tax", 10);
        }

        // Return built dataset with the taxes
        ds.Tables.Add(table);
        return ds;
    }

    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));

        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;

        taxTable.Rows.Add(row);
    }
}

As you can see, I'm still only testing and trying to add 10% to each item. What am I missing? Should this just be working now or is there something else I need to do? Thanks!

Correct Answer

Delford Chaffin answered on September 12, 2014 06:27

Got an answer from Kentico support. They fixed my loader class as follows:

[ECommerceModuleLoader]
public partial class CMSModuleLoader
{
    private class ECommerceModuleLoader : CMSLoaderAttribute
    {
        public ECommerceModuleLoader()
        {
            // Require E-commerce module to load properly
            RequiredModules = new string[] { ModuleEntry.ECOMMERCE };
        }

        public override void Init()
        {
            TaxClassInfoProvider.ProviderObject = new FranchiseTaxClassInfoProvider();
        }
    }
}

I had changed the outermost class name without understanding that wouldn't work. The whole point is to extend the CMSModuleLoader class. Seems obvious in retrospect.

Thanks for the help and hope this discussion helps someone else.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on September 11, 2014 20:12

Have you debugged and verified that it runs through your code and your override is occurring?

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on September 11, 2014 21:10

Thanks, but I'm not entirely sure how to do that. I have turned on the debug stuff in Kentico, but I'm new to this area and am not sure how to do what you asked.

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on September 11, 2014 21:22

I added a call to the database in my class because I could easily see if that was called and it doesn't look like it is, which I supposed means my override is not being called. What now?

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 11, 2014 21:26

I would suggest opening up the visual studio solution and running it locally, set breakpoints in your override, then if it never gets there, you will know that your override code is incorrect. Otherwise, if it does get into it, it may be in the logic itself.

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on September 11, 2014 21:43

Thanks, that is a little difficult to do where the site is hosted remotely. Before I go along that route though, am I missing an integration step somewhere? I created the classes, but that is it. I haven't seen where I need to set or change anything to actually call this method. Should the cart just pick them up with that registration class or is there something else I need to do?

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 11, 2014 21:48

The way that it is supposed to work is that when the ecommercemoduleloader is initialized, it should call your getcustomclass method which returns your class with the overrides. Kentico makes it very easy to override their functions globally. It looks like you should have everything just by glancing, but the only way that you will find out is by debugging. Put breakpoints in both classes and make sure that the code is run through. If you do this, you should be able to narrow the issue down.

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on September 11, 2014 22:34

I thought I had found the answer. The following line was not in my loader class:

TaxClassInfoProvider.ProviderObject = new FranchiseTaxClassInfoProvider();

However, I added it and it made no difference.

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on September 12, 2014 00:42

OK, so I finally got some breakpoints in place and it is never getting to them. I'm not sure what that means or where my next step is.

Thanks

0 votesVote for this answer Mark as a Correct answer

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