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

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 The AppliedCoupon 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
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

Cart query output provides more information about the Cart object.