tags:

views:

30

answers:

2

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.

A: 

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);
}
Greg B
A: 

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>
Ryan