How do I iterate through product "options"?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

How do I iterate through product "options"?

Post by yattias » Thu Sep 10, 2009 4:16 pm

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!

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

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

Post by Logan Rhodehamel » Fri Sep 11, 2009 8:33 am

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
Image.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.

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Mon Sep 28, 2009 7:39 pm

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!

afm
Captain (CAPT)
Captain (CAPT)
Posts: 339
Joined: Thu Nov 03, 2005 11:52 pm
Location: Portland, OR
Contact:

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

Post by afm » Mon Sep 28, 2009 9:27 pm

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)
Andy Miller
Structured Solutions

Shipper 3 - High Velocity Shipment Processing

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Tue Sep 29, 2009 7:33 pm

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

afm
Captain (CAPT)
Captain (CAPT)
Posts: 339
Joined: Thu Nov 03, 2005 11:52 pm
Location: Portland, OR
Contact:

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

Post by afm » Tue Sep 29, 2009 11:25 pm

My guess is the "specified product options are invalid" :)
Andy Miller
Structured Solutions

Shipper 3 - High Velocity Shipment Processing

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Thu Oct 01, 2009 7:42 pm

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.

afm
Captain (CAPT)
Captain (CAPT)
Posts: 339
Joined: Thu Nov 03, 2005 11:52 pm
Location: Portland, OR
Contact:

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

Post by afm » Thu Oct 01, 2009 11:33 pm

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.
Andy Miller
Structured Solutions

Shipper 3 - High Velocity Shipment Processing

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Fri Oct 02, 2009 4:02 pm

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

User avatar
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"?

Post by jmestep » Sat Oct 03, 2009 6:19 am

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

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Sat Oct 03, 2009 11:44 am

i am using build 7.0.3

User avatar
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"?

Post by jmestep » Sat Oct 03, 2009 3:05 pm

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?
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

afm
Captain (CAPT)
Captain (CAPT)
Posts: 339
Joined: Thu Nov 03, 2005 11:52 pm
Location: Portland, OR
Contact:

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

Post by afm » Sat Oct 03, 2009 5:50 pm

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].
Andy Miller
Structured Solutions

Shipper 3 - High Velocity Shipment Processing

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Fri Oct 30, 2009 10:33 am

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?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

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

Post by mazhar » Mon Nov 02, 2009 4:43 am

Instead of "3,0" pass option list like "3,0,0,0,0,0,0,0" and try again.

yattias
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Thu Sep 10, 2009 4:11 pm

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

Post by yattias » Fri Nov 06, 2009 10:34 am

Thanks mazhar and all, that worked.

Post Reply