Skip to content

Instantly share code, notes, and snippets.

@aatronco
Last active March 25, 2020 12:55
Show Gist options
  • Save aatronco/c4ee3543d0817cab57ce4fc7105e402c to your computer and use it in GitHub Desktop.
Save aatronco/c4ee3543d0817cab57ce4fc7105e402c to your computer and use it in GitHub Desktop.
<!-- START - Create an Array with the selected filters -->
{% capture selected_values %}
{% for filter in filters %}
{% for value in filter.values %}
{% if value.selected %}
{{value.name }}
{% endif %}
{% endfor %}
{% endfor %}
{% endcapture %}
{% assign selected_values = selected_values | split: ' ' %}
{% assign selected_values_count = selected_values.size %}
<!-- END - Create an Array with the selected filters -->
<!-- START - On the paginated products create a rule to find one non zero STOCK of the variants that matches the Selected filters -->
{% for product in paged.products %}
{% for variant in product.variants %}
{% capture variant_values %}
{% for value in variant.values %}
{{value.name}}
{%endfor%}
{%endcapture%}
{% assign variant_values = variant_values | split: ' ' %}
{% assign filters_and_variant_values = variant_values | concat: selected_values %}
{% assign filters_and_variant_values_uniq = variant_values | concat: selected_values | uniq %}
{% assign dif_uniq = filters_and_variant_values.size | minus: filters_and_variant_values_uniq.size %}
{% assign variant_is_selected = false %}
{% if dif_uniq == selected_values_count %}
{% assign variant_is_selected = true %}
{% endif %}
{% if variant_is_selected and variant.stock > 0 %}
{% assign non_zero_stock = variant.stock %}
{%endif%}
{%endfor%}
<!-- END - On the paginated products create a rule to find a non zero STOCK sof the variants that matches the Selected filters -->
<div class="{% if filters != empty %}col-lg-4 col-md-4{% else %}col-lg-3 col-md-4{% endif%} col-6">
<div class="product-block">
<!-- START - Show Message -->
{% if non_zero_stock > 0 %}
Selected variant has stock
{%else%}
Selected variant does not have stock
{%endif%}
<!-- END - Show Message -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment