Safe way to confirm that a product has no options defined?

flavio07
Shopify Partner
3 0 0

Hi! For the purpose of my application, I want to know when a product has no "options" defined, meaning it will only have one default variant representing the product itself.

 

My first thought was to check if the variants array has length of 1, but I made a test creating one single option for my product, and then the variants also come with only 1 item.

 

What I noticed it's different about the variants/options array is that when there's no option defined, the value that returns on the "options" has "name": "Title". 

 

So I was thinking if this would be the safest way to check if a product has no options, checking if it has only one option and its name is "title"? Feels a little bit hacky but I guess it works, just thinking if I ain't missing something?

 

Thanks a lot for any input on this!

Replies 3 (3)

shraddha_patel
Shopify Partner
155 24 23

Hello,

It's Shraddha.

I would like to inform you there's another efficient way to do this.

 

You have to try "has_only_default_variant" property I guess. As checking "title" is not a proper solution just try "{{ product.has_only_default_variant }}"

 

This will give you true/false based on the variants.

 

Hope this will work for you. Let me know if anything with the same.

Thank you

Need more help with theme customization and store development ? You can Reach me here
Or Text me
flavio07
Shopify Partner
3 0 0

Hi Shraddha! Thanks for the reponse!

 

I realize I just forgot to specify that I am using the Storefront API, and its graphql endpoint. I saw that the "hasOnlyDefaultVariant" seems to be available only on the Admin API. 

 

But a property/method like this would be exactly what I am looking for... Do you know if there's a way to access something like this on the Storefront API?

shraddha_patel
Shopify Partner
155 24 23

Hello,

 

I see, if that's the case then you have to go with the text comparison as there's no any property in ajax API to check it directly. Try comparing text to the variant title or option name with 'Default Title'

 

This is the only way I guess.

Let me know if any.

Thank you

Need more help with theme customization and store development ? You can Reach me here
Or Text me