gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:07:22 26 4
gpt4 key购买 nike

我需要添加邮政编码作为新属性,您可以通过它在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com