OptionsDetails - correct way to create?
Can someone provide the correct code to create a new OptionsDetails object for use when adding a product to the cart? I know the product and I know the variants chosen, but I'm not sure of the correct way to turn this information into an OptionsDetails object. SKU, VariantAsNames and VariantAsIds are just strings, so do I just have to create those strings myself, or is there some helper that will create them if I pass the relevant objects in?
Hello Nick,
I will have a some code snippets for you shortly. I will let you know once the developers have finished with the code snippets.
Thank you for being patient while we work on this kind of implementation.
Regards,
Grace Hallwachs
the Telerik team
Hello,
I have provided a couple different examples for different scenarios you may be working with.
1)If you are using the ProductOptionsControl (the control that displays the drop-down(s) for the available options), then you can get the OptionsDetails using the following code:
OptionsDetails optionsDetails = productOptionsControl.SelectedOptions;
To create the VariantAsNames string by hand, do the following. First create the following statement:
List<List<AttributeValueNamePair>> attributeValueNamePairs =
new
List<List<AttributeValueNamePair>>();
Then for each option selected, create a new instance of AttributeValueNamePair and set its properties.
// ***** Begin loop through selected options *****
AttributeValueNamePair attributeValueNamePair =
new
AttributeValueNamePair();
attributeValueNamePair.Attribute = name of selected attribute (e.g. “size”)
attributeValueNamePair.AttributeValue = name of selected attribute value (e.g. “Large”)
// Add attributeValueNamePair to a List<AttributeValueNamePair>.
List<AttributeValueNamePair> optionNames =
new
List<AttributeValueNamePair>();
optionNames.Add(attributeValueNamePair);
// Now add optionNames to attributeValueNamePairs.
attributeValueNamePairs.Add(optionNames);
// ***** End loop through selected options *****
After iterating through all the selected options, you can serialize attributeValueNamePairs to a string of JSON using the following code:
JavaScriptSerializer serializer =
new
JavaScriptSerializer();
string
VariantAsNames = serializer.Serialize(attributeValueNamePairs);
To create the VariantAsIds string by hand, do the following. First create the following statement:
List<List<AttributeValuePair>> attributeValuePairs =
new
List<List<AttributeValuePair>>();
Then for each option selected, create a new instance of AttributeValuePair and set its properties.
// ***** Begin loop through selected options *****
AttributeValuePair attributeValuePair =
new
AttributeValuePair();
attributeValuePair.AttributeId = GUID identified of selected attribute (e.g. the GUID identifier
for
“size”)
attributeValuePair.AttributeValueId = GUID identifier of selected attribute value (e.g. the GUID identifier
for
“Large”)
// Add attributeValuePair to a List<AttributeValuePair>.
List<AttributeValuePair> optionIds =
new
List<AttributeValuePair>();
optionIds.Add(attributeValuePair);
// Now add optionIds to attributeValuePairs.
attributeValuePairs.Add(optionIds);
// ***** End loop through selected options *****
After iterating through all the selected options, you can serialize attributeValuePairs to a string of JSON using the following code:
JavaScriptSerializer serializer =
new
JavaScriptSerializer();
string
VariantAsIds = serializer.Serialize(attributeValuePairs);
Thanks very much, that second option works perfectly for us.