How do I iterate through product "options"?
How do I iterate through product "options"?
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!
- Logan Rhodehamel
- Developer
- Posts: 4116
- Joined: Wed Dec 10, 2003 5:26 pm
Re: How do I iterate through product "options"?
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.
Cheers,
Logan
.com
If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.
Logan

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.
Re: How do I iterate through product "options"?
Thanks Logan,
What I am doing is this for the case where an option ID is specified:
The error I am getting is this:
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()
This error only happens when I call CreateForProduct with a three parameter signature. Any reason why? Thanks again!C:\Inetpub\vhosts\serialcomm.com\httpdocs\AddToBasket.aspx.vb(70): error BC30516: Overload resolution failed because no accessible 'CreateForProduct' accepts this number of arguments.
Re: How do I iterate through product "options"?
CreateForProduct does not take the 3 parameters you are passing. Use one of the other overloads.yattias wrote:This error only happens when I call CreateForProduct with a three parameter signature. Any reason why? Thanks again!
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"?
Thanks for the reply Andy,
I tried the following (using VB)
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
I tried the following (using VB)
Code: Select all
Dim basketItem As BasketItem = BasketItemDataSource.CreateForProduct(Convert.ToInt32(productId), Convert.ToInt16(productQty), optionId, String.Empty)
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"?
My guess is the "specified product options are invalid" 

Re: How do I iterate through product "options"?
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.
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"?
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.
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"?
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:
The code above displays the choice ids which I tried passing in the following lines of code:
This code gives me the following error as usual:
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(" " + Choice.Name.ToString() + ", " + Choice.OptionChoiceId.ToString() + ", Did Load? " + didLoad.ToString + "<br/>")
Next
Next
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()
Exception of type 'System.Web.HttpUnhandledException' was thrown.; specified product options are invalid Parameter name: optionList
- jmestep
- AbleCommerce Angel
- Posts: 8164
- Joined: Sun Feb 29, 2004 8:04 pm
- Location: Dayton, OH
- Contact:
Re: How do I iterate through product "options"?
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.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx
Re: How do I iterate through product "options"?
i am using build 7.0.3
- jmestep
- AbleCommerce Angel
- Posts: 8164
- Joined: Sun Feb 29, 2004 8:04 pm
- Location: Dayton, OH
- Contact:
Re: How do I iterate through product "options"?
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?
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?
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx
Re: How do I iterate through product "options"?
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].
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"?
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:
And I get the following output:
To do so I am doing the following:
I have even tried the following to make sure that the values are correct:
And I always seem to get the following error message:
Am I doing anything wrong code wise? If so, what should I change / do?
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(" " + Choice.Name.ToString() + ", " + Choice.OptionChoiceId.ToString() + ", Did Load? " + didLoad.ToString + "<br/>")
Next
Next
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.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
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
As mentioned above, I am running version 7.0.3 of AbleCommerce.Exception of type 'System.Web.HttpUnhandledException' was thrown.; specified product options are invalid Parameter name: optionList
Am I doing anything wrong code wise? If so, what should I change / do?
Re: How do I iterate through product "options"?
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"?
Thanks mazhar and all, that worked.