扩展ShoppingCartInfo对象以在Kentico CMS中添加新的税务类别

本文关键字:添加 税务 CMS 对象 ShoppingCartInfo Kentico 扩展 | 更新日期: 2023-09-27 17:58:02

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

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

因此:

  1. 我需要扩展哪些对象才能将新税率分配给某个税类
  2. 当产品添加到用户的购物车中时,我需要修改哪些用户控件才能计算产品的税价和总价

更新1:

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

我建立了一些联系:

CMSModules_Ecommerce_Controls_ShoppingCart_ShopingCartContent类继承自ShoppingCartStep,后者具有一个名为Shopping Cart的属性。ShoppingCart属性似乎正在公开Shopping卡特尔信息类的属性。其中一个属性被称为ContentTable,它是似乎包含购物车数据的DataTable对象。如果这是真的,那么我相信我需要一些方法来修改这个表,以包括我的新税率(或者如果数据表包括计算值,则需要做更多的事情)。

更新2:

这篇文章看起来会给我指明正确的答案。

扩展ShoppingCartInfo对象以在Kentico CMS中添加新的税务类别

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

然而,税务类的类并没有按照我预期的方式建模,而且我真的无法理解税务类、购物车和所有其他相关电子商务类中所有数据的相关位置,因为我无法访问源代码。

因此,我只是创建了一个自定义TaxClassInfoProvider类,并根据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()方法。