Re: Get inventory quantity for line items in cart

Get inventory quantity for line items in cart

anish_soni
Shopify Partner
8 0 0

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".

Replies 6 (6)

playwright-mike
Shopify Partner
72 18 33

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

Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.

anish_soni
Shopify Partner
8 0 0

Thanks for your reply but I want to fetch the stock of line item not line item quantity.

playwright-mike
Shopify Partner
72 18 33

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.

Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.

Kumar2573
Shopify Partner
171 8 21
Shopify Certified Developer Profile - https://www.credly.com/badges/1733d05c-2a88-490a-aef5-b01baf3b94ce/public_url
Expert Shopify Plus & shopify Developer & Theme customizer
- If the problem is solved remember to click to Accept Solution
- Press Like ?? if the answer was helpful
- Still, Facing issue, Reach out to me Email :- jitendra3496@gmail.com | Skype :- jitendra2573_1
- Direct Hire me :- Direct Contact me
JeffSpurlock
Shopify Partner
10 0 13

Yes, that works for rendering it in html/liquid template, but is not a valid method in checkout scripts. 

JeffSpurlock
Shopify Partner
10 0 13

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