/cart/add.js only returning an undocumented error message when added quantity is too high

Highlighted
New Member
4 0 0

Has anyone else had trouble getting /cart/add.js to return the correct inventory-related error messages?

 

I've verified I'm able to add items to my cart, and in varying quantities. When I try to add more of an item to my cart than I have in stock I expect one of the error messages listed here:

 

https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#add-to-cart

 

e.g.

 

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

While I get the 422 error code back, I only get the message "You can't add more #{item.name} to the cart." This isn't descriptive and potentially annoying for customers, especially since "more" implies that there may possibly already be some of this item in the cart.

 

I've already tried reaching out to Shopify support which has resulted in first blaming my theme (this happens on their own themes too, and I'm not sure how these server-side responses could be theme related,) and then declaring this feature was removed, and now most recently declaring API-related questions are out of the scope of support.

 

My inventory is managed and tracked by Shopify so I'm at a loss.

0 Likes
Shopify Staff
Shopify Staff
34 5 7

Hey @tdot,

 

This is actually an old error message that should have been removed from our documentation. Shops no longer expose inventory quantities on the storefront, so a generic error message is expected when you try to add more inventory than is available. Thanks for pointing out the incorrect error message in the docs, we'll be sure to get that updated.

JB | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
New Member
4 0 0

@_JB 

 

I see. I understand the given reasoning behind this is to protect shop owners from bots, but what exactly is being protected? It feels to me like this should be an option, and not the default. "You can not add more" when someone has 0 of an item in a cart is an awful message. And customers are expected to try changing the quantity until they're finally able to add an item to the cart? If I were a customer, and I have nothing in my cart, and was told I couldn't "add more" I'd just bail; there's no intuitive reason to keep trying different quantities. This feels like Shopify is going to hurt conversions.

 

Additionally, why is it that I can visit a shop such as Rikumo and the documented error message still exists?

0 Likes
Shopify Staff
Shopify Staff
34 5 7

Hey @tdot ,

 

You don't have to return the AJAX response message on the page. If you'd like to return a different message including the remaining quantity, you can catch this error and use the liquid property {{ variant.inventory_quantity }} to display your own message with the remaining quantity. If you want all remaining inventory added to the cart, you can also use this liquid property to send another cart POST with the available quantity.

 

Stores created before a certain date may still use the old error messaging, so it's possible to still see it out there. However it's expected that most stores will see the responses without the remaining inventory included, so I'd build with that expectation in mind. Using liquid properties to get the remaining quantity will work on any store, so you won't have to worry about compatibility issues.

 

JB | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes
New Member
4 0 0

@_JB 

 

I appreciate the suggestion, but I really have to wonder who gave the thumbs up on this change, and again why it isn't an optional setting.

 

If I use {{ variant.inventory_quantity }} I now have to account for idle pages, otherwise I risk presenting the user with incorrect inventory counts. So I can add a few lines of JS to set a refresh timeout if there isn't any mouse movement, sure. But then I also have to determine if any of the available quantity is already in the customer's cart. These are just two problems that I can quickly think of to mimic a feature that already existed and was removed to "protect from bots."

0 Likes