Can I use a variable for product options

I’m attempting to finish up a custom product page that not only allows for ‘customizable products’ that have thousands of options (that part is working fine), but that will also be able to handle standard Wix-style products with their options stored in the Stores/Products database.

I can pull out the option JSON object and format it without problem (the way I have it set up is that it will handle anywhere from 0-5 options, one of the 5 being ‘Color’ with the other 0-4 being lists [drop downs]). But here’s the catch: I don’t know in advance how many options the product will have or what the option names will be. My code pulls all of that from the product data fields and passes the properly formatted option choices into a string once the user makes their choices.

The issue I’m having is that the addToCart function fails when I use the variable for choices. It keeps telling me that the option Field is required. I have the option names 100% correct and I also have the option choices 100% correct. Is this a Wix thing, not allowing a product option name to be addressed through a variable, or am I missing something?

Here’s the part of the code that it hangs up on:

optionstring=“‘Size’: ‘small’, ‘Format’: ‘dvd’” ; //option names and choices that exist for this item

        $w('#shoppingCartIcon3').addToCart(myproductId, myqty, {  

                "choices":{optionstring}})  

            .then(() => {  

                console.log("Product added");  

            })  

            .catch((error) => {  

                console.log(error);  

            });  

I know that I can pass a variable for the choices themselves, if the product option names are known. I can also copy the text of my string and put it where my ‘optionstring’ variable is and it also works fine ( “choices”:{ ‘Size’: ‘small’, ‘Format’: ‘dvd’}}) works just fine ). But the names of the options won’t be known in advance, and will vary from product to product, so I cannot preset the option names.

any ideas?

I figured it out shortly after posting this question.
Option choices are a JSON object , so they obviously won’t accept a string. For custom product pages for products that have options, one needs to convert the text-string of the option choices into an object using JSON.parse(mystring);