On Shopify, check if a given product is not on any collection

Viewed 52

Within the product page, I would like to hide a certain element if the current product isn't associated with any collection. Is there any way to check it with Liquid ?

2 Answers

The object product.collections is an array containing all collections where the product is listed.

So you may use this:

{% if product.collections.size < 1 %}
    Do something
{% endif %}

Documentation about arrays here: https://shopify.dev/api/liquid/filters#size

{% if 0 == product.collections | size %}
 !! put your code here !!
{% endif %}

Or

{% for _ in  product.collections   %}
{% else %}
 !! put your code here !!
{% endif %}
Related