Unexpected behaviour from AJAX Add to Cart API

Unexpected behaviour from AJAX Add to Cart API

mattyonice
Shopify Partner
4 0 4

According to the documentation;

 

If the exact quantity specified for one of the items can't be added to the cart (for example, you are trying to add 9 items, 2 are already in the cart, and 10 are in stock), then no items are added the cart.

However, when I am adding in items from a list (in bulk), I am getting duplicates of the items showing up in my cart. 

 

It seems that when the 422 response comes back (from an array items being added) all the items in that list are actually successfully added, just the quantities are adjusted to accomodate if a product is out of stock. 

 

When the response is 404, then no items in the list are added. 

 

This makes it quite difficult to reliably add items to the cart (without duplicating them). I also can't submit items in one at a time, because then it overloads the Shopify site and I get locked out for 1 hour. 

 

Is this a bug or normal behaviour? Sounds like a bug if we believe the documentation.

Replies 3 (3)

SBD_
Shopify Staff
1831 273 419

Hey @mattyonice 

 

Are you able to DM me store details / replication steps please?

Scott | Developer Advocate @ Shopify 

simon_gld
Shopify Partner
1 0 1

Hi,

 

I'm also experiencing strange ajax behavior. The ajax cart-add api returns 422, but still adds the items. 

 

I've even managed multiple times (although cannot reliably replicate it) to add a product again and the ajax api does add up the quantity. But instead it returns 2 items with the exact same item.id, qty, variant_id and even key!

 

The only difference I can see is that an automatic discount applies differently to both items.

One says:

"discounts": [
{
"amount": 76,
"title": "10kroff"
},
 
The other:
"discounts": [
{
"amount": 77,
"title": "10kroff"
},

 

This discount is an order-level discount with fixed amount, so it seems to be correctly divided onto those items. Except that these items should have been merged by the ajax api.

 

Nick_24
Shopify Partner
13 0 0

Can you dm please more discuss about it.