首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >扩展ShoppingCartInfo对象以在Kentico中添加一个新的税类

扩展ShoppingCartInfo对象以在Kentico中添加一个新的税类
EN

Stack Overflow用户
提问于 2014-04-11 22:01:25
回答 1查看 1.1K关注 0票数 2

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

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

所以:

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

更新1:

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

我已经建立了一些联系:

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

更新2:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-18 19:42:18

最初,我想在Kentico的内置税种中添加一些内容。我认为我可以从TaxClassInfo类继承并为ZIPCodeID定义一个新字段,然后重写TaxClassInfoProvider类的方法,将ZIPCodeID添加为计算税收的参数。

然而,税务类的类并不像我所期望的那样建模,而且由于我无法访问源代码,所以我无法真正理解所有数据在税务类、购物车和所有其他相关电子商务类之间的关联位置。

因此,我只是创建了一个定制的TaxClassInfoProvider类,并重写了每个这次来自肯蒂科的网络研讨会的GetTaxesInternal()方法。

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

代码语言:javascript
运行
复制
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
}

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

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

代码语言:javascript
运行
复制
AddressInfo customerAddress = AddressInfoProvider.GetAddressInfo(cart.ShoppingCartShippingAddressID);

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23023318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档