carolineschnapp
2/21/2012 - 7:13 PM

When filtering a collection by size, say, 'Small', that is the code to use to hide products which 'Small' variants are out of stock.

When filtering a collection by size, say, 'Small', that is the code to use to hide products which 'Small' variants are out of stock.

{% for product in collection.products %}

  {% assign hide_product = false %}

  {% assign size_index = 0 %}
  {% for option in product.options %}
    {% if option == 'Size' %}
      {% assign size_index = forloop.index0 %}
    {% endif %}
  {% endfor %}

  {% assign filter_by_size = false %}
  {% assign filtered_size = '' %}
  {% for tag in current_tags %}
    {% if tag == 'Small' or tag == 'Medium' or tag == 'Large' %}
      {% assign filter_by_size = true %}
      {% assign filtered_size = tag %}
    {% endif %}
  {% endfor %}

  {% if filter_by_size %}
    {% assign at_least_one_item_in_stock = false %}
    {% for variant in product.variants %}
      {% if variant.options[size_index] == filtered_size and variant.available %}
        {% assign at_least_one_item_in_stock = true %}
      {% endif %}
    {% endfor %}
    {% unless at_least_one_item_in_stock %}
      {% assign hide_product = true %}
    {% endunless %}
  {% endif %}
  
  {% if product.available %}
  {% unless hide_product %}
  
  [ LIQUID AND HTML FOR YOUR PRODUCT ]  
  
  {% endunless %}
  {% endif %}

{% endfor %}