Re: Get stock level by product ID or handle

Get stock level by product ID or handle

Dannooch
Shopify Partner
1 0 0

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

Replies 2 (2)

Prashant-Patel
Shopify Partner
8 1 4

Hi @Dannooch ,

Try this code.

 

<div id="variant-inventory">
               {% 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 %}
              </div>

 

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

 

If you have any more queries or help then you can be sent mail me at [email protected] 

If helpful then please Like and Accept Solution.
Want to modify or custom changes on store Hire me.
Contact me on [email protected] regarding any help | Figma to Shopify
Shopify Theme Developer | Skype : live:apd12121998

colleenc
Shopify Partner
91 9 21

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 %}
    <l><i class="fa fa-gift" aria-hidden="true"></i> There are only {{ gift_inventory }} gifts left</l>
  {% elsif gift_inventory == 0 %}
    <l>Gift is Sold Out.</l>
  {% 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!