Show sizes in product title if 2 or less are left

Highlighted
New Member
5 0 0

We are using this method on our store and everything is working: https://community.shopify.com/c/Shopify-Design/Show-available-sizes-without-viewing-product/m-p/2351...

 

I am wondering if anyone knows how to only show the sizes available IF there are 2 or less sizes.

 

Currently all the sizes available are showing on the collection pages using the code below but for ALL products with more than one size. We only want the sizes to show when a product has 2 sizes that have stock available. What do I need to add to the code to get this to work properly?

 

{% assign sizes = '' %}
{% for variant in product.variants %}
{% if variant.inventory_quantity != 0 %}
{% assign sizes = sizes | append: variant.options[0] | append: '_' %}
{% endif %}
{% endfor %}

{% if product.options.size > 1 %}
{% assign sizesArr = sizes | split: '_' | join: ", "| uniq %}
{% for size in sizesArr %}
- Size <span>{{ size }}</span> Available
{% endfor %}
{% endif %}

0 Likes