我需要为每件商品指定一些值。 想象一下,能够单独向购物篮中的每件商品添加礼品消息。
怎么能实现这一目标?
我正在使用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控件来显示产品。
I have a requirement to specify some values per-item per-sale. Imagine being able to add a gift message to each item in the basket individually.
How can this be achieved?
I'm using nopCommerce 1.6 (for .net 3.5 compatibility).
I have added three "Product Attributes" (Catalog > Products > Product Attributes). Created a product and in the default product variation, added the three attributes to the product.
The attributes are of type TextBox which, I believe will allow me to enter any value I like as a string.
How do I programatically set these values. From what I can tell ShoppingCartManager.AddToCart looks like it takes a string containing XML for the attributes as the fourth argument:
public static List<string> AddToCart(ShoppingCartTypeEnum shoppingCartType, int productVariantId, string selectedAttributes, decimal customerEnteredPrice, int quantity);But I can't see anything that explains how the XML should be structured.
Please note: I'm integrating with another CMS so I'm not using the standard nopCommerce controls for the display of the products.
最满意答案
要在产品变体上手动设置产品属性的值,您可以使用以下中的辅助方法:
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类型。 这允许将任何字符串设置为属性的值。
流程概述
获取产品变体,假设您已经知道产品ID以及您想要的产品变体(如果您有多个)。 获取变体的属性。 使用ProductAttributeHelper生成属性XML字符串 使用这些属性将产品保存到购物车。示例代码
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); }To manually set the value of product attributes on a product variant you can use the helper methods found in :
NopSolutions.NopCommerce.BusinessLogic.Products.ProductManager NopSolutions.NopCommerce.BusinessLogic.Products.Attributes.ProductAttributeManager NopSolutions.NopCommerce.BusinessLogic.Products.Attributes.ProductAttributeHelper NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartManager(this presumes your project is based on the normal nopCommerce example site.)
The process is fairly straight forward however; I assume the product attributes are of type TextBox in the nopCommerce catalog. This allows any string to be set as the value of the attribute.
Overview of process
Get the product variant, this assumes you already know the product Id and which variant of the product you want (if you have more than one). Get the attributes for the variant. Use ProductAttributeHelper to generate your attribute XML string Save the product to the cart with these attributes.Example code
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); }更多推荐
发布评论