Page 1 of 1

How do I iterate through product "options"?

Posted: Thu Sep 10, 2009 4:16 pm
by yattias
Hi, I need to iterate through the various product options (color, choices) that I defined in the admin panel for my products. How would I do so code wise? What classes would I need to instantiate? Sample code will greatly be appreciated!

Re: How do I iterate through product "options"?

Posted: Fri Sep 11, 2009 8:33 am
by Logan Rhodehamel
If you look in the admin section of the site, there is a page that creates the variant grid where it shows (and allows you to page through) all of the variants. That would be a great place to get sample code from.

Re: How do I iterate through product "options"?

Posted: Mon Sep 28, 2009 7:39 pm
by yattias
Thanks Logan,

What I am doing is this for the case where an option ID is specified:

Code: Select all


Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(productId), Convert.ToInt16(productQty), Convert.ToInt32(optionId))
Token.Instance.User.Basket.Items.Add(basketItem)
Token.Instance.User.Basket.Save()

The error I am getting is this:
C:\Inetpub\vhosts\serialcomm.com\httpdocs\AddToBasket.aspx.vb(70): error BC30516: Overload resolution failed because no accessible 'CreateForProduct' accepts this number of arguments.
This error only happens when I call CreateForProduct with a three parameter signature. Any reason why? Thanks again!

Re: How do I iterate through product "options"?

Posted: Mon Sep 28, 2009 9:27 pm
by afm
yattias wrote:This error only happens when I call CreateForProduct with a three parameter signature. Any reason why? Thanks again!
CreateForProduct does not take the 3 parameters you are passing. Use one of the other overloads.

There are 3 versions:

CreateForProduct(int productId, short quantity)
CreateForProduct(int productId, short quantity, string optionList, string kitList)
CreateForProduct(int productId, short quantity, string optionList, string kitList, int userId)

optionList look like "1,2,3". Each value is a choice for the corresponding option. They should be in the same order that options are presented in the admin.

For example, if the first option choice is 123, and the second is 456, then you would pass "123,456".

I assume kitList is the same. If you don't have kit choices, then pass in an empty string. Putting that all together, you would do something like:

Code: Select all

Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(productId), Convert.ToInt16(productQty), optionId.ToString(), String.EmptyString)

Re: How do I iterate through product "options"?

Posted: Tue Sep 29, 2009 7:33 pm
by yattias
Thanks for the reply Andy,

I tried the following (using VB)

Code: Select all

        Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(productId), Convert.ToInt16(productQty), optionId, String.Empty)
And I am getting the following error:

Exception of type 'System.Web.HttpUnhandledException' was thrown.; specified product options are invalid Parameter name: optionList

Any idea why?

Thank you

Re: How do I iterate through product "options"?

Posted: Tue Sep 29, 2009 11:25 pm
by afm
My guess is the "specified product options are invalid" :)

Re: How do I iterate through product "options"?

Posted: Thu Oct 01, 2009 7:42 pm
by yattias
That is what I initially thought, but when I look in the "options and variants" section of the admin menu, it shows that the option ids for this specific product are 1 & 2 and these are the ids I am passing when I get the error.

I would also like to add that since I am fairly new to AbleCommerce, I check the variant ids by examining the link of the "edit option" button in the "options and variants" section. I use the OptionId that is specified in that link. On the same note, is there a better way of checking option IDs?

But regardless, what else can I do to solve this error? Please help. Thank you.

Re: How do I iterate through product "options"?

Posted: Thu Oct 01, 2009 11:33 pm
by afm
The optionList is actually a list of option choice IDs, not option IDs. For example, if the first option is Color with an option ID of 1, and the first color choice is Red with an option choice ID of 31, then you would pass "31" in optionList if you want the Red variant; not "1".

The easiest way to find the option choice IDs using the UI is to call up the product in a browser, then look in the HTML. The choice IDs will be listed in the <select> element for that option. Otherwise, you will need to read the database. Both of these assume you are looking to hard code the IDs. Personally, I would look them up using code (for example, I would scan the choices using code to find the "Red" choice and then pass the corresponding choice ID).

When faced with something that isn't working the way you expect, remember "Occam's Razor" and "select isn't Broken" from The Pragmatic Programmer.

Re: How do I iterate through product "options"?

Posted: Fri Oct 02, 2009 4:02 pm
by yattias
Hey Andy, I followed your guidelines and unfortunately it is still not working. In order to make sure that the option choices do exist, I used the commercebuilder API to display their IDs. I did the following to obtain all of the choice / variant ids for each of the options:

Code: Select all

Dim product As Product = ProductDataSource.Load(productId)
        Dim optCollection As ProductOptionCollection = product.ProductOptions

        For Each opt As ProductOption In optCollection
            Response.Write("Product Option Name: " + opt.Option.Name.ToString() + "<br/>")
            Response.Write("Product Option ID: " + opt.Option.OptionId.ToString() + "<br/>")


            'get the choices of each option
            Dim Choices As OptionChoiceCollection = opt.Option.Choices
            Response.Write("This Option has: " + Choices.Count.ToString + " Choices:" + "<br/>")

            For Each Choice As OptionChoice In Choices
                Dim didLoad As Boolean = Choice.Load(Choice.OptionChoiceId)
                Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + Choice.Name.ToString() + ", " + Choice.OptionChoiceId.ToString() + ", Did Load? " + didLoad.ToString + "<br/>")
            Next

        Next
The code above displays the choice ids which I tried passing in the following lines of code:

Code: Select all

        Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(productId), Convert.ToInt16(productQty), optionId.ToString, String.Empty)
        Token.Instance.User.Basket.Items.Add(basketItem)
        Token.Instance.User.Basket.Save()
This code gives me the following error as usual:
Exception of type 'System.Web.HttpUnhandledException' was thrown.; specified product options are invalid Parameter name: optionList

Re: How do I iterate through product "options"?

Posted: Sat Oct 03, 2009 6:19 am
by jmestep
What build of Able are you running? I vaguely remember a change to the ProductDataSource.Load having to do with options or kits where I have had to change the code after an upgrade.

Re: How do I iterate through product "options"?

Posted: Sat Oct 03, 2009 11:44 am
by yattias
i am using build 7.0.3

Re: How do I iterate through product "options"?

Posted: Sat Oct 03, 2009 3:05 pm
by jmestep
That won't be the problem then.
Have you tried putting a debug point in your code and checking the value of optionList and then put a debug point in the product page add to basket to compare the two values?

Re: How do I iterate through product "options"?

Posted: Sat Oct 03, 2009 5:50 pm
by afm
If you have more than one option, then you have to list all the choices (e.g. "123,345"). You may need to include 8 choices, even if some/most are blank (e.g. "123,,,,,,," or "123,0,0,0,0,0,0,0").

Just for kicks, load all the ProductVariant's for the product, then call GetOptionList. It will return the appropriate option list that you would need to pass. [I'm not sure it is called GetOptionList, but it is something like that].

Re: How do I iterate through product "options"?

Posted: Fri Oct 30, 2009 10:33 am
by yattias
Hi all,
Sorry I didn't reply for a while, I was working on a different project for the past month, but anyways, I tried Andy's method and it didn't work for me (maybe I am doing it wrong). And no, I did not try placing a debugging point but I did however place exact values in the function call "CreateForProduct". So maybe I should tell you exactly what I am doing and what I need done.

First off, for testing / debugging purposes, I am iterating through all the product options and display their IDs as well as their variants ids by doing the following:

Code: Select all

        'Code to iterate through all product options and choices of a specific option
        Dim product As Product = ProductDataSource.Load(productId)
        Dim optCollection As ProductOptionCollection = product.ProductOptions

        For Each opt As ProductOption In optCollection
            Response.Write("Product Option Name: " + opt.Option.Name.ToString() + "<br/>")
            Response.Write("Product Option ID: " + opt.Option.OptionId.ToString() + "<br/>")


            'get the choices of each option
            Dim Choices As OptionChoiceCollection = opt.Option.Choices
            Response.Write("This Option has: " + Choices.Count.ToString + " Choices:" + "<br/>")

            For Each Choice As OptionChoice In Choices
                Dim didLoad As Boolean = Choice.Load(Choice.OptionChoiceId)
                Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + Choice.Name.ToString() + ", " + Choice.OptionChoiceId.ToString() + ", Did Load? " + didLoad.ToString + "<br/>")
            Next

        Next
And I get the following output:
Product Option Name: Connector Type
Product Option ID: 2
This Option has: 2 Choices:
ST Connector, 3, Did Load? True
SC Connector, 4, Did Load? True
Now I need to be able to be able to add this product (or any other product) with the variant (ST or SC connectors) to the cart through code.

To do so I am doing the following:

Code: Select all

Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(productId), Convert.ToInt16(productQty), variantList, String.Empty)
'followed by code to add to basket but it doesnt matter since it fails here anyway


I have even tried the following to make sure that the values are correct:

Code: Select all

Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(23), Convert.ToInt16(1), "3,0", String.Empty) 'for ST connector product
'followed by code to add to basket but it doesnt matter since it fails here anyway
And I always seem to get the following error message:
Exception of type 'System.Web.HttpUnhandledException' was thrown.; specified product options are invalid Parameter name: optionList
As mentioned above, I am running version 7.0.3 of AbleCommerce.

Am I doing anything wrong code wise? If so, what should I change / do?

Re: How do I iterate through product "options"?

Posted: Mon Nov 02, 2009 4:43 am
by mazhar
Instead of "3,0" pass option list like "3,0,0,0,0,0,0,0" and try again.

Re: How do I iterate through product "options"?

Posted: Fri Nov 06, 2009 10:34 am
by yattias
Thanks mazhar and all, that worked.