How to detect which productVariants belong to the same product by global id ?

How to detect which productVariants belong to the same product by global id ?

jacobovelasquez
Shopify Partner
7 0 0

Hi guys i need to detect which gids belong to a variant of the same produtc and then group it like so:

having this array:

const globalIds = [
"gid://shopify/ProductVariant/43293721723032",
"gid://shopify/ProductVariant/43293722214552",
"gid://shopify/ProductVariant/43293722280088",
"gid://shopify/ProductVariant/43293721755800",
"gid://shopify/ProductVariant/43293721788568",
"gid://shopify/ProductVariant/43293721821336",
"gid://shopify/ProductVariant/43293721854104"
];

this 2 are different products:
"gid://shopify/ProductVariant/43293722214552",
"gid://shopify/ProductVariant/43293722280088",

and this 5 are variants of the same product:

"gid://shopify/ProductVariant/43293721723032",
"gid://shopify/ProductVariant/43293721755800",
"gid://shopify/ProductVariant/43293721788568",
"gid://shopify/ProductVariant/43293721821336",
"gid://shopify/ProductVariant/43293721854104"

they share the first 8 digits of the 14 that are after the last / character, the thing is that the 2 above also share it so taking it as way to identify which are part of the same preduct is not viable.

do you guys know any way of doing this ?, I need it to recalc a bundle when it is edited in an app that im doing.

Replies 0 (0)