Check if a collection exists and has products
There are 5 collection settings. They are all set.
- "collection_1" is set to "non-empty-collection", a published collection with products.
- "collection_2" is set to "empty-collection", a published collection with no products in it.
- "collection_3" is set to "non-empty-collection-that-is-hidden-from-online-store", a collection that has products but is hidden.
- "collection_4" is set to "empty-hidden-collection", a collection that is empty and hidden.
- "collection_5" is set to "deleted-collection", a collection that was selected in the theme settings at one point, but has since been deleted from the store.
Hidden collections are like non-existent collections, except they can still be selected in the theme settings, so you must use unless == blank
to make sure the collection exists and is published, before you access it.
Liquid
<h4>Good way, using == blank check</h4>
{% for i in (1..5) %}
{% capture key %}collection_{{ i }}{% endcapture %}
{% unless settings[key] == blank or collections[settings[key]] == blank or collections[settings[key]].products_count == 0 %}
{{ collections[settings[key]].title }} has {{ collections[settings[key]].products_count }} {{ collections[settings[key]].products_count | pluralize: 'product', 'products' }}<br>
{% endunless %}
{% endfor %}
<h4>Good way, using != blank check</h4>
{% for i in (1..5) %}
{% capture key %}collection_{{ i }}{% endcapture %}
{% if settings[key] != blank and collections[settings[key]] != blank and collections[settings[key]].products_count != 0 %}
{{ collections[settings[key]].title }} has {{ collections[settings[key]].products_count }} {{ collections[settings[key]].products_count | pluralize: 'product', 'products' }}<br>
{% endif %}
{% endfor %}
<h4>Another good way, using empty? check</h4>
{% for i in (1..5) %}
{% capture key %}collection_{{ i }}{% endcapture %}
{% unless settings[key] == blank or collections[settings[key]].empty? or collections[settings[key]].products_count == 0 %}
{{ collections[settings[key]].title }} has {{ collections[settings[key]].products_count }} {{ collections[settings[key]].products_count | pluralize: 'product', 'products' }}<br>
{% endunless %}
{% endfor %}
<h4>Bad way, not using == blank check</h4>
{% for i in (1..5) %}
{% capture key %}collection_{{ i }}{% endcapture %}
{% unless settings[key] == blank or collections[settings[key]].products_count == 0 %}
{{ collections[settings[key]].title }} has {{ collections[settings[key]].products_count }} {{ collections[settings[key]].products_count | pluralize: 'product', 'products' }}<br>
{% endunless %}
{% endfor %}
<h4>Bad way, expecting non-existent or hidden resource to be nil so falsy</h4>
{% for i in (1..5) %}
{% capture key %}collection_{{ i }}{% endcapture %}
{% if settings[key] != blank and collections[settings[key]] and collections[settings[key]].products_count != 0 %}
{{ collections[settings[key]].title }} has {{ collections[settings[key]].products_count }} {{ collections[settings[key]].products_count | pluralize: 'product', 'products' }}<br>
{% endif %}
{% endfor %}
<h4>The following collections are either hidden or don't exist</h4>
{% for i in (1..5) %}
{% capture key %}collection_{{ i }}{% endcapture %}
{% if settings[key] != blank and collections[settings[key]] == blank %}
{{ settings[key] | replace: '-', ' ' | capitalize }}<br>
{% endif %}
{% endfor %}
Output
Good way, using == blank check
Non-empty collection has 3 products
Good way, using != blank check
Non-empty collection has 3 products
Another good way, using empty? check
Non-empty collection has 3 products
Bad way, not using == blank check
Non-empty collection has 3 products
Bad way, expecting non-existent or hidden resource to be nil so falsy
Non-empty collection has 3 products
The following collections are either hidden or don't exist