1 ---
2 tags:
3 - cart
4 - storefront
5 products:
6 - cart
7 ---
8
9 import { CodeTabs, CodeTab, Table } from "docs-ui"
10
11 export const metadata = {
12 title: `Show Cart Totals`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to show the cart totals in the checkout flow. This is usually shown as part of the checkout and cart pages.
18
19 ## Cart Total Fields
20
21 The `Cart` object has various fields related to its totals, which you can check out in the [Store API reference](!api!/store/carts/schema).
22
23 The fields that are most commonly used are:
24
25 <Table>
26 <Table.Header>
27 <Table.Row>
28 <Table.HeaderCell>
29 Field
30 </Table.HeaderCell>
31 <Table.HeaderCell>
32 Description
33 </Table.HeaderCell>
34 </Table.Row>
35 </Table.Header>
36 <Table.Body>
37 <Table.Row>
38 <Table.Cell>
39 `subtotal`
40 </Table.Cell>
41 <Table.Cell>
42 The cart's subtotal before discounts, excluding taxes. Calculated as the sum of `item_subtotal` and `shipping_subtotal`.
43 </Table.Cell>
44 </Table.Row>
45 <Table.Row>
46 <Table.Cell>
47 `discount_total`
48 </Table.Cell>
49 <Table.Cell>
50 The total amount of discounts applied to the cart, including the tax portion of discounts.
51 </Table.Cell>
52 </Table.Row>
53 <Table.Row>
54 <Table.Cell>
55 `shipping_total`
56 </Table.Cell>
57 <Table.Cell>
58 The sum of all shipping methods' totals after discounts, including taxes.
59 </Table.Cell>
60 </Table.Row>
61 <Table.Row>
62 <Table.Cell>
63 `tax_total`
64 </Table.Cell>
65 <Table.Cell>
66 The cart's tax total after discounts. Calculated as the sum of `item_tax_total` and `shipping_tax_total`.
67 </Table.Cell>
68 </Table.Row>
69 <Table.Row>
70 <Table.Cell>
71 `total`
72 </Table.Cell>
73 <Table.Cell>
74 The cart's final total after discounts and credit lines, including taxes.
75 </Table.Cell>
76 </Table.Row>
77 </Table.Body>
78 </Table>
79
80 ---
81
82 ## Example: Show Cart Totals in React Storefront
83
84 Here's an example of how you can show the cart totals in a React component:
85
86 <Note title="Tip">
87
88 This example uses the `useCart` hook from the [Cart Context](../context/page.mdx) to retrieve the cart.
89
90 </Note>
91
92 export const highlights = [
93 ["3", "useCart", "The `useCart` hook was defined in the Cart React Context documentation."],
94 ["8", "formatPrice", "A function to format a price using the `Intl.NumberFormat` API."],
95 ["23", "formatPrice", "Show the cart's subtotal"],
96 ["27", "formatPrice", "Show the total discounts"],
97 ["31", "formatPrice", "Show the shipping total"],
98 ["35", "formatPrice", "Show the tax total"],
99 ["39", "formatPrice", "Show the total amount"],
100 ]
101
102 ```tsx highlights={highlights}
103 "use client" // include with Next.js 13+
104
105 import { useCart } from "@/providers/cart"
106
107 export default function CartTotals() {
108 const { cart } = useCart()
109
110 const formatPrice = (amount: number): string => {
111 return new Intl.NumberFormat("en-US", {
112 style: "currency",
113 currency: cart?.currency_code || "USD",
114 })
115 .format(amount)
116 }
117
118 return (
119 <div>
120 {!cart && <span>Loading...</span>}
121 {cart && (
122 <ul>
123 <li>
124 <span>Subtotal (excl. taxes)</span>
125 <span>{formatPrice(cart.subtotal ?? 0)}</span>
126 </li>
127 <li>
128 <span>Discounts</span>
129 <span>{formatPrice(cart.discount_total ?? 0)}</span>
130 </li>
131 <li>
132 <span>Shipping</span>
133 <span>{formatPrice(cart.shipping_total ?? 0)}</span>
134 </li>
135 <li>
136 <span>Taxes</span>
137 <span>{formatPrice(cart.tax_total ?? 0)}</span>
138 </li>
139 <li>
140 <span>Total</span>
141 <span>{formatPrice(cart.total ?? 0)}</span>
142 </li>
143 </ul>
144 )}
145 </div>
146 )
147 }
148 ```
149
150 In the example, you first retrieve the cart using the [Cart Context](../context/page.mdx). Then, you define the [formatPrice](../retrieve/page.mdx#format-prices) function to format the total amounts.
151
152 Finally, you render the cart totals in a list, showing the subtotal, discounts, shipping, taxes, and the total amount.
153
154 ---
155
156 ## Retrieve and Show Cart Item Totals
157
158 You can also show the totals specific to each item in the cart. This is useful when you want to show the price breakdown for each item, such as price before and after discounts.
159
160 The cart item totals aren't included by default in the cart object. You need to explicitly expand the `items.*` relation when retrieving the cart. This will add item totals like `total`, `subtotal`, `tax_total`, and `discount_total` to each item object in the cart. You can learn about other total fields in the [Store API reference](!api!/store/carts/schema).
161
162 For example, when retrieving the cart, you can expand the `items.*` relation like this:
163
164 ```ts
165 sdk.store.cart.retrieve(cartId, {
166 fields: "+items.*",
167 // TIP: You can also expand both items and shipping methods at the same time
168 // fields: "+items.*, +shipping_methods.*",
169 })
170 .then(({ cart: dataCart }) => {
171 setCart(dataCart)
172 })
173 ```
174
175 Then, you can show the item totals in a React component like this:
176
177 ```tsx
178 "use client" // include with Next.js 13+
179
180 import { HttpTypes } from "@medusajs/types"
181
182 type CartItemTotalsProps = {
183 cart: HttpTypes.StoreCart
184 }
185
186 export default function CartItemTotals({
187 cart,
188 }: CartItemTotalsProps) {
189 const formatPrice = (amount: number): string => {
190 return new Intl.NumberFormat("en-US", {
191 style: "currency",
192 currency: cart.currency_code,
193 })
194 .format(amount)
195 }
196
197 return (
198 <ul>
199 {cart.items.map((item) => (
200 <li key={item.id}>
201 <span>{item.title}</span>
202 <span>{formatPrice(item.total!)}</span>
203 <span>(Subtotal: {formatPrice(item.subtotal!)})</span>
204 <span>(Discounts: {formatPrice(item.discount_total!)})</span>
205 <span>(Taxes: {formatPrice(item.tax_total!)})</span>
206 </li>
207 ))}
208 </ul>
209 )
210 }
211 ```
212
213 In the example, you receive the cart with the expanded item totals as a prop. Then, you define the [formatPrice](../retrieve/page.mdx#format-prices) function to format the total amounts.
214
215 Finally, you render the cart items in a list, showing each item's title, total, subtotal, discounts, and taxes.
216
217 ---
218
219 ## Retrieve and Show Shipping Method Totals
220
221 You can also show the totals specific to each shipping method in the cart. This is useful when you want to show the price breakdown for each shipping method, such as price before and after discounts.
222
223 The cart shipping method totals aren't included by default in the cart response. You need to explicitly expand the `shipping_methods.*` relation when retrieving the cart. This will add shipping method totals like `total`, `subtotal`, `tax_total`, and `discount_total` to each shipping method object in the cart. You can learn about other total fields in the [Store API reference](!api!/store/carts/schema).
224
225 For example, when retrieving the cart, you can expand the `shipping_methods.*` relation like this:
226
227 ```ts
228 sdk.store.cart.retrieve(cartId, {
229 fields: "+shipping_methods.*",
230 // TIP: You can also expand both items and shipping methods at the same time
231 // fields: "+items.*, +shipping_methods.*",
232 })
233 .then(({ cart: dataCart }) => {
234 setCart(dataCart)
235 })
236 ```
237
238 Then, you can show the shipping method totals in a React component like this:
239
240 ```tsx
241 "use client" // include with Next.js 13+
242
243 import { HttpTypes } from "@medusajs/types"
244
245 type CartShippingMethodTotalsProps = {
246 cart: HttpTypes.StoreCart
247 }
248
249 export default function CartShippingMethodTotals({
250 cart,
251 }: CartShippingMethodTotalsProps) {
252 const formatPrice = (amount: number): string => {
253 return new Intl.NumberFormat("en-US", {
254 style: "currency",
255 currency: cart.currency_code,
256 })
257 .format(amount)
258 }
259
260 return (
261 <ul>
262 {cart.shipping_methods.map((method) => (
263 <li key={method.id}>
264 <span>{method.name}</span>
265 <span>{formatPrice(method.total!)}</span>
266 <span>(Subtotal: {formatPrice(method.subtotal!)})</span>
267 <span>(Discounts: {formatPrice(method.discount_total!)})</span>
268 <span>(Taxes: {formatPrice(method.tax_total!)})</span>
269 </li>
270 ))}
271 </ul>
272 )
273 }
274 ```
275
276 In the example, you receive the cart with the expanded shipping method totals as a prop. Then, you define the [formatPrice](../retrieve/page.mdx#format-prices) function to format the total amounts.
277
278 Finally, you render the cart shipping methods in a list, showing each method's name, total, subtotal, discounts, and taxes.