Raw
1 import { Tabs, TabsList, TabsTrigger, TabsContent, TabsContentWrapper, TypeList } from "docs-ui"
2
3 export const metadata = {
4 title: `Prices Calculation`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn how prices are calculated when you use the [calculatePrices method](/references/pricing/calculatePrices) of the Pricing Module's main service.
10
11 ## calculatePrices Method
12
13 The [calculatePrices method](/references/pricing/calculatePrices) accepts the ID of one or more price sets and a context as parameters.
14
15 It returns a price object with the best-matching price for each price set.
16
17 The `calculatePrices` method is useful for retrieving the prices of a product variant or a shipping option that matches a specific context, such as a currency code, in your backend customizations.
18
19 ### Calculation Context
20
21 The calculation context is an optional object passed as the second parameter to the `calculatePrices` method. It accepts rules as key-value pairs to restrict the selected prices in the price set.
22
23 For example:
24
25 ```ts
26 const price = await pricingModuleService.calculatePrices(
27 { id: [priceSetId] },
28 {
29 context: {
30 currency_code: "eur",
31 region_id: "reg_123",
32 },
33 }
34 )
35 ```
36
37 In this example, you retrieve the prices in a price set for the specified currency code and region ID.
38
39 ### Returned Price Object
40
41 For each price set, the `calculatePrices` method selects two prices:
42
43 - A calculated price: Either a price that belongs to a price list and best matches the specified context, or the same as the original price.
44 - An original price, which is either:
45 - The same price as the calculated price if it belongs to a price list of type `override`;
46 - Otherwise, a price that doesn't belong to a price list and [best matches](#original-price-selection-logic) the specified context.
47
48 Both prices are returned in an object with the following properties:
49
50 <TypeList
51 types={[
52 {
53 name: "id",
54 type: "`string`",
55 description: "The ID of the price set from which the price was selected."
56 },
57 {
58 name: "is_calculated_price_price_list",
59 type: "`boolean`",
60 description: "Whether the calculated price belongs to a price list."
61 },
62 {
63 name: "calculated_amount",
64 type: "`number`",
65 description: "The amount of the calculated price, or `null` if there isn't a calculated price. This is the amount shown to the customer."
66 },
67 {
68 name: "is_original_price_price_list",
69 type: "`boolean`",
70 description: "Whether the original price belongs to a price list."
71 },
72 {
73 name: "original_amount",
74 type: "`number`",
75 description: "The amount of the original price, or `null` if there isn't an original price. This amount is useful for comparing with the `calculated_amount`, such as to check for a discounted value. It's `null` when the price set has no default price and only prices from price lists of type `sale` match the context."
76 },
77 {
78 name: "currency_code",
79 type: "`string`",
80 description: "The currency code of the calculated price, or `null` if there isn't a calculated price."
81 },
82 {
83 name: "is_calculated_price_tax_inclusive",
84 type: "`boolean`",
85 description: "Whether the calculated price is tax inclusive. Learn more about tax inclusivity in [this document](../tax-inclusive-pricing/page.mdx)"
86 },
87 {
88 name: "is_original_price_tax_inclusive",
89 type: "`boolean`",
90 description: "Whether the original price is tax inclusive. Learn more about tax inclusivity in [this document](../tax-inclusive-pricing/page.mdx)"
91 },
92 {
93 name: "calculated_price",
94 type: "`object`",
95 description: "The calculated price's price details.",
96 children: [
97 {
98 name: "id",
99 type: "`string`",
100 description: "The ID of the price."
101 },
102 {
103 name: "price_list_id",
104 type: "`string`",
105 description: "The ID of the associated price list."
106 },
107 {
108 name: "price_list_type",
109 type: "`string`",
110 description: "The price list's type. For example, `sale`."
111 },
112 {
113 name: "min_quantity",
114 type: "`number`",
115 description: "The price's minimum quantity condition."
116 },
117 {
118 name: "max_quantity",
119 type: "`number`",
120 description: "The price's maximum quantity condition."
121 }
122 ]
123 },
124 {
125 name: "original_price",
126 type: "`object`",
127 description: "The original price's price details.",
128 children: [
129 {
130 name: "id",
131 type: "`string`",
132 description: "The ID of the price."
133 },
134 {
135 name: "price_list_id",
136 type: "`string`",
137 description: "The ID of the associated price list."
138 },
139 {
140 name: "price_list_type",
141 type: "`string`",
142 description: "The price list's type. For example, `sale`."
143 },
144 {
145 name: "min_quantity",
146 type: "`number`",
147 description: "The price's minimum quantity condition."
148 },
149 {
150 name: "max_quantity",
151 type: "`number`",
152 description: "The price's maximum quantity condition."
153 }
154 ]
155 }
156 ]}
157 sectionTitle="Returned Calculated Price"
158 />
159
160 ### Original Price Selection Logic
161
162 When the calculated price isn't from a price list of type `override`, the original price is selected based on the following logic:
163
164 ![Diagram illustrating the original price selection logic](https://res.cloudinary.com/dza7lstvk/image/upload/v1757058523/Medusa%20Resources/original-price-calculation_sxjw3l.jpg)
165
166 1. If the context doesn't have any rules, select the default price (the price without any rules).
167 2. If the context has rules and there's a price that matches all the rules, select that price.
168 3. If the context has rules and there's no price that matches all the rules:
169 - Find all the prices whose rules match at least one rule in the context.
170 - Sort the matched prices by the number of matched rules in descending order.
171 - Select the first price in the sorted list (the one that matches the most rules).
172
173 Prices from price lists of type `sale` are never selected as the original price, since the original price represents the customer's reference (non-sale) price.
174
175 ### When the Original Price is Null
176
177 The `original_amount` and `original_price` are `null` when the calculation finds no original price. This happens when both of the following are true:
178
179 - The price set has no default price (a price that doesn't belong to a price list) matching the context.
180 - Only prices from price lists of type `sale` match the context. If a price from a price list of type `override` matches, it's used as the original price instead.
181
182 In this case, the `calculated_amount` still holds the matching sale price, but there's no reference price to compare it against.
183
184 ---
185
186 ## Examples
187
188 Consider the following price set, which has a default price, prices with rules, and tiered pricing:
189
190 ```ts
191 const priceSet = await pricingModuleService.createPriceSets({
192 prices: [
193 // default price
194 {
195 amount: 5,
196 currency_code: "eur",
197 rules: {},
198 },
199 // prices with rules
200 {
201 amount: 4,
202 currency_code: "eur",
203 rules: {
204 region_id: "reg_123",
205 },
206 },
207 {
208 amount: 4.5,
209 currency_code: "eur",
210 rules: {
211 city: "krakow",
212 },
213 },
214 {
215 amount: 3.5,
216 currency_code: "eur",
217 rules: {
218 city: "warsaw",
219 region_id: "reg_123",
220 },
221 },
222 // tiered price
223 {
224 amount: 2,
225 currency_code: "eur",
226 min_quantity: 100,
227 },
228 ],
229 })
230 ```
231
232 ### Default Price Selection
233
234 <Tabs defaultValue="code">
235 <TabsList>
236 <TabsTrigger value="code">Code</TabsTrigger>
237 <TabsTrigger value="result">Result</TabsTrigger>
238 </TabsList>
239 <TabsContentWrapper>
240 <TabsContent value="code">
241
242 ```ts
243 const price = await pricingModuleService.calculatePrices(
244 { id: [priceSet.id] },
245 {
246 context: {
247 currency_code: "eur"
248 }
249 }
250 )
251 ```
252
253 </TabsContent>
254 <TabsContent value="result">
255
256 The returned price is:
257
258 ```ts
259 const price = {
260 id: "<PRICE_SET_ID>",
261 is_calculated_price_price_list: false,
262 calculated_amount: 5,
263
264 is_original_price_price_list: false,
265 original_amount: 5,
266
267 currency_code: "eur",
268
269 is_calculated_price_tax_inclusive: false,
270 is_original_price_tax_inclusive: false,
271
272 calculated_price: {
273 price_id: "<DEFAULT_PRICE_ID>",
274 price_list_id: null,
275 price_list_type: null,
276 min_quantity: null,
277 max_quantity: null,
278 },
279
280 original_price: {
281 price_id: "<DEFAULT_PRICE_ID>",
282 price_list_id: null,
283 price_list_type: null,
284 min_quantity: null,
285 max_quantity: null,
286 },
287 }
288 ```
289
290 - Original price selection: Since there are no provided rules in the context, the original price is the default price of the price set.
291 - Calculated price selection: Since there are no associated price lists, the calculated price is set to the original price.
292
293 </TabsContent>
294 </TabsContentWrapper>
295 </Tabs>
296
297 ### Calculate Prices with Exact Match
298
299 <Tabs defaultValue="code">
300 <TabsList>
301 <TabsTrigger value="code">Code</TabsTrigger>
302 <TabsTrigger value="result">Result</TabsTrigger>
303 </TabsList>
304 <TabsContentWrapper>
305 <TabsContent value="code">
306
307 ```ts
308 const price = await pricingModuleService.calculatePrices(
309 { id: [priceSet.id] },
310 {
311 context: {
312 currency_code: "eur",
313 region_id: "reg_123",
314 city: "warsaw"
315 }
316 }
317 )
318 ```
319
320 </TabsContent>
321 <TabsContent value="result">
322
323 The returned price is:
324
325 ```ts
326 const price = {
327 id: "<PRICE_SET_ID>",
328 is_calculated_price_price_list: false,
329 calculated_amount: 3.5,
330
331 is_original_price_price_list: false,
332 original_amount: 3.5,
333
334 currency_code: "eur",
335
336 is_calculated_price_tax_inclusive: false,
337 is_original_price_tax_inclusive: false,
338
339 calculated_price: {
340 price_id: "<FOURTH_PRICE_ID>",
341 price_list_id: null,
342 price_list_type: null,
343 min_quantity: null,
344 max_quantity: null,
345 },
346
347 original_price: {
348 price_id: "<FOURTH_PRICE_ID>",
349 price_list_id: null,
350 price_list_type: null,
351 min_quantity: null,
352 max_quantity: null,
353 },
354 }
355 ```
356
357 - Original price selection: The fourth price in the price set is selected as the best price because it matches both the `region_id` and `city` rules.
358 - Calculated price selection: Since there are no associated price lists, the calculated price is set to the original price.
359
360 </TabsContent>
361 </TabsContentWrapper>
362 </Tabs>
363
364
365 ### Calculate Prices with Partial Match
366
367 <Tabs defaultValue="code">
368 <TabsList>
369 <TabsTrigger value="code">Code</TabsTrigger>
370 <TabsTrigger value="result">Result</TabsTrigger>
371 </TabsList>
372 <TabsContentWrapper>
373 <TabsContent value="code">
374
375 ```ts
376 const price = await pricingModuleService.calculatePrices(
377 { id: [priceSet.id] },
378 {
379 context: {
380 currency_code: "eur",
381 region_id: "reg_123",
382 city: "krakow"
383 }
384 }
385 )
386 ```
387
388 </TabsContent>
389 <TabsContent value="result">
390
391 The returned price is:
392
393 ```ts
394 const price = {
395 id: "<PRICE_SET_ID>",
396 is_calculated_price_price_list: false,
397 calculated_amount: 4,
398
399 is_original_price_price_list: false,
400 original_amount: 4,
401
402 currency_code: "eur",
403
404 is_calculated_price_tax_inclusive: false,
405 is_original_price_tax_inclusive: false,
406
407 calculated_price: {
408 price_id: "<SECOND_PRICE_ID>",
409 price_list_id: null,
410 price_list_type: null,
411 min_quantity: null,
412 max_quantity: null,
413 },
414
415 original_price: {
416 price_id: "<SECOND_PRICE_ID>",
417 price_list_id: null,
418 price_list_type: null,
419 min_quantity: null,
420 max_quantity: null,
421 },
422 }
423 ```
424
425 - Original price selection: The second price in the price set is selected as the best price because it matches the `region_id` rule.
426 - Although the third price also matches a rule (`city`), the second price is selected because it appears first in the price set.
427 - Calculated price selection: Since there are no associated price lists, the calculated price is set to the original price.
428
429 </TabsContent>
430 </TabsContentWrapper>
431 </Tabs>
432
433 ### Tiered Pricing Selection
434
435 <Tabs defaultValue="code">
436 <TabsList>
437 <TabsTrigger value="code">Code</TabsTrigger>
438 <TabsTrigger value="result">Result</TabsTrigger>
439 </TabsList>
440 <TabsContentWrapper>
441 <TabsContent value="code">
442
443 ```ts
444 const price = await pricingModuleService.calculatePrices(
445 { id: [priceSet.id] },
446 {
447 context: {
448 cart: {
449 items: [
450 {
451 id: "item_1",
452 quantity: 150,
453 // assuming the price set belongs to this variant
454 variant_id: "variant_1",
455 // ...
456 }
457 ],
458 // ...
459 }
460 }
461 }
462 )
463 ```
464
465 </TabsContent>
466 <TabsContent value="result">
467
468 The returned price is:
469
470 ```ts
471 const price = {
472 id: "<PRICE_SET_ID>",
473 is_calculated_price_price_list: false,
474 calculated_amount: 2,
475
476 is_original_price_price_list: false,
477 original_amount: 2,
478
479 currency_code: "eur",
480
481 is_calculated_price_tax_inclusive: false,
482 is_original_price_tax_inclusive: false,
483
484 calculated_price: {
485 price_id: "<FIFTH_PRICE_ID>",
486 price_list_id: null,
487 price_list_type: null,
488 min_quantity: 100,
489 max_quantity: null,
490 },
491
492 original_price: {
493 price_id: "<FIFTH_PRICE_ID>",
494 price_list_id: null,
495 price_list_type: null,
496 min_quantity: 100,
497 max_quantity: null,
498 },
499 }
500 ```
501
502 - Original price selection: Since the cart's item quantity is `100` or more, the tiered price is selected as the best price.
503 - Calculated price selection: Since there are no associated price lists, the calculated price is set to the original price.
504
505 </TabsContent>
506 </TabsContentWrapper>
507 </Tabs>
508
509 ### Price Selection with Price List
510
511 <Tabs defaultValue="code">
512 <TabsList>
513 <TabsTrigger value="code">Code</TabsTrigger>
514 <TabsTrigger value="result">Result</TabsTrigger>
515 </TabsList>
516 <TabsContentWrapper>
517 <TabsContent value="code">
518
519 ```ts
520 const priceList = pricingModuleService.createPriceLists([{
521 title: "Summer Price List",
522 description: "Price list for summer sale",
523 starts_at: Date.parse("01/10/2023").toString(),
524 ends_at: Date.parse("31/10/2023").toString(),
525 rules: {
526 region_id: ['region_123', 'region_456'],
527 },
528 type: "sale",
529 prices: [
530 {
531 amount: 2,
532 currency_code: "eur",
533 price_set_id: priceSet.id,
534 },
535 {
536 amount: 1.5,
537 currency_code: "usd",
538 price_set_id: priceSet.id,
539 }
540 ],
541 }]);
542
543 const price = await pricingModuleService.calculatePrices(
544 { id: [priceSet.id] },
545 {
546 context: {
547 currency_code: "eur",
548 region_id: "reg_123",
549 city: "krakow"
550 }
551 }
552 )
553 ```
554
555 </TabsContent>
556 <TabsContent value="result">
557
558 The returned price is:
559
560 ```ts
561 const price = {
562 id: "<PRICE_SET_ID>",
563 is_calculated_price_price_list: true,
564 calculated_amount: 2,
565
566 is_original_price_price_list: false,
567 original_amount: 4,
568
569 currency_code: "eur",
570
571 is_calculated_price_tax_inclusive: false,
572 is_original_price_tax_inclusive: false,
573
574 calculated_price: {
575 price_id: "<PL_PRICE_ID_1>",
576 price_list_id: "<PRICE_LIST_ID>",
577 price_list_type: "sale",
578 min_quantity: null,
579 max_quantity: null,
580 },
581
582 original_price: {
583 price_id: "<SECOND_PRICE_ID>",
584 price_list_id: null,
585 price_list_type: null,
586 min_quantity: null,
587 max_quantity: null,
588 },
589 }
590 ```
591
592 - Original price selection: The second price in the price set is selected as the best price because it matches the `region_id` rule.
593 - Although the third price also matches a rule (`city`), the second price is selected because it appears first in the price set.
594 - Calculated price selection: The price from the price list is selected as the calculated price because it matches the `region_id` rule of the price list.
595
596 </TabsContent>
597 </TabsContentWrapper>
598 </Tabs>