How to set custom values per item per sale

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 лет назад
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.
13 лет назад
You can create a product attrbiute (for example, 'Your message') and add this attribute to each available product variant with textbox attribute type
13 лет назад
Hi Andrei,

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.

Any advice would be appreciated!
13 лет назад
GregBrant wrote:
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
13 лет назад
Thanks!
13 лет назад
Andrei,

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 :    public bool 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 :                        return false;
Line 126 :                    }


So how do I get the value that was stored in these attributes?
13 лет назад
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)
13 лет назад
So I should retrieve the data manually?

OK, thanks.
13 лет назад
GregBrant wrote:
So I should retrieve the data manually?

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
13 лет назад
Ah, ok. Thanks again
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.