I've created a way to filter products in my Shopify shop by size on any given collection page. However, it only works when size is the only variant.
For my shop that's fine, but since I've open sourced the filter, I'd like it to work for just one variant even when there are more than one variants.
The trouble seems to be that a line like {% for variant in product.variants %}
works for just one present variant (e.g. size), but I cannot seem to pass a size argument such as {% for variant in product.variants.size %}
.
I even tried the recommended variant.option1
(i.e. {% for variant in product.variants.size.option %}
) but to no avail.
Is there some way of outputting just an array of the size variants even when there are multiple variants for a product?
Also, found this similar question, but it was not similar enough to provide a solution for my use case.
You can use
{{ sizes }}
now as a comma separated list, alternatively swap out the comma for html, for example:Then you could output the size list items as a ul using
<ul>{{sizes}}</ul>
.Finally if you really did need the sizes as an array you could use split
{% assign sizes_array = sizes | split: ',' }}
looking at the size filter and the forloops "range" parameter may present a way to loop through that.