How to get 'image.attached_to_variant?' to work on the media object.

Chulee0606
New Member
1 0 1

Hi, 

I'm trying to hide the variant images from the product thumbnails below the main image. Normally I would use 'image.attached_to_variant?' to check and hide them, but because the newer themes are using the media object. instead of image, to support new types of medias, this doesn't work according to Shopify documentation. Does anyone know how to achieve the same result with the media object? 

Screen Shot 2020-09-16 at 9.34.41 AM.png

 

davidhollenbeck
Shopify Partner
9 0 1

Hey Chulee, did you ever get this working?

0 Likes
ryan99
Shopify Partner
8 0 2

The simplest solution I've found is to build an array of the images attached to variants and then reference that within the loop:

 

{%- assign variant_images = product.images | where: 'attached_to_variant?', true | map: 'src' -%}
{%- if product.media.size > 1 -%}
    {%- for media in product.media -%}
       {%- if variant_images contains media.src -%}
           ... do what you need to do with the variant images
       {%- endif -%}
       ... do whatever else you need to do
   {%- endfor -%}
{%- endif -%}   

 

Since the the only media type that can be attached to variants is images you won't risk missing any. `{{media.src}}` seems to be the most reliable way to match since the id's very under different circumstances.

Christopotamus
New Member
1 0 0

Absolutely insane that we have to resort to this... but it works great. I imagine if you have a lot of products being displayed with lots of different variant images, this will slow things down a lot, but I guess it's probably one of the only ways to do it without an external app, which may end up being even slower

0 Likes