1 ---
2 tags:
3 - cart
4 - name: how to
5 label: Retrieve Cart Totals
6 - server
7 ---
8
9 import { CodeTabs, CodeTab } from "docs-ui"
10
11 export const metadata = {
12 title: `Retrieve Cart Totals using Query`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to retrieve cart totals in your Medusa application using [Query](!docs!/learn/fundamentals/query).
18
19 You may need to retrieve cart totals in your Medusa customizations, such as workflows or custom API routes, to perform custom actions with them. The ideal way to retrieve totals is with Query.
20
21 <Note title="Looking for a storefront guide?">
22
23 Refer to the [Retrieve Cart Totals in Storefront](../../../storefront-development/cart/totals/page.mdx) guide for a storefront-specific approach.
24
25 </Note>
26
27 ## How to Retrieve Cart Totals with Query
28
29 To retrieve cart totals, you mainly need to pass the `total` field within the `fields` option of the Query. This will return the cart's grand total, along with the totals of its line items and shipping methods. You can also pass additional total fields that you need for your use case.
30
31 For example, to retrieve all totals of a cart:
32
33 <Note title="Tip">
34
35 Use `useQueryGraphStep` in workflows, and `query.graph` in custom API routes, scheduled jobs, and subscribers.
36
37 </Note>
38
39 <CodeTabs group="query-type">
40 <CodeTab label="useQueryGraphStep" value="useQueryGraphStep">
41
42 ```ts highlights={[["12"]]}
43 import { createWorkflow } from "@medusajs/framework/workflows-sdk"
44 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
45
46 export const myWorkflow = createWorkflow(
47 "my-workflow",
48 () => {
49 const { data: carts } = useQueryGraphStep({
50 entity: "cart",
51 fields: [
52 "id",
53 "currency_code",
54 "total",
55 "subtotal",
56 "tax_total",
57 "discount_total",
58 "discount_subtotal",
59 "discount_tax_total",
60 "original_total",
61 "original_tax_total",
62 "item_total",
63 "item_subtotal",
64 "item_tax_total",
65 "original_item_total",
66 "original_item_subtotal",
67 "original_item_tax_total",
68 "shipping_total",
69 "shipping_subtotal",
70 "shipping_tax_total",
71 "original_shipping_tax_total",
72 "original_shipping_subtotal",
73 "original_shipping_total",
74 "gift_card_total",
75 "gift_card_tax_total",
76 "credit_line_subtotal",
77 "credit_line_tax_total",
78 "credit_line_total",
79 "items.*",
80 "shipping_methods.*",
81 ],
82 filters: {
83 id: "cart_123", // Specify the cart ID
84 },
85 })
86 }
87 )
88 ```
89 </CodeTab>
90 <CodeTab label="query.graph" value="query.graph">
91
92 ```ts highlights={[["10"]]}
93 const query = container.resolve(
94 ContainerRegistrationKeys.QUERY
95 ) // or req.scope.resolve in API routes
96
97 const { data: [cart] } = await query.graph({
98 entity: "cart",
99 fields: [
100 "id",
101 "currency_code",
102 "total",
103 "subtotal",
104 "tax_total",
105 "discount_total",
106 "discount_subtotal",
107 "discount_tax_total",
108 "original_total",
109 "original_tax_total",
110 "item_total",
111 "item_subtotal",
112 "item_tax_total",
113 "original_item_total",
114 "original_item_subtotal",
115 "original_item_tax_total",
116 "shipping_total",
117 "shipping_subtotal",
118 "shipping_tax_total",
119 "original_shipping_tax_total",
120 "original_shipping_subtotal",
121 "original_shipping_total",
122 "gift_card_total",
123 "gift_card_tax_total",
124 "credit_line_subtotal",
125 "credit_line_tax_total",
126 "credit_line_total",
127 "items.*",
128 "shipping_methods.*",
129 ],
130 filters: {
131 id: "cart_123", // Specify the cart ID
132 },
133 })
134 ```
135 </CodeTab>
136 </CodeTabs>
137
138 The shape of the returned `cart` object depends on where you access it. Server-side, each total is a [BigNumber](!docs!/learn/fundamentals/data-models/big-numbers) instance. In an API route's response, each total is a plain number.
139
140 <Note>
141
142 Learn how to use a big-number total in the [Perform Arithmetic or Display Cart Totals](#perform-arithmetic-or-display-cart-totals) section.
143
144 </Note>
145
146 <CodeTabs group="value-shape">
147 <CodeTab label="Server-side" value="server">
148
149 ```
150 // each total is a BigNumber instance
151 {
152 id: "cart_123",
153 currency_code: "usd",
154 total: BigNumber { numeric_: 10 },
155 subtotal: BigNumber { numeric_: 10 },
156 tax_total: BigNumber { numeric_: 0 },
157 discount_total: BigNumber { numeric_: 0 },
158 discount_subtotal: BigNumber { numeric_: 0 },
159 discount_tax_total: BigNumber { numeric_: 0 },
160 original_total: BigNumber { numeric_: 10 },
161 original_tax_total: BigNumber { numeric_: 0 },
162 item_total: BigNumber { numeric_: 10 },
163 item_subtotal: BigNumber { numeric_: 10 },
164 item_tax_total: BigNumber { numeric_: 0 },
165 original_item_total: BigNumber { numeric_: 10 },
166 original_item_subtotal: BigNumber { numeric_: 10 },
167 original_item_tax_total: BigNumber { numeric_: 0 },
168 shipping_total: BigNumber { numeric_: 10 },
169 shipping_subtotal: BigNumber { numeric_: 10 },
170 shipping_tax_total: BigNumber { numeric_: 0 },
171 original_shipping_tax_total: BigNumber { numeric_: 0 },
172 original_shipping_subtotal: BigNumber { numeric_: 10 },
173 original_shipping_total: BigNumber { numeric_: 10 },
174 gift_card_total: BigNumber { numeric_: 0 },
175 gift_card_tax_total: BigNumber { numeric_: 0 },
176 credit_line_subtotal: BigNumber { numeric_: 0 },
177 credit_line_tax_total: BigNumber { numeric_: 0 },
178 credit_line_total: BigNumber { numeric_: 0 },
179 items: [
180 {
181 id: "cali_123",
182 // ...
183 unit_price: BigNumber { numeric_: 10 },
184 subtotal: BigNumber { numeric_: 10 },
185 total: BigNumber { numeric_: 0 },
186 original_total: BigNumber { numeric_: 10 },
187 discount_total: BigNumber { numeric_: 0 },
188 discount_subtotal: BigNumber { numeric_: 0 },
189 discount_tax_total: BigNumber { numeric_: 0 },
190 tax_total: BigNumber { numeric_: 0 },
191 original_tax_total: BigNumber { numeric_: 0 },
192 }
193 ],
194 shipping_methods: [
195 {
196 id: "casm_01K10AYZDKZGQXE8WXW3QP9T22",
197 // ...
198 amount: BigNumber { numeric_: 10 },
199 subtotal: BigNumber { numeric_: 10 },
200 total: BigNumber { numeric_: 10 },
201 original_total: BigNumber { numeric_: 10 },
202 discount_total: BigNumber { numeric_: 0 },
203 discount_subtotal: BigNumber { numeric_: 0 },
204 discount_tax_total: BigNumber { numeric_: 0 },
205 tax_total: BigNumber { numeric_: 0 },
206 original_tax_total: BigNumber { numeric_: 0 },
207 }
208 ],
209 }
210 ```
211
212 </CodeTab>
213 <CodeTab label="API Response" value="api">
214
215 ```json
216 {
217 "id": "cart_123",
218 "currency_code": "usd",
219 "total": 10,
220 "subtotal": 10,
221 "tax_total": 0,
222 "discount_total": 0,
223 "discount_subtotal": 0,
224 "discount_tax_total": 0,
225 "original_total": 10,
226 "original_tax_total": 0,
227 "item_total": 10,
228 "item_subtotal": 10,
229 "item_tax_total": 0,
230 "original_item_total": 10,
231 "original_item_subtotal": 10,
232 "original_item_tax_total": 0,
233 "shipping_total": 10,
234 "shipping_subtotal": 10,
235 "shipping_tax_total": 0,
236 "original_shipping_tax_total": 0,
237 "original_shipping_subtotal": 10,
238 "original_shipping_total": 10,
239 "gift_card_total": 0,
240 "gift_card_tax_total": 0,
241 "credit_line_subtotal": 0,
242 "credit_line_tax_total": 0,
243 "credit_line_total": 0,
244 "items": [
245 {
246 "id": "cali_123",
247 // ...
248 "unit_price": 10,
249 "subtotal": 10,
250 "total": 0,
251 "original_total": 10,
252 "discount_total": 0,
253 "discount_subtotal": 0,
254 "discount_tax_total": 0,
255 "tax_total": 0,
256 "original_tax_total": 0,
257 }
258 ],
259 "shipping_methods": [
260 {
261 "id": "casm_01K10AYZDKZGQXE8WXW3QP9T22",
262 // ...
263 "amount": 10,
264 "subtotal": 10,
265 "total": 10,
266 "original_total": 10,
267 "discount_total": 0,
268 "discount_subtotal": 0,
269 "discount_tax_total": 0,
270 "tax_total": 0,
271 "original_tax_total": 0,
272 }
273 ]
274 }
275 ```
276
277 </CodeTab>
278 </CodeTabs>
279
280 <Note title="Using the Cart Module's service">
281
282 You get the same `BigNumber` totals if you resolve the Cart Module's service and call a method like `retrieveCart` or `listCarts`, passing a total field in the `select` option. Query uses these service methods internally, so the returned shape is the same.
283
284 </Note>
285
286 ### Cart Totals
287
288 The cart will include the following total fields:
289
290 - `total`: The cart's final total after discounts and credit lines, including taxes.
291 - `subtotal`: The cart's subtotal before discounts, excluding taxes. Calculated as the sum of `item_subtotal` and `shipping_subtotal`.
292 - `tax_total`: The cart's tax total after discounts. Calculated as the sum of `item_tax_total` and `shipping_tax_total`.
293 - `discount_total`: The total amount of discounts applied to the cart, including the tax portion of discounts.
294 - `discount_subtotal`: The total amount of discounts applied to the cart's subtotal (excluding tax portion). Used in the final total calculation.
295 - `discount_tax_total`: The total amount of discounts applied to the cart's tax. Represents the tax portion of discounts.
296 - `original_total`: The cart's total before discounts, including taxes. Calculated as the sum of `original_item_total` and `original_shipping_total`.
297 - `original_tax_total`: The cart's tax total before discounts. Calculated as the sum of `original_item_tax_total` and `original_shipping_tax_total`.
298 - `item_total`: The sum of all line items' totals after discounts, including taxes.
299 - `item_subtotal`: The sum of all line items' subtotals before discounts, excluding taxes.
300 - `item_tax_total`: The sum of all line items' tax totals after discounts.
301 - `original_item_total`: The sum of all line items' original totals before discounts, including taxes.
302 - `original_item_subtotal`: The sum of all line items' original subtotals before discounts, excluding taxes.
303 - `original_item_tax_total`: The sum of all line items' original tax totals before discounts.
304 - `shipping_total`: The sum of all shipping methods' totals after discounts, including taxes.
305 - `shipping_subtotal`: The sum of all shipping methods' subtotals before discounts, excluding taxes.
306 - `shipping_tax_total`: The sum of all shipping methods' tax totals after discounts.
307 - `original_shipping_tax_total`: The sum of all shipping methods' original tax totals before discounts.
308 - `original_shipping_subtotal`: The sum of all shipping methods' original subtotals before discounts, excluding taxes.
309 - `original_shipping_total`: The sum of all shipping methods' original totals before discounts, including taxes.
310 - `gift_card_total`: The total amount of gift cards applied to the cart.
311 - `gift_card_tax_total`: The tax total of gift cards applied to the cart.
312 - `credit_line_subtotal`: The subtotal of credit lines applied to the cart, excluding taxes.
313 - `credit_line_tax_total`: The tax total of credit lines applied to the cart.
314 - `credit_line_total`: The total amount of credit lines applied to the cart, including taxes. Subtracted from the final total.
315
316 ### Cart Line Item Totals
317
318 The `items` array in the `cart` object contains total fields for each line item:
319
320 - `unit_price`: The price of a single unit of the line item. This field is not calculated and is stored in the database.
321 - `subtotal`: The line item's subtotal before discounts, excluding taxes.
322 - `total`: The line item's total after discounts, including taxes.
323 - `original_total`: The line item's original total before discounts, including taxes.
324 - `discount_total`: The total amount of discounts applied to the line item, including the tax portion of discounts.
325 - `discount_subtotal`: The total amount of discounts applied to the line item's subtotal (excluding tax portion).
326 - `discount_tax_total`: The total amount of discounts applied to the line item's tax. Represents the tax portion of discounts.
327 - `tax_total`: The line item's tax total after discounts.
328 - `original_tax_total`: The line item's original tax total before discounts.
329
330 ### Cart Shipping Method Totals
331
332 The `shipping_methods` array in the `cart` object contains total fields for each shipping method:
333
334 - `amount`: The amount charged for the shipping method. This field is not calculated and is stored in the database.
335 - `subtotal`: The shipping method's subtotal before discounts, excluding taxes.
336 - `total`: The shipping method's total after discounts, including taxes.
337 - `original_total`: The shipping method's original total before discounts, including taxes.
338 - `discount_total`: The total amount of discounts applied to the shipping method, including the tax portion of discounts.
339 - `discount_subtotal`: The total amount of discounts applied to the shipping method's subtotal (excluding tax portion).
340 - `discount_tax_total`: The total amount of discounts applied to the shipping method's tax. Represents the tax portion of discounts.
341 - `tax_total`: The shipping method's tax total after discounts.
342 - `original_tax_total`: The shipping method's original tax total before discounts.
343
344 ---
345
346 ## Perform Arithmetic or Display Cart Totals
347
348 When you retrieve cart totals with Query in server-side code, computed totals like `total` are instances of [BigNumber](!docs!/learn/fundamentals/data-models/big-numbers), not plain numbers.
349
350 To perform arithmetic on these totals without losing precision, use the `MathBN` utility from `@medusajs/framework/utils`. Don't use JavaScript's arithmetic operators, as they can introduce rounding errors:
351
352 ```ts
353 import { MathBN } from "@medusajs/framework/utils"
354
355 // cart retrieved with Query
356
357 const totalWithoutTax = MathBN.sub(
358 cart.total,
359 cart.tax_total
360 )
361
362 // use the result as a plain number
363 const totalNumber = totalWithoutTax.toNumber()
364 ```
365
366 To display a cart total, convert it to a plain number and format it. Since Medusa stores prices in major currency units, you can format the value directly:
367
368 ```ts
369 import { BigNumber } from "@medusajs/framework/utils"
370
371 const total =
372 cart.total instanceof BigNumber
373 ? cart.total.numeric
374 : cart.total
375
376 const formatted = new Intl.NumberFormat("en-US", {
377 style: "currency",
378 currency: cart.currency_code,
379 }).format(total)
380
381 // formatted is "$10.00"
382 ```
383
384 Learn more in the [Big Numbers](!docs!/learn/fundamentals/data-models/big-numbers) documentation.
385
386 ---
387
388 ## Caveats of Retrieving Cart Totals
389
390 ### Using Asterisk (*) in Cart's Query
391
392 Cart totals are calculated based on the cart's line items, shipping methods, taxes, and any discounts applied. They are not stored in the `Cart` data model.
393
394 For that reason, you cannot retrieve cart totals by passing `*` to the Query's fields. For example, the following query will not return cart totals:
395
396 <CodeTabs group="query-type">
397 <CodeTab label="useQueryGraphStep" value="useQueryGraphStep">
398
399 ```ts
400 const { data: carts } = useQueryGraphStep({
401 entity: "cart",
402 fields: ["*"],
403 filters: {
404 id: "cart_123",
405 },
406 })
407
408 // carts don't include cart totals
409 ```
410 </CodeTab>
411 <CodeTab label="query.graph" value="query.graph">
412
413 ```ts
414 const { data: [cart] } = await query.graph({
415 entity: "cart",
416 fields: ["*"],
417 filters: {
418 id: "cart_123",
419 },
420 })
421
422 // cart doesn't include cart totals
423 ```
424 </CodeTab>
425 </CodeTabs>
426
427 This will return the cart data stored in the database, but not the calculated totals.
428
429 You also can't pass `*` along with `total` in the Query's fields option. Passing `*` will override the `total` field, and you will not retrieve cart totals. For example, the following query will not return cart totals:
430
431 <CodeTabs group="query-type">
432 <CodeTab label="useQueryGraphStep" value="useQueryGraphStep">
433
434 {/* eslint-disable @medusajs/no-wildcard-with-specific-fields */}
435
436 ```ts
437 const { data: carts } = useQueryGraphStep({
438 entity: "cart",
439 fields: ["*", "total"],
440 filters: {
441 id: "cart_123",
442 },
443 })
444
445 // carts don't include cart totals
446 ```
447 </CodeTab>
448 <CodeTab label="query.graph" value="query.graph">
449
450 {/* eslint-disable @medusajs/no-wildcard-with-specific-fields */}
451
452 ```ts
453 const { data: [cart] } = await query.graph({
454 entity: "cart",
455 fields: ["*", "total"],
456 filters: {
457 id: "cart_123",
458 },
459 })
460
461 // cart doesn't include cart totals
462 ```
463 </CodeTab>
464 </CodeTabs>
465
466 Instead, when you need to retrieve cart totals, explicitly pass the `total` field in the Query's fields option, as shown in [the previous section](#how-to-retrieve-cart-totals-with-query). You can also include other fields and relations you need, such as `email` and `items.*`.
467
468 ### Applying Filters on Cart Totals
469
470 You can't apply filters directly on the cart totals, as they are not stored in the `Cart` data model. You can still filter the cart based on its properties, such as `id`, `email`, or `currency_code`, but not on the calculated totals.