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

izzy365
Tourist
8 1 1

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
12291 2547 3698

@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!

If helpful then please Like and Accept Solution | Email: dmw.webartisan@gmail.com |  Instagram: @dmw.webartisan
Check here PageFly App to customize your pages | #1 Product Filter & Search app on Shopify | The most powerful Shopify page builder app
izzy365
Tourist
8 1 1

Hi.

 

Thank you so much for your help.

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

Moeed
Shopify Partner
3697 932 1166

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

Need a Shopify developer? Chat on WhatsApp


- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
- Your Coffee Tip and my code, A perfect blend. ❤️
izzy365
Tourist
8 1 1

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
Tourist
8 1 1

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.