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.
If you do not specify a value for the destination_cart_id input argument, the mutation determines the customer's cart ID and uses that value.
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.
data-variant=info
data-slots=text
Syntax
mutation {
mergeCarts(
source_cart_id: String!
destination_cart_id: String
) {
Cart!
}
}
Reference
The mergeCarts reference provides detailed information about the types and fields defined in this mutation.
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:
mutation {
mergeCarts(
source_cart_id: "mPKE05OOtcxErbk1Toej6gw6tcuxvT9O",
destination_cart_id: "CYmiiQRjPVc2gJUc5r7IsBmwegVIFO43"
) {
itemsV2 {
items {
id
product {
name
sku
}
quantity
}
total_count
page_info {
page_size
current_page
total_pages
}
}
}
}
Response:
{
"data": {
"mergeCarts": {
"itemsV2": {
"items": [
{
"id": "14",
"product": {
"name": "Overnight Duffle",
"sku": "24-WB07"
},
"quantity": 2
},
{
"id": "17",
"product": {
"name": "Radiant Tee",
"sku": "WS12"
},
"quantity": 1
}
],
"total_count": 2,
"page_info": {
"page_size": 20,
"current_page": 1,
"total_pages": 1
}
}
}
}
}
The following example executes the previous request without specifying destination_cart_id.
Request:
mutation {
mergeCarts(
source_cart_id: "mPKE05OOtcxErbk1Toej6gw6tcuxvT9O"
) {
itemsV2 {
items {
id
product {
name
sku
}
quantity
}
total_count
page_info {
page_size
current_page
total_pages
}
}
}
}
Response:
{
"data": {
"mergeCarts": {
"itemsV2": {
"items": [
{
"id": "14",
"product": {
"name": "Overnight Duffle",
"sku": "24-WB07"
},
"quantity": 2
},
{
"id": "17",
"product": {
"name": "Radiant Tee",
"sku": "WS12"
},
"quantity": 1
}
]
},
"total_count": 2,
"page_info": {
"page_size": 20,
"current_page": 1,
"total_pages": 1
}
}
}
}
Errors
Current user does not have an active cart.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 missingsource_cart_id attribute contains an empty value.The current customer isn't authorized.oauth_token table, or you tried to merge two guest carts.The current user cannot perform operations on cartCould not create empty cart for customer