Exclude Variant Images in Shopify

To my chagrin, I cannot now find the post where I got this code – unknown generous developer, I apologise.

This kind of illustrates a point though – I got lucky finding that post. If you are reading this, you got lucky finding this post.

In terms of organisation, curation and outright findability, Shopify University is no Stackoverflow, that’s for sure. There are even people selling Apps at $5 a month for this simple code change.

You’ve got a product, you have product variants, those product variants have images, you don’t want those images appearing in the main carousel/gallery for the product.

So, how do you exclude product variant images in the product.liquid template?

Easy;

{% for image in product.images %}
    {% unless image.attached_to_variant? %}

    <img src="{{ image.src | img_url: 'master' }}" alt="{{ image.alt | escape }}" />
        
    {% endunless %}
{% endfor %}