Variant Quantity Updating Code - "Only X Left in stock"

Variant Quantity Updating Code - "Only X Left in stock"

izzy365
Excursionist
14 1 3

I currently have this code on my products -


{% if product.selected_or_first_available_variant.inventory_quantity < 3 %}

<p style="color: black"> {{ product.selected_or_first_available_variant.inventory_quantity }} left in stock</p>
{% endif %}

 

But it only shows the correct variant quantity when you refresh the page. Is it possible to show the correct product quantity when selected? See the image where it shows the 'ruled' variant is out of stock, but the Graph has 2 left, and when you click back on ruled, it still shows '2 left in stock'. I have noticed a few customers clicking through the variants and leaving the site.

 

I tried tweaking it myself but out of my depth. I am using the Dawn theme.

 

thanks! 

 

Screenshot 2023-07-04 at 14.24.15.png

Replies 6 (6)

dmwwebartisan
Shopify Partner
12321 2552 3729

@izzy365 

Please try this code 

 

{% if product.selected_variant.inventory_quantity < 3 %}
  <p style="color: black">
    {{ product.selected_variant.inventory_quantity }} left in stock
  </p>
{% endif %}

 

Thanks!

izzy365
Excursionist
14 1 3

Hi.

 

Thank you so much for your help.

Unfortunately, this didn't work. It doesn't show the quantity.

Moeed
Shopify Partner
5808 1578 1875

Hey @izzy365 

Try to replace your code with the code mentioned below and check if this works for you

{% if product.selected_variant.inventory_quantity < 3 %}
  <p style="color: black">
    {{ product.selected_variant.inventory_quantity }} left in stock
  </p>
{% endif %}

 

If I managed to help you then, don't forget to Like it and Mark it as Solution!

 

Best Regards,
Moeed

- Get a quick Shopify quote – Click here!

- Need a Shopify Specialist? Chat on WhatsApp

- Custom Design | Advanced Coding | Store Modifications


izzy365
Excursionist
14 1 3

Hi.

 

Thank you so much for your help.

Unfortunately, this didn't work. It doesn't show any text on the product now.

It doesn't seem to recognise "product.selected_variant.inventory_quantity"

izzy365
Excursionist
14 1 3

Update, I edited the code so that it only shows up with stock quantities on products that do not have variants. If anyone ever solves this please let me know.

Zel_
Shopify Partner
25 0 1

Hello, if you are okay with using 3rd party app for this use case, you can check this application. It directly shows the left in stock in variant level.