Liquid, JavaScript, themes, sales channels
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!
Solved! Go to the solution
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 }}
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.
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.
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 }}
Hello. I cant seem to figure out where exactly to add this code.
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.
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!
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.
Are you ready to take your business to the next level? Look no further than the latest ...
By SarahF_Shopify Apr 15, 2024We’re keeping the ball rolling to make sure you’re always ahead of the game. So buckle ...
By JasonH Apr 8, 2024Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024