作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要为每次销售的每件商品指定一些值(value)。想象一下,能够分别为购物篮中的每件商品添加礼品留言。
如何实现?
我正在使用 nopCommerce 1.6(为了与 .net 3.5 兼容)。
我添加了三个“产品属性”(目录 > 产品 > 产品属性)。创建产品并在默认产品变体中,将三个属性添加到产品。
属性是 TextBox 类型,我相信它允许我输入任何我喜欢的字符串值。
如何以编程方式设置这些值。据我所知,ShoppingCartManager.AddToCart
看起来它需要一个包含属性 XML 的字符串作为第四个参数:
public static List<string> AddToCart(ShoppingCartTypeEnum shoppingCartType, int productVariantId, string selectedAttributes, decimal customerEnteredPrice, int quantity);
但我看不到任何解释 XML 应该如何构建的内容。
请注意:我正在与另一个 CMS 集成,因此我没有使用标准的 nopCommerce 控件来显示产品。
最佳答案
要手动设置产品变体的产品属性值,您可以使用以下帮助方法:
NopSolutions.NopCommerce.BusinessLogic.Products.ProductManager
NopSolutions.NopCommerce.BusinessLogic.Products.Attributes.ProductAttributeManager
NopSolutions.NopCommerce.BusinessLogic.Products.Attributes.ProductAttributeHelper
NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartManager
(假设您的项目基于普通的 nopCommerce 示例站点。)
但是这个过程相当简单;我假设产品属性是 nopCommerce 目录中的 TextBox 类型。这允许将任何字符串设置为属性的值。
流程概览
示例代码
private bool SaveProductToBasket()
{
var product = GetTheProduct();
int productId = product.ProductId;
var variants = ProductManager.GetProductVariantsByProductId(productId);
int variantId = GetDesiredVariantId();
var variant = variants[variantId];
var attributes =
ProductAttributeManager.GetProductVariantAttributesByProductVariantId(variant.ProductVariantId);
string data = string.Empty;
data = SetVariantAttribute(data, attributes, "Attribute1", value1.ToString());
data = SetVariantAttribute(data, attributes, "Attribute2", value2.ToString());
data = SetVariantAttribute(data, attributes, "Attributee", value3.ToString());
var addToCartWarnings =
ShoppingCartManager.AddToCart(ShoppingCartTypeEnum.ShoppingCart, variant.ProductVariantId, data, decimal.Zero, 1);
if (addToCartWarnings.Count == 0)
{
return true;
}
// TODO: Bind warnings.
return false;
}
private string SetVariantAttribute(string data, ProductVariantAttributeCollection attributes, string attributeName, string value)
{
var attribute = (from a in attributes
where a.ProductAttribute.Name == attributeName
select a).First();
return ProductAttributeHelper.AddProductAttribute(data, attribute, value);
}
关于nopcommerce - 使用 nopCommerce 设置 ProductVariantAttribute 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3565595/
我需要为每次销售的每件商品指定一些值(value)。想象一下,能够分别为购物篮中的每件商品添加礼品留言。 如何实现? 我正在使用 nopCommerce 1.6(为了与 .net 3.5 兼容)。 我
我是一名优秀的程序员,十分优秀!