ConvertCartMutation
This file documents two related mutations: convertGuestCartToCustomerCart and convertCustomerCartToGuestCart.
convertGuestCartToCustomerCart
Description
Converts a guest cart to a customer cart. This mutation is typically used when a guest user logs in or creates an account and you want to associate their existing cart with their customer account.
Endpoint
mutation convertGuestCartToCustomerCart($cartId: String!, $userId: Int!) {
convertGuestCartToCustomerCart(cartId: $cartId, userId: $userId) {
# Return fields
}
}
Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| cartId | String | Yes | The unique identifier of the guest cart to convert. This ID is obtained from the createGuestCart mutation. |
| userId | Int | Yes | The ID of the customer to associate with the cart. |
Return Values
The mutation returns a CartType object with the same structure as described in the CartQuery documentation.
Usage Example
mutation {
convertGuestCartToCustomerCart(
cartId: "abc123",
userId: 42
) {
items {
id
quantity
name
}
totals {
grand_total {
value
currency
}
}
}
}
Example Response
{
"data": {
"convertGuestCartToCustomerCart": {
"items": [
{
"id": "123",
"quantity": 2,
"name": "Sample Product"
}
],
"totals": {
"grand_total": {
"value": 39.98,
"currency": "USD"
}
}
}
}
}
convertCustomerCartToGuestCart
Description
Converts a customer cart to a guest cart. This mutation is typically used when a customer logs out and you want to maintain their cart as a guest cart.
Endpoint
mutation convertCustomerCartToGuestCart($cartId: String!) {
convertCustomerCartToGuestCart(cartId: $cartId) {
# Return fields
}
}
Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| cartId | String | Yes | The unique identifier of the customer cart to convert. This ID is obtained from the createCustomerCart mutation or from a previous cart query. |
Return Values
The mutation returns a CartType object with the same structure as described in the CartQuery documentation.
Usage Example
mutation {
convertCustomerCartToGuestCart(
cartId: "abc123"
) {
items {
id
quantity
name
}
totals {
grand_total {
value
currency
}
}
}
}
Example Response
{
"data": {
"convertCustomerCartToGuestCart": {
"items": [
{
"id": "123",
"quantity": 2,
"name": "Sample Product"
}
],
"totals": {
"grand_total": {
"value": 39.98,
"currency": "USD"
}
}
}
}
}
Notes
- These mutations are useful for maintaining cart continuity during user authentication state changes.
- If the conversion fails for any reason, an error will be thrown with a descriptive message.
- The cart contents remain the same after conversion; only the association with a user changes.
- After converting a guest cart to a customer cart, the original guest cart ID remains valid and refers to the same cart, now associated with the customer.
- Similarly, after converting a customer cart to a guest cart, the original cart ID remains valid and refers to the same cart, now disassociated from the customer.