Get stock level by product ID or handle

Topic summary

Problem: A user wants to display the inventory quantity of a specific product using its ID or handle via Liquid code on pages other than that product’s own page.

Solution Provided:

To access inventory by product handle, use:

{{ all_products['PRODUCT-HANDLE'].variants.first.inventory_quantity }}

Additional Context:

  • One response included code checking if inventory management is set to “shopify” and conditionally displaying stock levels or out-of-stock messages
  • A practical example was shared showing how to pull inventory from a product metafield (gift-with-purchase scenario) to display FOMO messaging like “Only ## gifts left” based on current stock levels
  • The solution works by accessing the first variant’s inventory_quantity property of the referenced product

Status: The question appears resolved with working code examples provided.

Summarized with AI on November 17. AI used: claude-sonnet-4-5-20250929.

How can I display stock quantity of a product by ID/Handle via liquid?

I want to display the inventory quantity of a certain product elsewhere on the site, NOT on that product page.

Something like below, but obviously that does not work at all.

{{ all_products[‘PRODUCT-HANDLE’].inventory_quantity }}

Hi @Dannooch ,

Try this code.


               {% if product.variants.first.inventory_management == "shopify" %}
                  {% if product.variants.first.inventory_quantity > 0 %}
                     We currently have {{ product.variants.first.inventory_quantity }} in stock.
                  {% else %}
                     The product is out of stock
                  {% endif %}
               {% else %}
                  This product is available
               {% endif %}
              

Output:- https://prnt.sc/MrK_oN7LxQVE

If you have any more queries or help then you can be sent mail me at aradeshanaprashant12@gmail.com

I needed to do exactly this on product pages in order to display this:

Screenshot 2023-09-05 at 10.08.42 PM.jpg

The code I used is:

{% assign gift_product = product.metafields.custom.gift_with_purchase.value %}
  {% assign gift_inventory = gift_product.variants.first.inventory_quantity %}

  {% if gift_inventory > 0 and gift_inventory < 100 %}
    
  {% elsif gift_inventory == 0 %}
    
  {% endif %}
{% endif %}

So I created a product metafield which pulled a variant that will be a gift with purchase item. I wanted to push a little FOMO when stocks drop, so the code pulls and displays the current inventory level of the gift and posts the “There are only ## gifts left” message.

In your case, you are going to use this code:

[‘PRODUCT-HANDLE’].variants.first.inventory_quantity

I hope this helps you do what you need in your store. Good luck!