1 ---
2 tags:
3 - cart
4 - storefront
5 products:
6 - cart
7 ---
8
9 import { CodeTabs, CodeTab } from "docs-ui"
10
11 export const metadata = {
12 title: `Retrieve Cart in Storefront`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to retrieve a cart's details in your storefront.
18
19 Assuming you stored the cart's ID in the `localStorage` as explained in the [Create Cart guide](../create/page.mdx), you can retrieve a cart by sending a request to the [Get a Cart API route](!api!/store/carts/get-a-cart).
20
21 For example:
22
23 <CodeTabs group="store-request">
24 <CodeTab label="React" value="react">
25
26 export const highlights = [
27 ["17", "cartId", "Retrieve the cart ID from `localStorage`."],
28 ["19", "TODO", "You can create the cart and set it here as explained in the Create Cart guide."],
29 ["29", "formatPrice", "This function was previously created to format product prices. You can re-use the same function."],
30 ["32", "currency", "If you reuse the `formatPrice` function, pass the currency code as a parameter."],
31 ]
32
33 ```tsx highlights={highlights}
34 "use client" // include with Next.js 13+
35
36 import { useEffect, useState } from "react"
37 import { HttpTypes } from "@medusajs/types"
38 import { sdk } from "@/lib/sdk"
39
40 export default function Cart() {
41 const [cart, setCart] = useState<
42 HttpTypes.StoreCart
43 >()
44
45 useEffect(() => {
46 if (cart) {
47 return
48 }
49
50 const cartId = localStorage.getItem("cart_id")
51 if (!cartId) {
52 // TODO create cart
53 return
54 }
55
56 sdk.store.cart.retrieve(cartId)
57 .then(({ cart: dataCart }) => {
58 setCart(dataCart)
59 })
60 }, [cart])
61
62 const formatPrice = (amount: number): string => {
63 return new Intl.NumberFormat("en-US", {
64 style: "currency",
65 currency: cart?.currency_code,
66 })
67 .format(amount)
68 }
69
70 return (
71 <div>
72 {!cart && <span>Loading...</span>}
73 {cart && (
74 <>
75 <span>Cart ID: {cart.id}</span>
76 <ul>
77 {cart.items?.map((item) => (
78 <li key={item.id}>
79 {item.title} -
80 Quantity: {item.quantity} -
81 Price: {formatPrice(item.unit_price)}
82 </li>
83 ))}
84 </ul>
85 <span>Cart Total: {formatPrice(cart.total)}</span>
86 </>
87 )}
88 </div>
89 )
90 }
91 ```
92
93 </CodeTab>
94 <CodeTab label="JS SDK" value="js-sdk">
95
96 export const fetchHighlights = [
97 ["1", "cartId", "Pass the customer's cart ID as a parameter."],
98 ]
99
100 ```ts highlights={fetchHighlights}
101 sdk.store.cart.retrieve(cartId)
102 .then(({ cart }) => {
103 // use cart...
104 console.log(cart)
105 })
106 ```
107
108 </CodeTab>
109 </CodeTabs>
110
111 In this example, you retrieve a cart by sending a request to the [Get a Cart API route](!api!/store/carts/get-a-cart).
112
113 The response of the [Get a Cart API route](!api!/store/carts/get-a-cart) has a `cart` field, which is a [cart object](!api!/store/carts/schema).
114
115 ### Format Prices
116
117 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:
118
119 ```ts
120 const formatPrice = (amount: number): string => {
121 return new Intl.NumberFormat("en-US", {
122 style: "currency",
123 currency: cart?.currency_code,
124 })
125 .format(amount)
126 }
127 ```
128
129 Since this is the same function used to [format the prices of product variants](../../products/price/page.mdx), 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.