Ad

Shopify Loop - Skipping A Product Variant If Variant Has No Image

- 1 answer

I need to do two things; add products with extra colours to the collections loop (which I've done below, via a snippet elsewhere), however I need to skip when the variant doesn't have its own image which I've yet to figure out.

The break I attempted is in the preview code:

{% for option in product.options %}

   {% if option == 'Colour' %}
   {% assign index = forloop.index0 %}
   {% assign colourlist = '' %}
   {% assign colour = '' %}
   {% for variant in product.variants %}
   {% capture colour %}
   {{ variant.options[index] }}
   {% endcapture %}

   {% if variant.image.src %}
    {% break %}
   {% endif %}

   {% unless colourlist contains colour %}

          {% include 'product-grid-item' %}

        {% capture tempList %}
      {{colourlist | append: colour | append: " " }}
      {% endcapture %}
      {% assign colourlist = tempList %}
      {% endunless %}
      {% endfor %}
  {% endif %}


      {% else %}

        <div class="grid-item">
          <p>{{ 'collections.results.no_products' | t }}</p>
        </div>

  {% endfor %}

{% endfor %}
Ad

Answer

The keyword you're looking for, to skip the current iteration of the loop and move to the next, is {% continue %}

For example:

{% for variant in product.variants %}
  {% if variant.featured_image == blank %}
     {% continue %}
  {% endif %}
  <!-- HTML STUFF -->
{% endfor %}
Ad
source: stackoverflow.com
Ad