Adobe Commerce 2.3 reached end of support in September 2022.

mergeCarts mutation

The mergeCarts mutation transfers the contents of a guest cart into the cart of a logged-in customer. This mutation must be run on behalf of a logged-in customer.

The mutation retains any items that were already in the logged-in customer’s cart. If both the guest and customer carts contain the same item, mergeCarts adds the quantities. Upon success, the mutation deletes the original guest cart.

Use the customerCart query to determine the value of the destination_cart_id attribute.

Syntax

1
2
3
4
5
6
7
8
mutation {
  mergeCarts(
    source_cart_id: String!
    destination_cart_id: String!
  ) {
    Cart!
  }
}

Example usage

In the following example, the customer had one Overnight Duffle in the cart (CYmiiQRjPVc2gJUc5r7IsBmwegVIFO43) before a guest cart (mPKE05OOtcxErbk1Toej6gw6tcuxvT9O) containing a Radiant Tee and another Overnight Duffle was merged. The cart now includes three items, including two Overnight Duffles.

Request:

1
2
3
4
5
6
7
8
9
10
11
12
mutation {
  mergeCarts(source_cart_id: "mPKE05OOtcxErbk1Toej6gw6tcuxvT9O", destination_cart_id: "CYmiiQRjPVc2gJUc5r7IsBmwegVIFO43") {
    items {
      id
      product {
        name
        sku
      }
      quantity
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "data": {
    "mergeCarts": {
      "items": [
        {
          "id": "14",
          "product": {
            "name": "Overnight Duffle",
            "sku": "24-WB07"
          },
          "quantity": 2
        },
        {
          "id": "17",
          "product": {
            "name": "Radiant Tee",
            "sku": "WS12"
          },
          "quantity": 1
        }
      ]
    }
  }
}

Input attributes

Attribute Data Type Description
destination_cart_id String! The ID of the logged-in customer’s cart
source_cart_id String! The ID of the guest cart

Output attributes

The mergeCarts mutation returns a Cart object.

Attribute Data Type Description
cart Cart! Describes the contents of the specified shopping cart

Cart object

Attribute Data Type Description
applied_coupon AppliedCoupon Deprecated. Use applied_coupons instead
applied_coupons [AppliedCoupon] An array of AppliedCoupon objects. Each object contains the code text attribute, which specifies the coupon code
applied_gift_cards [AppliedGiftCard] An array of AppliedGiftCard objects. An AppliedGiftCard object contains the code text attribute, which specifies the gift card code. applied_gift_cards is a Commerce-only attribute, defined in the GiftCardAccountGraphQl module
applied_store_credit AppliedStoreCredit Contains store credit information applied to the cart. applied_store_credit is a Commerce-only attribute, defined in the CustomerBalanceGraphQl module
available_payment_methods [AvailablePaymentMethod] Available payment methods
billing_address BillingCartAddress Contains the billing address specified in the customer’s cart
email String The customer’s email address
id ID! The ID of the cart
is_virtual Boolean! Indicates whether the cart contains only virtual products
items [CartItemInterface] Contains the items in the customer’s cart
prices CartPrices Contains subtotals and totals
selected_payment_method SelectedPaymentMethod Selected payment method
shipping_addresses [ShippingCartAddress]! Contains one or more shipping addresses
total_quantity Float! Total Quantity of products in the cart

Cart query output provides more information about the Cart object.

Errors

Error Description
Current user does not have an active cart. The mergeCarts mutation deactivates the guest cart specified in the source_cart_id after merging. The guest cannot make any further operations with it.
Required parameter "destination_cart_id" is missing The destination_cart_id attribute contains an empty value.
Required parameter "source_cart_id" is missing The source_cart_id attribute contains an empty value.
The current customer isn't authorized. The current customer is not currently logged in, or the customer’s token does not exist in the oauth_token table, or you tried to merge two guest carts.
The current user cannot perform operations on cart The authorized customer tried to merge a guest cart into the cart of another customer.