Creating a shopping cart

How to create a new shopping cart (draft order)

Problem

You need to create a new shopping cart where the customer can put the items they want to purchase.

Solution

Within Commerce Layer, a shopping cart is just a draft order, so creating a new shopping cart actually means creating a new order. To do that, all you need to do is to send a POST request to the /api/orders endpoint.

Example

The following request creates a draft order that acts as a new (empty) shopping cart:

curl -g -X POST \
  'http://yourdomain.commercelayer.io/api/orders' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
  "data": {
    "type": "orders"
    }
}'

Additional notes

Market scope

The new order will be automatically associated with the market embedded in the request access token, as its scope.

Empty carts

For performance reasons, we highly suggest you avoid dealing with empty carts — just create a new order right before adding the first line item to it.

More to read

See our documentation if you need more information on how to create an order.

Last updated