Adding the customer

How to add a customer to the order

Problem

You need to add a customer to a draft order, for example as the first step of the order checkout flow.

Solution

The easiest way to set the customer of an order is to change its customer_email attribute. To do that, send a PATCH request to the /api/orders/:id endpoint.

In case a customer with the same email address exists in your organization, that customer is associated with the order. Otherwise, a new customer is created.

Example

The following request associates the customer identified by the email address "john@example.com" with the order identified by the "NgojhKoyYN" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/orders/NgojhKoyYN' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "orders",
      "id": "NgojhKoyYN",
      "attributes": {
        "customer_email": "john@example.com"
      }
    }
  }'

Mapping

The image below shows how the related field of an email form is mapped to a specific attribute of the order object.

Logged customers

In case you checkout an order with a logged customer — i.e. using a customer token — the customer associated with the order is forced to be the same as the customer associated with the token and there is no way to explicitly change it. So all you need to do to associate the logged customer to the order is send a PATCH request to the /api/orders/:id endpoint, with empty attributes.

Additional notes

Newsletter subscription

When adding a customer to an order, you might want to prompt the customer to subscribe to your newsletter. This can be easily achieved by creating a customer_subscription resource, using the customer email and any subscription reference — e.g. "Newsletter".

Abandoned cart recovery

When you add a customer to a draft order, the order status changes to pending . Pending orders are suitable to be recovered with specific marketing campaigns, as they contain a way to contact the customer even if the order has not been placed yet.

More to read

See our documentation if you need more information on how to update an order, get a customer token, or create a subscription.

Last updated