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