使用 nopCommerce 设置 ProductVariantAttribute 值

发布于 2024-09-16 12:46:31 字数 641 浏览 2 评论 0原文

我需要为每次销售的每件商品指定一些值。想象一下能够为购物篮中的每件商品单独添加礼物信息。

如何才能实现这一目标?

我正在使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-09-23 12:46:31

要手动设置产品变体的产品属性值,您可以使用以下帮助程序方法:

  • 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 类型。这允许将任何字符串设置为属性的值。

流程概述

  1. 获取产品变体,这假设您已经知道产品 ID 以及您想要的产品的变体(如果您有多个变体)。
  2. 获取变体的属性。
  3. 使用 ProductAttributeHelper 生成属性 XML 字符串 使用
  4. 这些属性将产品保存到购物车。

示例代码

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

  1. 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).
  2. Get the attributes for the variant.
  3. Use ProductAttributeHelper to generate your attribute XML string
  4. 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);
}
夜无邪 2024-09-23 12:46:31

只是添加到这个字符串中。产品属性的 XML 如下所示...

<Attributes>
  <ProductVariantAttribute ID="66">
    <ProductVariantAttributeValue>
      <Value>484</Value>
    </ProductVariantAttributeValue>
  </ProductVariantAttribute>
  <ProductVariantAttribute ID="67">
    <ProductVariantAttributeValue>
      <Value>486</Value>
    </ProductVariantAttributeValue>
  </ProductVariantAttribute>
</Attributes>

Just to add to this string. The XML for the product attributes look like this...

<Attributes>
  <ProductVariantAttribute ID="66">
    <ProductVariantAttributeValue>
      <Value>484</Value>
    </ProductVariantAttributeValue>
  </ProductVariantAttribute>
  <ProductVariantAttribute ID="67">
    <ProductVariantAttributeValue>
      <Value>486</Value>
    </ProductVariantAttributeValue>
  </ProductVariantAttribute>
</Attributes>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文