c# - 扩展 ShoppingCartInfo 对象以在 Kentico CMS 中添加新的税类

标签 c# kentico

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

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

所以:

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

更新1:

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

我建立的一些联系:

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

更新2:

This article看起来它会指引我找到正确的答案。

最佳答案

最初,我想添加到 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() 方法即可。

关于c# - 扩展 ShoppingCartInfo 对象以在 Kentico CMS 中添加新的税类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23023318/

相关文章:

c# - 为什么要更改后退按钮的标题?

c# - 装饰器模式,通过继承还是依赖注入(inject)?

C# 程序使用 NPOI 编辑 excel(.xls) 的单元格值不起作用

events - Kentico - 自动电子邮件确认 - 事件预订系统

azure-blob-storage - 页面附件中的图像与媒体库 Kentico

c# - 如何检查字符串是否包含超过 50 个字符的单词?

c# - 是什么导致 Calibri 在 9 到 14 pt 之间丢失 ClearType?

c# - IsDesign 与 PortalContext.IsDesignMode() 之间的区别

asp.net - 如何将 Kentico 实例复制到本地机器?

c# - 类型或命名空间名称不存在