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 4 (4)
playwright-matt
Shopify Partner
72 18 27

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-matt
Shopify Partner
72 18 27

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
124 6 12
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