Skip to content

Instantly share code, notes, and snippets.

@carolineschnapp
Created February 21, 2012 19:13
Show Gist options
  • Save carolineschnapp/1878238 to your computer and use it in GitHub Desktop.
Save carolineschnapp/1878238 to your computer and use it in GitHub Desktop.
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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment