/cart/add.js no longer returns remaining inventory when maximum exceeded

Shopify Partner
46 1 16

I only noticed this issue yesterday, but it appears based on this reddit post that the issue has been there for at least 4 months.

 

It used to be the case that when a customer adds more of an item to their cart than is available, that the API endpoint returned a message with the remaining inventory, like so:

"You can only add #{item.remaining_inventory} #{item.name} to the cart"

Now, it only returns a generic message with no remaining quantity:

"You can't add more #{item.name} to the cart"

When was this change made, and why? Is there any way to undo this change? My themes relied on this message for error recovery, as I would extract the remaining inventory quantity and resubmit the form with the maximum allowed quantity (while showing a message to the customer letting them know).

 

UPDATE:

It appears that the remaining inventory is still returned ONLY if the maximum quantity is already in the cart. In all other cases, the remaining quantity is not returned in the error message.

0 Likes
Highlighted
Shopify Partner
46 1 16

Admittedly this is an unreliable solution, since I shouldn't be relying on error messages to stay the same. I assumed they would, since this error message [used to be] documented.

 

In the meantime, I've come up with a more complex, but more reliable workaround:

 

  • In my product forms, add an extra hidden field containing the product's handle (so my javascript can gain access to the product's url)
  • Create an alternate product template where I output a JSON object giving me the available quantity of each variant, and the cart quantity of each variant
  • In my javascript Add to Cart form processing, use the added handle field to make an AJAX request for the quantities of this product (using the alternate product template). Find the relevant variant in the JSON packet, and retrieve the quantities I need
  • Adjust the requested quantities so that the customer doesn't exceed what is available, and submit the cart form again
  • Show the customer a message letting them know that the quantity was changed
0 Likes
Highlighted
Shopify Expert
2 0 0

We are experiencing the same issue.

 

We could do a workaround by getting the cart state and then calculating how much they can add (available - already in cart), but this seems unnecessary. It would be way better if the API returned a better error message that displays this quantity.

0 Likes