Cart Item Available Quantity in Inventory

Highlighted
New Member
3 0 0

Hello, Good Day fellow developers...
I need some sort of advice about this Cart API.

Okay, here is the thing. I am trying to get the Quantity Available of a product that is already added inside the Cart,
for 2 weeks of searching I have read some post saying that "inventory_quantity" has been deprecated already, and they're gave me this "Inventory API".
Now my problem is every time I tried requesting to this "Inventory API", I'm getting an error 

"Access to XMLHttpRequest at 'https://******.myshopify.com/admin/api/2019-07/inventory_levels.json?inventory_item_ids=21108251164770' from origin 'https://******.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

I don't understand a thing about this thing because I just started developing our store few months ago,
hopefully there is someone here can answer me about this matter.

by the way I am using Jquery (Javascript) as a developing language

0 Likes
Highlighted
Shopify Staff
Shopify Staff
619 48 94

Hi @globalxmart,

 

If you're making calls to the admin API, you will need to do so from the server side, rather than client side. The Admin API is an authenticated API and should never be used from the storefront.

 

If you are only concerned with whether an item is available to purchase, you can use the AJAX API on the storefront to see the available boolean, which indicates whether the item is available (in stock) to be purchased.

 

Hope that helps give you some context!

0 Likes
Highlighted
New Member
3 0 0

Hi Thanks for the reply...

My concern is for "Changing the Cart Item Qty" right now I am able to change the Cart Item Qty from 1 to 10, but it seems that when that product is already inside the cart when I try to change it's qty to 10 even though the available product qty for that item is 8...
So what I am trying to do is make an "SELECT TAG" ranges from minimum 1 up to "Max product qty available"...

 

my Question is how will I know or get the "Max product qty available" ?
by the way I am creating a cart popup not a cart page that's why I need API...

0 Likes
Highlighted
Shopify Staff
Shopify Staff
619 48 94

Got it. You would need to make authenticated calls to the inventory level API from a backend app in that case. Inventory isn't exposed in the front-end.

0 Likes
Highlighted
New Member
1 0 0

Hi you do not need to access the Inventory API from the backend as suggested. The "inventory_quantity" was only deprecated from the AJAX API however it is still available to your liquid templates( {{ variant.inventory_quantity }} ). So long as you are accessing some product variant on the page you are building your cart popup you can also store the associated inventory level for that product variant. You can write a script in the liquid template that stores the product information you require into an object. Once the page loads you can simply access the javascript object to extract the data and build the cart as you desire. Have you already found a solution for this? I'm happy to go further into this and provide an example if required. Write back if you want to understand more about this method.

Regards. 

0 Likes
Highlighted
Tourist
7 1 0

Hi i have created minicart page, it likes this image. User can remove item from page or change quantity interactivly, i am populating minicart page with data from cart.js, but i dont have access to inventory quantites i dont know how can i control if user adds more items to the cart then are in inventory,

 

{%- assign cart_items_count = cart.items | size -%}
var cartItemMaxQuantities = {
  {%- for item in cart.items -%}
  "{{ item.id }}": {{- item.variant.inventory_quantity -}}
  {%- unless forloop.index == cart_items_count -%},{%- endunless -%}
  {%- endfor -%}
};

this code i have added to cart-template.liquid file, it stores data about maximal quantites,
but this is usless because user can add new items to minicart or remove items from there, so i need to
re render that portion of that code when cart data is changed, is there any way to do that

 

0 Likes
Highlighted
New Member
1 0 0

I'm running into the same problem here. i added a shipping rate called "Local Delivery". I was able to add a label with a warning to the product and cart pages that local delivery isn't available for items that we don't have in stock, and instead will be dropshipped. But it looks like i'm not able to do that for the javascript Drawer Cart. We have items set to sell even when they are out of stock, so "available" is always true.

0 Likes
Highlighted