Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
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.
<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]
I needed to do exactly this on product pages in order to display this:
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!