Commencez votre voyage transfrontalier
Contactez-nous

Développement de sites web, service à la clientèle

Service clientèle

Concepteur (réalisation de prototypes Figma)

Shopify Product Variants Image Grouping

1, dans l'arrière-plan du code, dans les snippets à l'intérieur pour trouver product-media.liquid

Ajouter un code :

{% if current_variant_color == media.alt or media.alt == 'all' %}
données appariées
{% endif %}

2, dans les actifs pour trouver global.js

Ajouter un 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"

    );

    // Définir l'affichage des vignettes par défaut

    mediaThumbnailWrapper.forEach((media) => {

      media.removeAttribute('data-matched') ;

    });

    // Définir l'affichage modal des médias par défaut

    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"),

        vrai

      );

    });

    // Remise à zéro du curseur

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

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

      sliders.forEach((slider) => {

        slider.resetPages() ;

      });

    });

  }

3, dans les actifs, trouver 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 ;
}