This question might be a bit far fetched, but I guess I lose nothing by asking.
I understand that in the GraphQL API we have prefixes for product and variant ids (gid://shopify/ProductVariant/[number], gid://shopify/Product/[number]) but, referring to the numeric part of the id, are there any patterns or validations which could help us detect that it’s a variant id, or to differentiate a product id from a variant id?
It would be ideal to know a consistent set of conditions that variant ids always satisfy.
A bit of context: I’m currently working on a theme embed extension script that dynamically reads ids from the DOM, and I don’t have any way to know if the id that I’m reading is a product or a variant id, and in my case I need variant ids only to fetch it’s data.
Sorry if this question is a bit too specific! Any insight helps.
Hi @Pridentt!
You’re looking at a very complex and unreliable regex pattern going down that route (unreliable as we may make changes on our end without notice).
If you’re reading the DOM, you can try reading it for the terms ‘product’ and ‘variant’. Usually this gets output as in a class or part of a data attribute.
I find reading the structured data (the script tags with a type of application/ld+json) great for this).
pretty weird request… variant_id is a lot bigger (like some billion) than product_id this is your best bet (if you cannot / dont want check them one by one) but if i was in you i’ll find a more empiric way
Agreed, I ended up searching for common patterns in themes for storing variant ids and used that to get them in a relatively effective way.
Just wanted to confirm that there wasn’t a more deterministic way to validate variant ids that I didn’t know of.
Thanks @Paige-Shopify and @emilianoc !
As a heuristic only , and possibly only for new stores.
If this is put in a public app it will likely bite you.
The sanest way to differentiate GID-less numbers is to call an endpoint to sample it pass/error style.
e.g. ajax api for online-sales-channel, storefront api
If product and no variant == product
If no product and variant == variant
if product && variant == log the unique collision for the novelty
.
Since this thread is already closed I am replying just once only for educational purposes and nomore, I do not mean to be rude in any way and if you want to talk more or reply fell free to reach me by PM or whatever
I don’t see the point of quoting only that part and decontextualizing the meaning
by the way: taking for granted that it’s much more better store data in a way that let you avoid the check.
-
They are not going to bite you for use less storefront tokenless api they probably thanks you.
the rule is:
- if its not against the rule
- if it save you from call the api
- if it works on every shop and every theme
- if it works even if they change something and still work for years
than aleluya
-
you’re more likely to get bitten if you go and check thousands of products one by one without the right timing
assume you have a list of 10 ids on your page and 100k page viewed par day you have 3 Million tokenless storefront api request per day … it’s not a big number but it’s something you have to factor in when designing the logic or you get bitten
.
-
in 99% of cases they(shopify) cannot even read your logic and in 100% of cases they(shopify) don’t want to
it is your responsability write good backend or frontend logic
if you want compare color with size on your logic you are free to do it … and if some one want to buy an app which compare color and size they are free to buy.
the only thing that really matters is not breaking the rules
-
this is the worst thing that can be done read the “3 Millions reasons above” … If it’s not Shopify that disables your app, it will be your competitor with a DDOS.
last but not least a product cannot be a variant and viceversa ids are uniques across the whole shopify ecosystem
you cannot find a single product or variant with the same id not even if you check across all shops
so if you want to check:
if is_variant : take_variant()
else try take_product() catch log_or_whatever
1 Like
Yes , that’s the point of doing the check it tells you which deterministically instead of just blindly assuming and hoping and insisting.
But good luck with the rest of that yeeesh 