OptionsDetails - correct way to create?

Posted by Community Admin on 05-Aug-2018 14:36

OptionsDetails - correct way to create?

All Replies

Posted by Community Admin on 02-Feb-2012 00:00

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?

Posted by Community Admin on 08-Feb-2012 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 11-Feb-2012 00:00

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:

Copy Code
OptionsDetails optionsDetails = productOptionsControl.SelectedOptions;
 
where productOptionsControl is a reference to the ProductOptionsControl on the page.

2) If you are NOT using the ProductOptionsControl, you will have to create SKU, VariantAsNames and VariantAsIds strings by hand.  There is no helper method.

To create the VariantAsNames string by hand, do the following. First create the following statement:

Copy Code
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 *****

Copy Code
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:

Copy 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:

Copy Code
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 *****

Copy Code
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:

Copy Code
JavaScriptSerializer serializer = new JavaScriptSerializer();
string VariantAsIds = serializer.Serialize(attributeValuePairs);

Please let us know if you have any questions about the code snippet.
Kind regards,
Grace Hallwachs
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 16-Feb-2012 00:00

Thanks very much, that second option works perfectly for us.

This thread is closed