Reputation: 325
I'm trying to display the sizes of the products on the product card so that the customer won't have to click on the product to find out which sizes are in stock. If the size is in stock, the size will have different styling to the sizes without stock.
What I have at the moment:
{% assign sizes = '' %}
{% for variant in product.variants %}
{% assign sizes = sizes | append: variant.options[0] | append: '_' %}
{% endfor %}
{% assign sizesArr = sizes | split: '_' | uniq %}
{% for size in sizesArr %}
<span>{{ size }}</span>
{% endfor %}
This shows all the sizes of the product. The problem is the code doesn't distinguish between the sizes that have stock and those that don't, so I can't change the styling between the two.
Thanks in advance.
Upvotes: 1
Views: 1488
Reputation: 3913
This cheat sheet will help you, shopify Cheat Sheet
There is variant.inventory_quantity
will you need to use to determine the stock.
{% for variant in product.variants %}
{% assign sizes = sizes | append: variant.options[0] | append: '_' %}
{% endfor %}
you need to check if a product has variants or not. then proceed when there is a variant. because there can be multiple product variants option.
{% for variant in product.variants %}
{% for option in variant.options %}
my stock is {{option.inventory_quantity}}
{% endfor %}
{% endfor %}
Upvotes: 1