Solved

Liquid loop for stock count on product page

welcomebrand
Shopify Partner
72 1 110

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: 

<div style="outline:5px solid red;padding:10px;">

	{% comment %} More than 5 in stock {% endcomment %}
	{% if current_variant.inventory_quantity > 5 %}
	<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color:pink">
	{% if current_variant.available %}
		{{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }}
	{% endif %}
	</p>

	{% comment %} Greater than 1 and less than 5 in stock {% endcomment %}
	{% elsif current_variant.inventory_quantity > 1 or current_variant.inventory_quantity < 5 %}
	<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color:blue">
	{% if current_variant.available %}
		Only {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }} - Don't miss out!
	{% endif %}
	</p>

	{% comment %} If there's only 1 left {% endcomment %}
	{% elsif current_variant.inventory_quantity == 1 %}
	<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color: green;"><b>Last one in stock</b> &ndash; Act fast!</p>

	{% comment %} Otherwise show as out of stock {% endcomment %}
	{% else %}
	<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color: orange;">Currently out of stock</p>

	{% endif %}
</div>

 

 

Many thanks 

Accepted Solution (1)
drakedev
Shopify Partner
685 148 230

This is an accepted solution.

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 %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color:pink">
    {% if current_variant.available %}
    {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }}
    {% endif %}
</p>

{% comment %} Greater than 1 and less than 5 in stock {% endcomment %}
{% elsif current_variant.inventory_quantity > 1 and current_variant.inventory_quantity < 5 %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color:blue">
    {% if current_variant.available %}
    Only {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }} - Don't miss out!
    {% endif %}
</p>

{% comment %} If there's only 1 left {% endcomment %}
{% elsif current_variant.inventory_quantity == 1 %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color: green;"><b>Last one in
        stock</b> &ndash; Act fast!</p>

{% comment %} Otherwise show as out of stock {% endcomment %}
{% else %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color: orange;">Currently out
    of stock</p>

{% endif %}
</div>

 

If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.

View solution in original post

Replies 5 (5)

drakedev
Shopify Partner
685 148 230

What's not working? Can you describe it?

If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.
welcomebrand
Shopify Partner
72 1 110

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. 

drakedev
Shopify Partner
685 148 230

This is an accepted solution.

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 %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color:pink">
    {% if current_variant.available %}
    {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }}
    {% endif %}
</p>

{% comment %} Greater than 1 and less than 5 in stock {% endcomment %}
{% elsif current_variant.inventory_quantity > 1 and current_variant.inventory_quantity < 5 %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color:blue">
    {% if current_variant.available %}
    Only {{ 'products.product.stock_label' | t: count: current_variant.inventory_quantity }} - Don't miss out!
    {% endif %}
</p>

{% comment %} If there's only 1 left {% endcomment %}
{% elsif current_variant.inventory_quantity == 1 %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color: green;"><b>Last one in
        stock</b> &ndash; Act fast!</p>

{% comment %} Otherwise show as out of stock {% endcomment %}
{% else %}
<p id="ProductInventory-{{ section_id }}" class="container-product-lead-inventory" style="color: orange;">Currently out
    of stock</p>

{% endif %}
</div>

 

If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.
welcomebrand
Shopify Partner
72 1 110

Thanks for that @drakedev works great. Appreciate your help 🙂

drakedev
Shopify Partner
685 148 230

Nice, happy to help.

If my answer was helpful click Like to say thanks
If the problem is solved remember to click Accept Solution
Shopify/Shopify Plus custom development: You can hire me for simple and/or complex tasks.