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.
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
13
14
15
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
}
]
}
}
}
The following example executes the previous request without specifying destination_cart_id
.
Request:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mutation {
mergeCarts(
source_cart_id: "mPKE05OOtcxErbk1Toej6gw6tcuxvT9O"
) {
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. If you do not specify a value, the mutation determines the customer’s cart ID and uses that value. |
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_reward_points |
RewardPointsAmount |
The amount of reward points applied to the cart |
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_gift_wrappings |
[GiftWrapping]! | The list of available gift wrapping options for the cart |
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 |
gift_message |
GiftMessage | A gift message added to the cart |
gift_receipt_included |
Boolean! | Indicates if the customer requested a gift receipt for the cart |
gift_wrapping |
GiftWrapping | The selected gift wrapping for the cart |
id |
ID! | The unique 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 |
printed_card_included |
Boolean! | Indicates if the customer requested a printed card for the cart |
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 "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. |
Could not create empty cart for customer |
The system could not create an empty cart for the logged-in customer |