Can you know the count of your current sold out products

Solved
DanielJ2
Tourist
10 0 1

Hello! I urgently need the count of the products in my store that says sold out on them. Any help would be greatly appreciated. Thanks!

0 Likes
Alan15
Pathfinder
93 21 25

This is an accepted solution.

If you just need to display the count on the screen, you can try adding this bit of liquid code to your page:

{% assign count = 0 %}
{% for product in collections.all.products %}
    {% if product.available == false %} 
  		{% assign count = count | plus: 1 %}
    {% endif %}
{% endfor %}
{{ count }}
 
0 Likes
DanielJ2
Tourist
10 0 1

Hello. I cant seem to figure out where exactly to add this code.

0 Likes
Alan15
Pathfinder
93 21 25

This is an accepted solution.

It depends where you want the message to appear.

If you are using the Debut theme, for example, and you want the number to show in the header of your site, then add the code in the header.liquid file in the Sections folder; if it's on the product page, then it would go in the product-template.liquid. You will need to experiment with the exact positioning and appearance.

0 Likes
DanielJ2
Tourist
10 0 1

Hey man this works perfectly but the number includes the variants also for example I have 5(19 variants altogether) products that says sold out on them but the code calculates 19. Is it possible to show products only without the variants in them. Thanks a lot!

0 Likes
Alan15
Pathfinder
93 21 25

This is an accepted solution.

Hi,

That that doesn't happen for me; I don't know why it would be counting the Product variants. Is there anything unusual in the setup of your variants?

This is what the documentation says about the the product.available attribute used in the code:

product.available
Returns true if a product is available for purchase. Returns false if all of the products variants' inventory_quantity values are zero or less, and their inventory_policy is not set to "Allow users to purchase this item, even if it is no longer in stock."

Maybe someone else has some ideas, I could be missing something obvious.