Check if a collection has products or not?

I am trying to code in custom edits to my site www.shopinhouse.com. On my site, I have certain collection pages with no active products (ie https://www.shopinhouse.com/collections/chevy-woods). I want it so that if there are collection pages with no active products, it will render text and a button. I am reaching out to see what code/keywords I should use to check if a collection has active products. I tried doing {% if collection.product.available == false %} but that did not work. Any advice is appreciated.

Hi @Dchan04 ,

This is Kate from PageFly - Landing page builder,

You can refer to the idea of ​​the code as below to check if a collection has any products:

{% if collection.products.size > 0 %}
  {% for product in collection.products %}
    {{ product.title }}
  {% endfor %}
{% else %}
  

This collection has no active products.

  Return to Homepage
{% endif %}

Here, the code checks the size of the collection’s products array. If it’s greater than zero, the code will loop through each product in the collection and display its title. If the collection has no products, it will display the specified text and button.

Hope my answer will help you

1 Like

This worked, thanks so much!

Hello Kate,

I have a similar issue but need a different outcome. Is there a way that if a collection has no products, I can be notified via email what collections are empty?

Thank you, Margie