Retrieve Cart in Storefront
In this guide, you'll learn how to retrieve a cart's details in your storefront.
Assuming you stored the cart's ID in the localStorage
as explained in the Create Cart guide, you can retrieve a cart by sending a request to the Get a Cart API route.
For example:
In this example, you retrieve a cart by sending a request to the Get a Cart API route.
The response of the Get a Cart API route has a cart
field, which is a cart object.
Format Prices#
When displaying the cart's totals or line item's price, make sure to format the price as implemented in the formatPrice
function shown in the above snippet:
Since this is the same function used to format the prices of product variants, you can define the function in one place and re-use it where necessary. In that case, make sure to pass the currency code as a parameter to the formatPrice
function.