Cart ajax API clarification

Need clarification on POST to cart.js endpoint

Based on the documentation if my request payload is trying to add more than the available quantity then it would add the available amount and return the following response

{
  "status": 422,
  "message": "Cart Error",
  "description": "You can't add more #{item.name} to the cart."
}

The issue is that when I do exactly that, nothing gets added to the cart, just the error response is received. Is that the expected behavior?

That’s weird, and interesting. As fair as I’m aware, this has always been the case. Was not aware of the mismatch with documentation.

Looking forward to see an explanation.

I have just tested using the following example, from the documentation, on a variant ID with exactly 50 in stock. Tracking inventory and dont have continue policy. I get the same error as you.

let formData = {
 'items': [{
  'id': 36110175633573,
  'quantity': 2
  }]
};

fetch(window.Shopify.routes.root + 'cart/add.js', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(formData)
})
.then(response => {
  return response.json();
})
.catch((error) => {
  console.error('Error:', error);
});

I think the behavior is different if the item is already in the cart or not (such as an existing).

They noted this in the changelog back in July of this year and docs could probably use some clarifying.

I’ve just tested, and this does not seem to be the case either. 422 is still returned (as expected) but nothing is added to cart.

Case:

  1. Manually add 1x {variant_id) in cart
  2. {variant_id) has a total of 50 in stock
  3. Post request (same example as above) with 60 quantity
  4. 422 returned and still only 1 in cart.
1 Like

Hey Taylor, thanks for your reply. I had seen the changelog and my confusion is that the current response is the same for both scenarios, when I do or do not have the item in the cart already.

What is missing is the documented behavior that if the item is not in the cart, the call would automatically add the remaining amount then respond with the 422 code but it doesn’t

1 Like