+1-888-365-2779
Try Now
More in this section

Forums / Ecommerce / OptionsDetails - correct way to create?

OptionsDetails - correct way to create?

4 posts, 1 answered
  1. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    02 Feb 2012
    Link to this post
    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?
  2. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    08 Feb 2012
    Link to this post
    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
  3. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    11 Feb 2012
    Link to this post
    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
    Answered
  4. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    16 Feb 2012
    Link to this post
    Thanks very much, that second option works perfectly for us.
4 posts, 1 answered