Set ProductVariantAttribute with nopCommerce

I have a requirement to specify some values ​​for each item for sale. Imagine that you can add a gift message to each item in the basket individually.

How can this be achieved?

I am using nopCommerce 1.6 (for compatibility with .net 3.5).

I added three Product Attributes (Catalog> Products> Product Attributes). The created product and in the product variant added three attributes to the product by default.

The attributes are of type TextBox, which, I believe, will allow me to enter any value that I like as a string.

How to programmatically set these values. From what I can say, ShoppingCartManager.AddToCartit looks like this: as a fourth argument, a string containing XML for the attributes is required:

public static List<string> AddToCart(ShoppingCartTypeEnum shoppingCartType, int productVariantId, string selectedAttributes, decimal customerEnteredPrice, int quantity);

But I don’t see anything that explains how XML should be structured.

Please note: I am integrating with another CMS, so I do not use standard nopCommerce controls to display products.

+3
source share
2 answers

To manually set the value of product attributes in 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 assumes your project is based on a regular nopCommerce example site.)

The process is fairly straightforward; I assume the product attributes are of type TextBox in the nopCommerce directory. This allows you to set any string as an attribute value.

Process overview

  • , , ( ).
  • .
  • 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);
}
+2

. XML :

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

Source: https://habr.com/ru/post/1761499/


All Articles