addProductsToNewCart mutation
This mutation is available only if you have installed Payment Services for Adobe Commerce 2.12.0 or higher.
The addProductsToNewCart
mutation always creates a new cart for the shopper then adds the specified products to that cart. This contrasts with the addProductsToCart
mutation, which requires you to specify an existing cart ID as input.
[!NOTE]
For a logged-in customer, the customer token is passed in the Authorization header.
Use case: smart button on the Product Details Page (PDP)
When a shopper clicks a smartbutton on the Product Details Page (PDP), the
addProductsToNewCart
mutation creates a new cart and adds the items, then returns acart
object, which includes thecart ID
field.It is recommended to use the
setCartAsInactive
mutation after theaddProductsToNewCart
mutation is successful, in case of an error during the payment process, or the shopper cancelling the payment process in the PDP, to avoid having multiple active carts for logged-in customers.If the shopper clicks the smartbutton again,
addProductsToNewCart
mutation runs once more to return a newcart
object.
Syntax
Copied to your clipboardmutation {addProductsToNewCart(cartItems: [CartItemInput!]!): AddProductsToNewCartOutput}
Reference
The addProductsToNewCart
mutation reference is based on the addProductsToCart
reference, which provides detailed information about the types and fields defined in the mutation.
Example usage
These examples show when the addProductsToNewCart
mutation returns a successful, or error message, when creating a new cart in the PDP.
Create a new cart (success)
The following example adds a simple product to a new cart successfully, returning a Cart object.
Request:
Copied to your clipboardmutation {addProductsToNewCart(cartItems: [{quantity: 1sku: "24-MB04"}]) {cart {id}user_errors {codemessage}}}
Response:
Copied to your clipboard{"data": {"addProductsToNewCart": {"cart": {"id": "848QJWDI9WJr0LrIz3n6lRdJQkoGRGYf"},"user_errors": null}}}
Create a new cart (failure)
The following example fails to create a new cart beccause the sku
does not exist in the catalog. It returns a CartUserInputError object.
Request:
Copied to your clipboardmutation {addProductsToNewCart(cartItems: [{quantity: 1sku: "24-MB0ee4"}]) {cart {id}user_errors {codemessage}}}
Response:
Copied to your clipboard{"data": {"addProductsToNewCart": {"cart": null,"user_errors": [{"code": "PRODUCT_NOT_FOUND","message": "Could not find a product with SKU \"24-MB0ee4\""}]}}}