I have a requirement for the customer to be able to values on a per item per sale basis.
This functionality is similar to the idea of a "enter a custom message" when you're ordering gift wrap, but I need to be able to add a custom message for each item in the basket.
I've done this as I figured this would allow what I need. However; I guess I missed one important details.
I'm not using nopCommerce as the frontend for the store. I'm integrating it into another CMS (which is going well so far btw).
So my question really is, how do I set the value for these product attributes programmatically?
When I call
var addToCartWarnings = ShoppingCartManager.AddToCart(ShoppingCartTypeEnum.ShoppingCart, variant.ProductVariantId, string.Empty, decimal.Zero, 1);
I get three warnings telling me that my three attributes needs setting. From digging around in the system, I believe I should be submitting some XML (as a string) to the fourth argument but I can't find a schema for this XML nor a utility method for generating it.
So my question really is, how do I set the value for these product attributes programmatically?
If you're using 'Products in grid' template, just set breakpoint into rptVariants_OnItemCommand method of Modules\ProductVariantsInGrid.ascx.cs file and see what values are passed into ShoppingCartManager.AddToCart method
That's working great for me but now I'm trying to get the values out.
I'm doing this:
var attributes = ProductAttributeHelper.ParseProductVariantAttributeValues(CartItem.AttributesXml);
I can see the XML String in CartItem.AttributesXml but attributes comes back as an empty collection.
When I step through the code I can see that in ParseProductVariantAttributeValues (ProductAttributeHelper.cs line 100) it skips parsing the value because if pva.ShouldHaveValues == false:
Line 100 : if (!pva.ShouldHaveValues) Line 101 : continue;
and in turn, this is set because the type of control is TextBox (ProductVariantAttribute.cs line 122)
ProductVariantAttribute.cs
Line 118 : publicbool ShouldHaveValues Line 119 : { Line 120 : get Line 121 : { Line 122 : if (this.AttributeControlType == AttributeControlTypeEnum.TextBox || Line 123 : this.AttributeControlType == AttributeControlTypeEnum.MultilineTextbox) Line 124 : { Line 125 : returnfalse; Line 126 : }
So how do I get the value that was stored in these attributes?
1. Create a product with textbox attribute. 2. Set breakpoint in the same line of code 3. See how textbox values are stored into this XML property (//Attributes/ProductVariantAttribute/ProductVariantAttributeValue/Value)
Not sure that I got you. See how attributes are rendered on a shopping cart page (below each product line). We use ProductAttributeHelper.FormatAttributes() method where all attributes and their values are loaded