Displaying the cart summary

How to show an order summary to your customers

Problem

You want to display an order summary to your customer, including the order line items and totals.

Solution

To retrieve the list of line items that compose an order summary, send a GET request to the /api/orders/:id endpoint and include its line items.

To get a faster response and fewer data to parse, we recommend requesting only the fields you need to show in the cart summary.

Example

The following request fetches the list of line items associated with the order identified by the "yzkWXfgHQS" ID, with a specific selection of the fields you need to show in the cart summary:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/orders/yzkWXfgHQS?include=line_items&fields[orders]=number,skus_count,formatted_subtotal_amount,formatted_discount_amount,formatted_shipping_amount,formatted_total_tax_amount,formatted_gift_card_amount,formatted_total_amount_with_taxes,line_items&fields[line_items]=item_type,image_url,name,sku_code,formatted_unit_amount,quantity,formatted_total_amount' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

Mapping

The image below shows how each of the elements of the shopping cart is mapped to a specific field of the order or line item objects.

Logged customers

When working with carts belonging to logged customers, you might want to retrieve the current order associated with the logged customer (e.g. to give them the possibility to edit the cart content before placing the order). To do that, you don't need to know the order ID in advance, but you can directly fetch the order relationship of the customer using a customer token. Just make sure to:

  • Select the last updated order belonging to the customer by sorting the API call results by the updated_at attribute (desc) with page[size]=1.

  • Double-check that the order has not been created as guest by filtering the API call by the guest attribute.

  • Check that the order is still editable by filtering the API call by status.

Example

The following request fetched the last updated, editable order belonging to the customer identified by the "nDNahABVZR" ID:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/customers/nDNahABVZR/orders?sort=-updated_at&page[size]=1&filter[q][guest_false]=true&filter[q][status_eq]=pending' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

Additional notes

Filtering by item type

You may have noticed the pseudo-code { "other": "... line items (different item types)" } in the response. That's because the above request fetches all the line items associated with the order (SKUs, shipments, payment methods, and promotions). If you plan to use only some of them, remember to properly filter them through some client-side logic based on their item_type attribute.

Showing the payment method cost

Commerce Layer supports the option for payment methods to have a cost. If that's your case and you want to display it in the cart summary, remember to add also the formatted_payment_method_amount order's field in the API call above.

More to read

Last updated