Liquid loop for stock count on product page

I’d like to show a few different messages on my product page using the stock count but I’m afraid my current liquid loop isn’t quite working. I’m sure it’s probably something simple but I can’t figure it out right now. Essentially I’d like to show the following:

More than 5 = Show normal stock count message

More than 1 and 5 or less = Show last few in stock!

Exactly 1 = Show Last one message

Zero = Show out of stock message

Can someone more capable with liquid statements help me sense check what I’ve done wrong here?

The loop I’ve got so far is:


	{% comment %} More than 5 in stock {% endcomment %}
	{% if current_variant.inventory_quantity > 5 %}
	

	{% if current_variant.available %}
		{{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }}
	{% endif %}
	

	{% comment %} Greater than 1 and less than 5 in stock {% endcomment %}
	{% elsif current_variant.inventory_quantity > 1 or current_variant.inventory_quantity < 5 %}
	

	{% if current_variant.available %}
		Only {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }} - Don't miss out!
	{% endif %}
	

	{% comment %} If there's only 1 left {% endcomment %}
	{% elsif current_variant.inventory_quantity == 1 %}
	

**Last one in stock** – Act fast!

	{% comment %} Otherwise show as out of stock {% endcomment %}
	{% else %}
	

Currently out of stock

	{% endif %}

Many thanks

What’s not working? Can you describe it?

Sorry, the bit that’s not working is the last two.

When I have a product with 1 item left in stock, it’s still rendering the text from the second statement (“Only 1 in stock - don’t miss out”) instead of saying “Last one in stock - act fast”

When there is 0 stock, it’s not rendering that text out at all.

Ok, there are a couple of problems in your script logic.

  1. First IF: the condition should be >= 5 and not > 5
  2. Second IF: you need to use and instead of or otherwise everything less than 5 will follow in this condition

Here’s the code reworked:

{% if current_variant.inventory_quantity >= 5 %}

    {% if current_variant.available %}
    {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }}
    {% endif %}

{% comment %} Greater than 1 and less than 5 in stock {% endcomment %}
{% elsif current_variant.inventory_quantity > 1 and current_variant.inventory_quantity < 5 %}

    {% if current_variant.available %}
    Only {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }} - Don't miss out!
    {% endif %}

{% comment %} If there's only 1 left {% endcomment %}
{% elsif current_variant.inventory_quantity == 1 %}

<b>Last one in
        stock</b> – Act fast!

{% comment %} Otherwise show as out of stock {% endcomment %}
{% else %}

Currently out
    of stock

{% endif %}

Thanks for that @drakedev works great. Appreciate your help :slightly_smiling_face:

Nice, happy to help.