Happening now! Shopify Community AMA: Building a Marketing Budget with 2H Media | Ask your marketing budget questions now!

how can i completely replace my entire cart with differnet line items with cart api in shopify

how can i completely replace my entire cart with differnet line items with cart api in shopify

bhavik_
Shopify Partner
2 0 1

Hi everyone,

With the deprecation of the Checkout API, I’m transitioning to Shopify’s Cart API. In the Checkout API, we had a simple way to replace the entire cart by passing new line items, but I don’t see a direct equivalent in the Cart API.

From my research, the Cart API provides mutations like:

  • cartLinesAdd (to add new items)
  • cartLinesRemove (to remove specific items)
  • cartLinesUpdate (to update existing items)

However, there doesn’t seem to be a cartLinesReplace or cartReplace mutation. This means that if I want to completely replace the cart with a new set of line items, I need to:

  1. Fetch the current cart items (cart(id: $cartId)).
  2. Remove all existing line items (cartLinesRemove).
  3. Add the new line items (cartLinesAdd).


Is there an efficient way to replace the entire cart in a single API call?

Reply 1 (1)

angieg0nzalez
Shopify Partner
3 0 0

I have the exact same question. In the migration guide it says 

 

The cartLinesUpdate mutation behaves similarly to checkoutLineItemsReplace in the Checkout API. It replaces the existing line items in the cart with the new line items provided in the lines argument. This is useful when you want to completely refresh the cart contents.

 

But I did not find this to be true. It needs me to pass the cart line id, which does not make sense if my new cart has more or less items than the old cart. The guide also shows a `ProductVariant` ID, but I'm pretty sure that's a mistake and it should be a CartLine ID.