Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

javascript - Hide additional variant thumbnails on select on Shopify product page

I've build a colour swatch selector for my products on my Shopify store. The selector works perfectly except for one issue - I'm would like to hide other colour variant image thumbnails when one colour is selected.

Problem: The selector currently selects the images assigned to each product by the image's alt text - I am now uploading products by a different method that no longer allows me to manually assign image alt text to each image.

Solution: Adjust the selector to select images that are assigned to the product colour variant, not by image alt text. This way the images will only be visible to the colour variant that is selected.

Any help would be greatly appreciated; thank you.

theme.js https://pastebin.com/vq2mrmnb

_filterThumbnails: function(variant){
  
  if(variant.featured_image != null && variant.featured_image.alt != null){
    
    $('[data-thumbnail-color]').hide();
    
    //show thumbnails for selected color
    
    var selected_color = variant.featured_image.alt;
    var thumbnail_selector = '[data-thumbnail-color="' + selected_color + '"]';
    $(thumbnail_selector).show();
  }
  
  else {
   
    //show all thumbnails
    $('[data-thumbnail-color]').show();     
  }
  
},

product-template.liquid https://pastebin.com/tiPdn2ff

          {% form 'product', product, class:form_classes, novalidate: 'novalidate', data-product-form: '' %}
            {% unless product.has_only_default_variant %}
              <div class="product-form__controls-group">
                {% for option in product.options_with_values %}
                  <div class="selector-wrapper js product-form__item">  

                    {%if option.name == "Colour"%}

                    <label>{{option.name}}</label>
                    {%assign index = forloop.index %}

                    <div class="variant-swatches">
                      {%for value in option.values%}
                      <input class="single-option-selector-{{ section.id }}" id="color-{{forloop.index}}" type="radio" name="color" value="{{ value | escape }}" data-index="option{{index}}" {% if option.selected_value == value %}checked{%endif%}/>
                      <label for="color-{{forloop.index}}">
                      <img src="{{ value | escape | strip | downcase | replace: ' ', '-' | append: '.png' | asset_img_url: '300x' }}"/>
                      </label>
                      {%endfor%}
                    </div>
                    {%else%}

                    <label for="SingleOptionSelector-{{ forloop.index0 }}">
                      {{ option.name }}
                    </label>                   

                    <select class="single-option-selector single-option-selector-{{ section.id }} product-form__input"
                      id="SingleOptionSelector-{{ forloop.index0 }}"
                      data-index="option{{ forloop.index }}"
                    >
                      {% for value in option.values %}
                        <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
                      {% endfor %}
                    </select>
                    {%endif%}
                  </div>
                {% endfor %}
              </div>
            {% endunless %}

Blue selected

Red selected

Product listing

question from:https://stackoverflow.com/questions/65887844/hide-additional-variant-thumbnails-on-select-on-shopify-product-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...