Re: Available quantity when modfying line items with useCart

Available quantity when modfying line items with useCart

FlorianWalther
Shopify Partner
3 0 1

I use React-Hydrogen. The cart it fetches and exposes via the useCart hook doesn't contain the available quantity for the variants of a product. When I try to update the line item via 

linesUpdate with an invalid quantity, it just returns a 200 response with the maximum available quantity applied (for example, if I try to set the quantity of a product to 3, but only 2 are available, it returns the new cart with a quantity of 2 applied).
 
How can I show some kind of error message in my app saying that the requested quantity isn't available, by using the data present in the cart? 
Replies 3 (3)

Liam
Shopify Staff
2873 312 819

Unfortunately, the Shopify Storefront API doesn provide a direct way to get the available quantity of a product variant, so you'll likely need to use the Admin API to handle this separately. 

 

An example approach could be:

  1. Keep track of the requested quantity in your own app using the admin API Inventory Item object.

  2. When you get the response from the linesUpdate mutation, compare the new quantity with the requested quantity.

  3. If the quantity in a cart is greater than the current quantity of the product, show an error message to the user indicating that the requested quantity isn't available.

Would this approach work?

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

FlorianWalther
Shopify Partner
3 0 1
Thank you for your answer!
 

@Liam wrote:

Unfortunately, the Shopify Storefront API doesn provide a direct way to get the available quantity of a product variant, so you'll likely need to use the Admin API to handle this separately. 


Are you sure about this? When I fetch a product directly from the Storefront API, I can get quantityAvailable and availableForSale.
 
The problem is that they are not inside the cart data.
moritzjacobs
Shopify Partner
1 0 0

I can confirm this from my side. If you ever find a workaround, please reply 🙂