Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To manually set the value of product attributes on a product variant you can use the helper methods found in :</p> <ul> <li><code>NopSolutions.NopCommerce.BusinessLogic.Products.ProductManager</code></li> <li><code>NopSolutions.NopCommerce.BusinessLogic.Products.Attributes.ProductAttributeManager</code></li> <li><code>NopSolutions.NopCommerce.BusinessLogic.Products.Attributes.ProductAttributeHelper</code></li> <li><code>NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartManager</code></li> </ul> <p>(this presumes your project is based on the normal nopCommerce example site.)</p> <p>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.</p> <p><strong>Overview of process</strong></p> <ol> <li>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).</li> <li>Get the attributes for the variant.</li> <li>Use ProductAttributeHelper to generate your attribute XML string</li> <li>Save the product to the cart with these attributes.</li> </ol> <p><strong>Example code</strong></p> <pre><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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload