What's the actual purpose of the 'variant_ids' attribute of Image?

sexybit
Shopify Partner
19 0 0

Good day.

 

As saddening and disconcerting as that is, Shopify's support has referred me to "the community of developers" to answer questions that they are unable to answer rather than escalate my request.

 

I'm writing to inquire about the purpose of the 'variant_ids' attribute of the Image entity.

 

I'm asking because:

1. There already seems to be a way to associate a Variant to an Image (and that's the Variant.image_id). If my understanding is correct, and please correct me if I am wrong, Shopify only allows 1 image to be associated to a Variant (and the rest to be associated to Product).

2. I've been observing some unexpected behavior when setting variant_ids with multiple variants. Specifically, after setting variant_ids to multiple values for EACH Image, subsequent querying of those Image(s) returns the variant_ids attribute empty for some of those images. I would attach a screenshot but .png files are not allowed here as attachment ...

 

Thank you in advance for shedding some light.

 

Replies 2 (2)

ShopifyDevSup
Shopify Staff
1357 224 473

Hey @sexybit - thanks for getting in touch. You're right that we do only allow for one image to be associated per variant. If I'm understanding correctly, you're seeing the variant_id field for some of those images showing up as empty despite the image being associated with variants? The main purpose of the variant_id field in this context is to allow you to attach the image to a variant within a Product Image Creation Post request

You are right though, you can also add product images to variants within a Product Variant Creation API call, which is the resource that uses the Variant.image_id field. My understanding is that the separation of the two fields under their respective objects (Image/Product variant) is to offer alternative ways of adding images to products/variants (for instance, you're making a multimedia app that doesn't need to touch the rest of the product resource - only modifying images this way could speed up processes and API calls at scale). That said, I understand how having the two methods could be seen as uncessary and I can't say for certain why the fields are returning empty arrays, but I'm happy to bring this up to my team. 

Would you be able to provide an example X-Request-ID from the header of one of Shopify's responses that shows the empty array?  This would help us determine expected behaviour. Happy to see if I can dig into this more and potentially share an answer with you if possible. 

 

Hope this helps!  

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

sexybit
Shopify Partner
19 0 0

Hi there.

 

Indeed, I now realize that Variant.image_id and Image.variant_ids[] are simply two alternative methods to set the association between the two entities. (A more eloquent documentation would've avoided all this guessing...).

 

My guess now is that some Image.variant_ids[] were being returned empty because only 1 image can be associated per variant (1:1 relation) whereas I was trying to associate multiple images to a variant. Consequently, if a variant has been associated to an image already (by means of Image.variant_ids[]), only that image will have variant_ids[] populated whereas other images will have variant_ids[] empty.

 

Unfortunately I'm unable to provide you with a X-Request-ID as I'm not logging requests/responses.

 

I will be doing more testing of Image.variant_ids[] as soon as I can and will get back to you. However, now that it's clear to me that Variant:Image is a 1:1 relation, I expect I won't have problems with this anymore.

 

Thanks.