Start your cross-border journey
Contact us now

Website development customer service

Customer Service

Designer (making Figma prototypes)

Shopify Product Variants Image Grouping

1, into the code background, in the snippets inside to find product-media.liquid

Add code:

{% if current_variant_color == media.alt or media.alt == 'all' %}
data-matched
{% endif %}

2, in assets to find global.js

Add code:

updateMediaBasedOnVariant() {

    const mediaGallery = document.getElementById(

      `MediaGallery-${this.dataset.section}`

    );

    const options = this.currentVariant.options.map((option) =>

      option.toLowerCase()

    );

    const mediaWrapper = Array.from(

      mediaGallery.querySelectorAll(".product__media-item")

    );

    const mediaThumbnailWrapper = mediaGallery.querySelectorAll(

      ".thumbnail-list__item"

    );

    const mediaModal = document.querySelectorAll(

      ".product-media-modal__content img"

    );

    // Set the thumbnail display by default

    mediaThumbnailWrapper.forEach((media) => {

      media.removeAttribute('data-matched');

    });

    // Set the media modal display by default

    mediaModal.forEach((media) => {

      media.removeAttribute('data-matched');

    });

    const matchesMedia = mediaWrapper.filter((media) => {

      media.removeAttribute('data-matched');

      const alt = media.getAttribute("data-media-alt");

      const isMatch = alt === "all" || this.currentVariant.options[0] === alt;

      if (isMatch) {

        const mediaId = media.getAttribute("data-media-id");

        const id = mediaId.split("-").pop();

        media.setAttribute("data-matched", true); media.setAttribute("data-matched", true); Media.

        const thumbnail = mediaGallery.querySelector(

          `[data-target="${mediaId}"]`

        );

        thumbnail && (thumbnail.style.display = "block");

        thumbnail && thumbnail.setAttribute("data-matched", true);

        const modal = document.querySelector(

          `.product-media-modal__content img[data-media-id="${id}"]`

        );

        modal && (modal.setAttribute("data-matched", true));

      }

      return isMatch.

    });

    customElements.whenDefined("media-gallery").then(() => {

      mediaGallery.setActiveMedia(

        matchesMedia[0].getAttribute("data-media-id"),

        true

      );

    });

    // Reset the slider

    customElements.whenDefined("slider-component").then(() => {

      const sliders = mediaGallery.querySelectorAll("slider-component");

      sliders.forEach((slider) => {

        slider.resetPages();

      });

    });

  }

3, in the assets inside to find section-main-product.css

Code 1.

.product-thumbnail_slider .product__media-item:not([data-matched]) {
display: none !important;
}

Code 2.

.thumbnail-list.slider-tablet-up .thumbnail-list__item.slider__slide:not([data-matched]) {
display: none !important;
}

.product-media-modal__content>img:not([data-matched]) {
display: none !important;
}