Personalized checkout and custom promotions with Shopify Scripts
Is there any way to fetch the inventory quantity of cart line items on the script editor. I have tried to use item.variant.inventory_quantity but it says undefined method "inventory_quantity".
Hi @anish_soni,
There is a "quantity" method directly on the line_item object. So you would call line_item.quantity
Here is a simple script that will output the variant id and quantity of each line item in the cart:
cart = Input.cart
cart.line_items.each do |line_item|
puts "#{line_item.variant.id}: #{line_item.quantity}"
end
Output.cart = cart
I hope that is helpful!
Matthew
Thanks for your reply but I want to fetch the stock of line item not line item quantity.
Ah, misread that.
It doesn't seem to be included in the variant object.
You might try passing as a line_item property, but it wouldn't necessarily be up-to-date. I imagine it would be the stock quantity of the variant the moment it was added to the cart. So that might not work for your scenario.
Yes, that works for rendering it in html/liquid template, but is not a valid method in checkout scripts.
Hi Anish_soni, unfortunately i wasn't able to access inventory quantities either, but i developed a workaround that works for my purposes that may work for you, but i did not need the actual quantity, i just needed to know if it was in stock or not. Here's what i did:
Using a shopify flow, i tagged the product as out-of-stock when its inventory level equals zero. then in your script, use
contains_tag = false
line_item.variant.product.tags.each do |tag|
if tag = "out-of-stock"
contains_tag=true
end
end
if contains_tag != true
{{ DO YOUR LOGIC HERE}}
end
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025