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

I currently have this code on my products -

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

{{ product.selected_or_first_available_variant.inventory_quantity }} left in stock


{% 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!

@izzy365

Please try this code

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

    {{ product.selected_variant.inventory_quantity }} left in stock
  

{% endif %}

Thanks!

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 %}
  

    {{ product.selected_variant.inventory_quantity }} left in stock
  

{% endif %}

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

Best Regards,
Moeed

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”

Hi.

Thank you so much for your help.

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

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.

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.