Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Shopify] Show multiple images per variant
<script>
jQuery(document).ready(function($){
var images = [];
{% for image in product.images %}
images.push({url: "{{ image | product_img_url: 'medium' }}", alt: "{{ image.alt }}"});
{% endfor %}
var thumbnails = $(".thumbs");
$('#product-select-option-0').change(function() {
var selected = $(this).val(), mainImage = jQuery('.featured img').attr('src').replace('_1024x1024', '_medium');
thumbnails.hide().html("");
arr = [];
var addImage = $.each( images, function( i, image ) {
var alt = images[i].alt, url = images[i].url;
if (alt == selected || url == mainImage) {
thumbnails.append('<div class="image span2"><a href="' + url.replace('_medium', '_1024x1024') + '" data-original-image="' + url.replace('_medium', '_1024x1024') + '"><img src="' + url + '" alt="' + alt + '"></a></div>');
}
});
arr.push(addImage);
$.when.apply($, arr).done(function () {
thumbnails.fadeIn();
$('#product .thumbs a').on('click', function(e) {
e.preventDefault();
switchImage($(this).attr('href'), null, $('.featured img')[0]);
});
});
});
});
</script>
<!-- Begin thumbnails -->
<div class="thumbs clearfix">
{% assign featured_alt = product.selected_or_first_available_variant.option1 %}
{% for image in product.images %}
{% if image.alt == featured_alt or image == featured_image %}
{% if settings.enable_product_image_zoom %}
<div class="image span2{% cycle 'last-in-row': '', '', ' last-in-row' %}">
<a href="{{ image | product_img_url: '1024x1024' }}" class="cloud-zoom-gallery">
<img src="{{ image | product_img_url: 'medium' }}" alt="{{ image.alt | escape }}" />
</a>
</div>
{% else %}
<div class="image span2{% cycle 'last-in-row': '', '', ' last-in-row' %}">
<a href="{{ image | product_img_url: '1024x1024' }}" data-original-image="{{ image | product_img_url: '1024x1024' }}">
<img src="{{ image | product_img_url: 'medium' }}" alt="{{ image.alt | escape }}" />
</a>
</div>
{% endif %}
{% endif %}
{% endfor %}
</div>
<!-- End thumbnails -->
@sniepoort

This comment has been minimized.

Copy link

@sniepoort sniepoort commented Mar 1, 2015

Not really working for me. I've done a console log inside the select change function, but it doesn't seem to be running. any ideas?

@ncaney

This comment has been minimized.

Copy link

@ncaney ncaney commented Aug 26, 2015

I am trying to get this to work on the Shopify theme Canopy. I have not been able to get it to work, but the product.liquid code is a bit different from what I see here. My code skills are not good enough to modify this for my theme. Could I send you the product.liquid file and have you alter it to make it work?

@roboe415

This comment has been minimized.

Copy link

@roboe415 roboe415 commented Aug 1, 2016

gistfile2.html works. It will only display the images assigned to the same alt tag.

gistfile1.html however, is not fading in or swapping out the thumbnails at the bottom, only if you refresh the page you see the thumbs assigned.

Any advice on this?

@vijay1094

This comment has been minimized.

Copy link

@vijay1094 vijay1094 commented Apr 28, 2018

This code not work in product.liquid so please help me.

{% if section.settings.image_layout == "stacked" %}
{% comment %}
Default or stacked layout
{% endcomment %}

      {% comment %}
        We need to figure out the max width we want the image to be on the page
        based on the aspect ratio of the image and based on the size of the
        image.
      {% endcomment %}
      <div class="product-single__photos">
        {% capture img_id_class %}product-single__photo-{{ featured_image.id }}{% endcapture %}
        {% capture wrapper_id %}ProductImageWrapper-{{ featured_image.id }}{% endcapture %}

        {% comment %}
          Display current variant image
        {% endcomment %}
        <div class="product-single__photo--flex-wrapper">
          <div class="product-single__photo--flex">
            {% include 'image-style' with image: featured_image, width: 575, height: 850, small_style: true, wrapper_id: wrapper_id, img_id_class: img_id_class %}
            <div id="{{ wrapper_id }}" class="product-single__photo--container">
              <div class="product-single__photo-wrapper" style="padding-top:{{ 1 | divided_by: featured_image.aspect_ratio | times: 100}}%;">
                {% assign img_url = featured_image | img_url: '1x1' | replace: '_1x1.', '_{width}x.' %}
                {% assign featured_image = current_variant.featured_image | default: product.featured_image %}
                <img class="product-single__photo lazyload {{ img_id_class }}"
                  src="{{ featured_image | img_url: '300x300' }}"
                  data-src="{{ img_url }}"
                  data-widths="[180, 360, 590, 720, 900, 1080, 1296, 1512, 1728, 2048]"
                  data-aspectratio="{{ featured_image.aspect_ratio }}"
                  data-sizes="auto"
                  {% if section.settings.zoom_enable %}data-mfp-src="{{ featured_image | img_url: '1024x1024' }}"{% endif %}
                  data-image-id="{{ featured_image.id }}"
                  alt="{{ featured_image.alt | escape }}">

                <noscript>
                  <img class="product-single__photo"
                    src="{{ featured_image | img_url: 'master' }}"
                    {% if section.settings.zoom_enable %}data-mfp-src="{{ featured_image | img_url: '1024x1024' }}"{% endif %}
                    alt="{{ featured_image.alt | escape }}" data-image-id="{{ featured_image.id }}">
                </noscript>
              </div>
            </div>
          </div>
        </div>

        {% comment %}
          Display rest of product images, not repeating the featured one
        {% endcomment %}
        {% assign featured_alt = product.selected_or_first_available_variant.option1 %}
        {% for image in product.images %}
        {% if image.alt == featured_alt or image == featured_image %}
          {% unless image contains featured_image %}

            {% comment %}
              We need to figure out the max width we want the image to be on the page
              based on the aspect ratio of the image and based on the size of the
              image.
            {% endcomment %}
            {% capture img_id_class %}product-single__photo-{{ image.id }}{% endcapture %}
            {% capture wrapper_id %}ProductImageWrapper-{{ image.id }}{% endcapture %}

            <div class="product-single__photo--flex-wrapper">
              <div class="product-single__photo--flex">
                {% include 'image-style' with image: image, width: 575, height: 850, small_style: true, wrapper_id: wrapper_id, img_id_class: img_id_class %}
                <div id="{{ wrapper_id }}" class="product-single__photo--container">
                  <div class="product-single__photo-wrapper" style="padding-top:{{ 1 | divided_by: image.aspect_ratio | times: 100}}%;">
                    {% assign img_url = image | img_url: '1x1' | replace: '_1x1.', '_{width}x.' %}
                    <img class="product-single__photo lazyload {{ img_id_class }}"
                      src="{{ image | img_url: '300x' }}"
                      data-src="{{ img_url }}"
                      data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]"
                      data-aspectratio="{{ image.aspect_ratio }}"
                      data-sizes="auto"
                      {% if section.settings.zoom_enable %}data-mfp-src="{{ image.src | img_url: '1024x1024' }}"{% endif %}
                      data-image-id="{{ image.id }}"
                      alt="{{ image.alt | escape }}">

                    <noscript>
                      <img class="product-single__photo" src="{{ image.src | img_url: 'master' }}"
                        {% if section.settings.zoom_enable %}data-mfp-src="{{ image.src | img_url: '1024x1024' }}"{% endif %}
                        alt="{{ image.alt | escape }}"
                        data-image-id="{{ image.id }}">
                    </noscript>
                  </div>
                </div>
              </div>
            </div>
          {% endunless %}
        {% endif %}
        {% endfor %}

      </div>

    {% else %} 
@neezybk

This comment has been minimized.

Copy link

@neezybk neezybk commented Oct 20, 2019

I got this to work for me, but the page is not automatically refreshing. Can anyone help me out with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment