Updating quantity with change.js, how to know if max quantity is reached?

Darkbound
Tourist
7 0 0

Hey guys, I am working on a cart and I am trying to determine if a max quantity is reached.

For example, when a customer clicks on the "Add to Cart" button, that one uses the add.js call, and if the user already has the max quantity in his cart, it returns a 422 status code, saying that max quantity is reached.

However, if the customer clicks on the + button in the cart to increase quantity, I am using the change.js call to increase (or decrease) the quantity, but once I reach the max quantity, it no longer increases, but it still returns status 200 as if everything is fine, and I have no way of detecting wether max quantity is reached with change.js. No way that I can find in the cart API documentation https://shopify.dev/docs/themes/ajax-api/reference/cart

So how do I determine wether a max quantity is reached with change.js?

I have just tried to do it with the update.js, and that is even allowing me to update the cart to quantities that are not available (for example, available quantity is 4, I got it up to 12!)! Still no errors.

Replies 3 (3)

gina-gregory
Shopify Expert
742 51 211

I usually try to set the max attribute on the input to the variant inventory quantity, and then check the validate the value of the input against that max.

<input type="number" max="{{ item.variant.inventory_quantity }}">

 

sher-bakhodirov
Shopify Partner
7 0 2

Have you found solution?

_Eric
Shopify Partner
4 0 0

We have discovered that this behavior is specific to some stores, and in some cases, only some cart items. We have Store A where going over the quantity available results in a 422 error, Store B where going over the available quantity results in a 200 error for all cart items, and Store C where we get a 422 error if we go over the available quantity only for the first item, but not for any subsequent items. 🙃