This is a beta release of documentation for Magento 2.4, published for previewing soon-to-be-released functionality. Content in this version is subject to change. Links to the v2.4 code base may not properly resolve until the code is officially released.

createEmptyCart mutation

The createEmptyCart mutation creates an empty shopping cart for a guest or logged in customer. You can allow the system to generate a cart ID, or assign a specific ID.

If you are creating a cart for a logged in customer, you must include the customer’s authorization token in the header of the request.

Syntax

mutation: {createEmptyCart}: String

Example usage

Create a cart with a randomly-generated cart ID

Request:

1
2
3
mutation {
  createEmptyCart
}

Response:

The response is the cart ID, which is sometimes called the quote ID. The remaining examples in this topic will use this cart ID.

1
2
3
4
5
{
  "data": {
    "createEmptyCart": "4JQaNVJokOpFxrykGVvYrjhiNv9qt31C"}
  }
}

Create an empty cart with an assigned cart ID

You can also create an empty cart with a specified cart_id.

Request:

1
2
3
4
mutation {
  createEmptyCart(input:
     { cart_id: "x2345678901234567890123456789012"})
}

Response:

The mutation returns the same cart_id.

1
2
3
4
5
{
  "data": {
    "createEmptyCart": "x2345678901234567890123456789012"
  }
}

Input attributes

Attribute Data Type Description
cart_id String An optional 32-character string

Output attributes

The createEmptyCart mutation returns the cart ID.

Errors

Error Description
Cart with ID "XXX" already exists. The specified cart ID was previously used to create a cart.
Cart ID length should to be 32 symbols. The cart ID is not the required length.