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
618 46 87

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
618 46 87

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