扩展ShoppingCartInfo对象以在Kentico CMS中添加新的Tax Class

时间:2014-04-11 22:01:26

标签: c# kentico

我需要添加邮政编码作为新属性,您可以通过该属性在Kentico中定义税收类。

我知道我需要在数据库中创建两个新表 - 一个用于存储邮政编码及其ID,另一个用于存储ZIP代码的税率,其中包含ZIPID的外键和TaxClassID的外键 - 但我不知道Kentico项目中的所有对象和控件都涉及到将产品添加到购物车时为产品分配税目的过程。

所以:

  1. 我需要扩展哪些对象才能将新税率分配给税级?
  2. 我需要修改哪些用户控件,以便在将产品添加到用户购物车时计算产品的税价和总价?
  3. 更新1:

    我只需要弄清楚如何从名为TaxClassZIP的自定义表格中将数据绑定到我的购物车控件(就像COM_TaxClassCountry和COM_TaxClassState一样)。

    我做过的一些联系:

    CMSModules_Ecommerce_Controls_ShoppingCart_ShoppingCartContent类继承自ShoppingCartStep,后者具有名为ShoppingCart的属性。 ShoppingCart属性似乎是公开ShoppingCartInfo类的属性。其中一个属性称为ContentTable,它是DataTable对象,似乎包含购物车的数据。如果这是真的,那么我认为我需要一些方法来修改此表以包括我的新税率(或者如果数据表包含计算值,则需要更多)。

    更新2:

    This article看起来会指出我正确的答案。

1 个答案:

答案 0 :(得分:3)

最初,我想加入Kentico的内置税级。我想我可以通过继承TaxClassInfo类并为ZIPCodeID定义一个新字段,然后重写TaxClassInfoProvider类的方法来添加ZIPCodeID作为计算税的参数来实现这一点。

然而,税收课程的课程并没有像我期望的那样建模,而且我无法理解税务课程,购物车和所有课程之间所有数据的相关性。其他相关的电子商务课程,因为我无法访问源代码。

相反,我只创建了一个自定义的TaxClassInfoProvider类,并按this webinar from Kentico覆盖了GetTaxesInternal()方法。

页面底部代码示例中的一个文件已经构建了很多:

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

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

/// <summary>
/// Sample tax class info provider. 
/// Can be registered either by replacing the TaxClassInfoProvider.ProviderObject (uncomment the line in SampleECommerceModule.cs) or through cms.extensibility section of the web.config
/// </summary>
public class CustomTaxClassInfoProvider : TaxClassInfoProvider
{
    #region "Example: Custom taxes calculation"

    /// <summary>
    /// Returns DataSet with all the taxes which should be applied to the shopping cart items.
    /// </summary>
    /// <param name="cart">Shopping cart</param>
    protected override DataSet GetTaxesInternal(ShoppingCartInfo cart)
    {
        DataSet ds = new DataSet();

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

        // Build taxes table 
        // ------------------------
        // Please note:         
        // Taxes table is built manually for the purpose of this example, however you can build it from the response of a tax calculation service as well.
        // All the data which might be required for the calculation service is stored in the ShoppingCartInfo object, e.g.:
        // - use AddressInfoProvider.GetAddresInfo(cart.ShoppingCartBillingAddressID) to get billing address info
        // - use AddressInfoProvider.GetAddresInfo(cart.ShoppingCartShippingAddressID) to get shipping address info        
        // etc.
        // ------------------------
        foreach (ShoppingCartItemInfo item in cart.CartItems)
        {
            // Get SKU properties
            string skuNumber = item.SKU.SKUNumber.ToLowerCSafe();
            int skuId = item.SKUID;

            switch (skuNumber)
            {
                // Tax for product A (20%)
                case "a":
                    AddTaxRow(table, skuId, "Tax A", 20);
                    break;

                // Taxes for product B (11% and 10%)
                case "b":
                    AddTaxRow(table, skuId, "Tax B1", 11);
                    AddTaxRow(table, skuId, "Tax B2", 10);
                    break;

                // Zero tax for product C (0%)
                case "c":
                    break;

                // The same tax for all other products (5%)
                default:
                    AddTaxRow(table, skuId, "Tax C", 5);
                    break;

            }
        }

        // Return built dataset with the taxes
        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));

        // Add optional columns
        //table.Columns.Add("TaxIsFlat", typeof(bool));
        //table.Columns.Add("TaxIsGlobal", typeof(bool));
        //table.Columns.Add("TaxClassZeroIfIDSupplied", 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>
    /// <param name="taxIsFlat">Indicates if the tax value is flat or relative. By default it is false (= relative tax)</param>
    /// <param name="taxIsGlobal">Indicates if the tax value is in global main currency or in site main currency. By default it is false (= tax value is in site main currency).</param>    
    /// <param name="taxIsGlobal">Indicates if the tax is zero if customer's registration ID is supplied. By default it is false (= tax is not zero if customer's tax registration ID is supplied).</param>    
    private void AddTaxRow(DataTable taxTable, int skuId, string taxName, double taxValue, bool taxIsFlat, bool taxIsGlobal, bool zeroTaxIfIDSupplied)
    {
        DataRow row = taxTable.NewRow();

        // Set required columns
        row["SKUID"] = skuId;
        row["TaxClassDisplayName"] = taxName;
        row["TaxValue"] = taxValue;

        // Set optional columns
        //row["TaxIsFlat"] = taxIsFlat;
        //row["TaxIsGlobal"] = taxIsGlobal;
        //row["TaxClassZeroIfIDSupplied"] = taxIsGlobal;

        taxTable.Rows.Add(row);
    }


    /// <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)
    {
        AddTaxRow(taxTable, skuId, taxName, taxValue, false, false, false);
    }

    #endregion

    #endregion
}

在我发现这个之后,我在SQL Server中创建了一个非常简单的3列表,其中包含ID,ZIP和TaxRate。

然后,我对上述代码进行了一些修改,以便使用地址信息提供商访问税率和当前客户的地址信息:

AddressInfo customerAddress = AddressInfoProvider.GetAddressInfo(cart.ShoppingCartShippingAddressID);

之后,只需用我的邮政编码税率填充数据集,然后将适当的税率传递给AddTaxRow()方法。