Shopify: How to get the size of a metafield list value
If you are enjoying using Shopify’s metafields you’ve possibly come across the need to check the number of items in a reference list type by now, only to find the typical .size
property returns null on products or collections.
Buried in the paginate
tag’s docs it mentions that for collection and product lists, you should use paginate on those values.
This means if you paginate the array, you can then access the paginate.items
property within the paginate tags, like this:-
{%- paginate article.metafields.related.products.value by 25 -%}
Total number of related products: {{ paginate.items }}
{%- endpaginate -%}
This can be very useful if you want to present a header where you show the singular or plural name for the items, for this you can use the pluralize filter like this:-
{{ paginate.items | pluralize: 'Product', 'Products' }}
If you found this useful maybe drop me a tip so I can buy a coffee — https://ko-fi.com/stephenkeable