Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
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!
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
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?
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