Re: Check if a collection has products or not?

Solved

Check if a collection has products or not?

Dchan04
Excursionist
24 0 4

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. 

Accepted Solution (1)

PageFly-Kate
Shopify Partner
1203 346 361

This is an accepted solution.

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 %}
  <p>This collection has no active products.</p>
  <a href="/" class="btn">Return to Homepage</a>
{% 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

Please let me know if it works by giving it a Like or marking it as a solution!


➜ Optimize your Shopify store with PageFly Page Builder (Free plan available) 
➜ Weekly updated Shopify tutorials on YouTube 


All features are available from Free plan. Live Chat Support is available 24/7.

View solution in original post

Replies 3 (3)

PageFly-Kate
Shopify Partner
1203 346 361

This is an accepted solution.

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 %}
  <p>This collection has no active products.</p>
  <a href="/" class="btn">Return to Homepage</a>
{% 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

Please let me know if it works by giving it a Like or marking it as a solution!


➜ Optimize your Shopify store with PageFly Page Builder (Free plan available) 
➜ Weekly updated Shopify tutorials on YouTube 


All features are available from Free plan. Live Chat Support is available 24/7.

Dchan04
Excursionist
24 0 4

This worked, thanks so much!

mreynolds
Excursionist
35 0 5

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