Question about multiple change request on mutation (change quantity to 0) #93
-
It's probably not an issue on liquid-ajax-cart side, so I'm creating a Q&A. I hope someone can drive me in the right direction: Scenario When a user adds a (main) product to their cart, I would like n additional related products to be programmatically added with quantity 1. Every related product should be removed as well if the primary product is removed. Expected Behaviour The cart items should retain their cart_item.key value in between "change" requests, and the cart should lose the items one by one. Current Behaviour The first related cart item has been removed, and the remaining related product - change request ends up in 400:
It appears that the cart_item.key value varies between "change" requests. Steps to Reproduce Add a mutation:
Pseudo remove implementation
Question I would appreciate if one could drive me to the right direction. I'm pretty new to Shopify Liquid, but I really enjoy using this nimble library, thanks! 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @menya-TIS So pls try the following approach and let me know if that works: const removeCrossProductsWithoutMain = () => {
// ...
const cart = window?.liquidAjaxCart?.cart
const requestBody = {}
cart.items.forEach(item => {
if (!hasMainInCart) {
requestBody[item.key] = 0;
}
});
if (Object.values(requestBody).length > 0) {
return {
requests: [
{
type: 'update',
body: requestBody
}
]
}
}
return null;
} The idea is to use the First, we generate the request body for the
where zeros are the new quantities, and pass this objects as a request body for the Pls let me know if that works. I didn't test the code yet :-) |
Beta Was this translation helpful? Give feedback.
@menya-TIS
I guess I didn't read the Shopify docs carefully.
The item keys shouldn't be the root props of the object.
It should be like this:
So the code should be the following:
Could you try one more time and let me know pls…