Shopify cart/update.js endpoint not updating the correct lineitem
Solution:
In that case (multiple lines with same variant id in the cart) is better to usechange.js
instead ofupdate.js
: https://shopify.dev/api/ajax/reference/cart#post-locale-cart-change-js (https://shopify.dev/api/ajax/reference/cart#post-locale-cart-change-js)
I'm doing like this:
$.ajax({
type: "post", url: "/cart/change.js", data: [
{"quantity": 2, "line": 0},
{"quantity": 4, "line": 1},
], dataType: 'json'
});
You see that I don't use thevariantId
but I'm using the line index. The downside of this approach is that if you need to apply a change to all the same variant ids you need to know which lines have to be changed. But that can be done reading the respone fromcart.js
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.