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:
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) withpage[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:
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