In order to comply with the new Shopify 429 aggressive behaviour, we’re trying to eliminate any cart api calls.
It seems that you can read new items added to cart using cart/add.js, however the data we’re seeing been returned differs from the example here:
In our experience, adding one item to cart returns one object no matter the quantity, while adding more than one one type of item, return the updated cart line items.
E.g. you add a single item:
{
"id": 43220605468884,
"properties": {},
"quantity": 1,
"variant_id": 43220605468884,
// bunch of other properties
}
I just tested this to confirm, and I’m seeing the same behavior as the documentation, which appears to match what you’re describing. Is the issue that this behavior isn’t what you expected?
The behavior changes with bundles, though.
For example, if you have a Cart Transform Function using the merge operation on a bundle and add all the bundle items in a single add to cart request, you’ll only see 1 object returned.
There’s also nested cart lines behavior to consider:
The issue is that the documentation only talks about behaviour number 1, I think it should cover all behaviours to deincentivize apps having to keep reading the cart on every callback.
For example, I didn’t know about behaviour number 3, so that would make me worried not reading the cart again.
Here’s an example, this app reads the cart 5-10 times, each time anything happens to the cart:
I want to make sure I’m following your concern correctly. The response format of /cart/add.js is actually consistent. It always returns an object with an items array, which is what the Ajax API reference documents.
The extra reads aren’t due to cart response formatting. Apps fetch /cart.js after cart operations because they need the full current state of the cart. In theory, apps could listen for a cart event from the theme instead of fetching independently. Some themes do broadcast the full cart response this way (Horizon does this with ThemeEvents.cartUpdate). But there’s no standard for it across themes, so apps can’t rely on it being available and end up fetching on their own. As a result, you have multiple apps contributing to 429 errors.