Skip to content

Instantly share code, notes, and snippets.

@kyleaparker
Last active July 16, 2020 21:59
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kyleaparker/560a3847860bace1d680 to your computer and use it in GitHub Desktop.
Save kyleaparker/560a3847860bace1d680 to your computer and use it in GitHub Desktop.
[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 -->
@vijay1094
Copy link

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
Copy link

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