CreateCustomerCartMutation
Description
Creates a new cart for a logged-in customer and returns the cart ID. This is typically the first mutation you would call when implementing a shopping cart for authenticated users. If the customer already has an active cart, this mutation will return the ID of that existing cart instead of creating a new one.
Endpoint
mutation createCustomerCart($userId: Int!) {
createCustomerCart(userId: $userId) {
cartId
}
}
Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
| userId | Int | Yes | The ID of the Craft user for whom to create the cart. |
Return Values
The mutation returns a QuoteType object with the following fields:
| Field | Type | Description |
|---|---|---|
| cartId | String | The unique identifier for the newly created or existing cart. This ID should be stored and used in subsequent cart operations. |
Usage Example
mutation {
createCustomerCart(userId: 42) {
cartId
}
}
Example Response
{
"data": {
"createCustomerCart": {
"cartId": "abc123def456"
}
}
}
Notes
- The cart ID returned by this mutation should be stored client-side for use in subsequent cart operations.
- This mutation will first check if the customer already has an active cart. If one exists, it will return that cart's ID rather than creating a new one.
- This cart is associated with a logged-in customer. If you need to create a cart for a guest user, use the
createGuestCartmutation instead. - Customer carts may have different capabilities or limitations compared to guest carts, depending on your store configuration.
- The cart will remain active until it is converted to an order or expires based on your store's cart lifetime settings.
- If the user is not authenticated or the user ID is invalid, an error will be thrown.