Skip to content

Commit ac5e23b

Browse files
authored
docs: fixes and improvements to price calculation guide (medusajs#13419)
1 parent a069372 commit ac5e23b

File tree

4 files changed

+212
-92
lines changed

4 files changed

+212
-92
lines changed

www/apps/book/public/llms-full.txt

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32562,7 +32562,7 @@ Each of these prices is represented by the [Price data model](#price-data-model)
3256232562

3256332563
A [PriceList](https://docs.medusajs.com/references/pricing/models/PriceList/index.html.md) is a group of prices that are only enabled when their conditions and rules are satisfied. For example, you can apply special prices to customers in the VIP group.
3256432564

32565-
When the conditions are met, the prices in the price list override the default prices in a price set.
32565+
When the conditions are met, the prices in the price list can override the default prices in a price set. Learn more in the [Price Calculation](https://docs.medusajs.com/Users/shahednasser/medusa/www/apps/resources/app/commerce-modules/pricing/price-calculation/index.html.md) guide.
3256632566

3256732567
A price list has optional `start_date` and `end_date` properties that indicate the date range in which a price list can be applied.
3256832568

@@ -32909,17 +32909,19 @@ Learn more about workflows in [this documentation](https://docs.medusajs.com/doc
3290932909

3291032910
# Prices Calculation
3291132911

32912-
In this document, you'll learn how prices are calculated when you use the [calculatePrices method](https://docs.medusajs.com/references/pricing/calculatePrices/index.html.md) of the Pricing Module's main service.
32912+
In this guide, you'll learn how prices are calculated when you use the [calculatePrices method](https://docs.medusajs.com/references/pricing/calculatePrices/index.html.md) of the Pricing Module's main service.
3291332913

3291432914
## calculatePrices Method
3291532915

32916-
The [calculatePrices method](https://docs.medusajs.com/references/pricing/calculatePrices/index.html.md) accepts as parameters the ID of one or more price sets and a context.
32916+
The [calculatePrices method](https://docs.medusajs.com/references/pricing/calculatePrices/index.html.md) accepts the ID of one or more price sets and a context as parameters.
3291732917

32918-
It returns a price object with the best matching price for each price set.
32918+
It returns a price object with the best-matching price for each price set.
32919+
32920+
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.
3291932921

3292032922
### Calculation Context
3292132923

32922-
The calculation context is an optional object passed as a second parameter to the `calculatePrices` method. It accepts rules to restrict the selected prices in the price set.
32924+
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.
3292332925

3292432926
For example:
3292532927

@@ -32928,7 +32930,7 @@ const price = await pricingModuleService.calculatePrices(
3292832930
{ id: [priceSetId] },
3292932931
{
3293032932
context: {
32931-
currency_code: currencyCode,
32933+
currency_code: "eur",
3293232934
region_id: "reg_123",
3293332935
},
3293432936
}
@@ -32943,19 +32945,19 @@ For each price set, the `calculatePrices` method selects two prices:
3294332945

3294432946
- 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.
3294532947
- An original price, which is either:
32946-
- The same price as the calculated price if the price list it belongs to is of type `override`;
32947-
- Or a price that doesn't belong to a price list and best matches the specified context.
32948+
- The same price as the calculated price if it belongs to a price list of type `override`;
32949+
- Otherwise, a price that doesn't belong to a price list and [best matches](#original-price-selection-logic) the specified context.
3294832950

32949-
Both prices are returned in an object that has the following properties:
32951+
Both prices are returned in an object with the following properties:
3295032952

3295132953
- id: (\`string\`) The ID of the price set from which the price was selected.
3295232954
- is\_calculated\_price\_price\_list: (\`boolean\`) Whether the calculated price belongs to a price list.
3295332955
- calculated\_amount: (\`number\`) The amount of the calculated price, or \`null\` if there isn't a calculated price. This is the amount shown to the customer.
3295432956
- is\_original\_price\_price\_list: (\`boolean\`) Whether the original price belongs to a price list.
32955-
- original\_amount: (\`number\`) The amount of the original price, or \`null\` if there isn't an original price. This amount is useful to compare with the \`calculated\_amount\`, such as to check for discounted value.
32957+
- original\_amount: (\`number\`) 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.
3295632958
- currency\_code: (\`string\`) The currency code of the calculated price, or \`null\` if there isn't a calculated price.
32957-
- is\_calculated\_price\_tax\_inclusive: (\`boolean\`) Whether the calculated price is tax inclusive. Learn more about tax-inclusivity in \[this document]\(../tax-inclusive-pricing/page.mdx)
32958-
- is\_original\_price\_tax\_inclusive: (\`boolean\`) Whether the original price is tax inclusive. Learn more about tax-inclusivity in \[this document]\(../tax-inclusive-pricing/page.mdx)
32959+
- is\_calculated\_price\_tax\_inclusive: (\`boolean\`) Whether the calculated price is tax inclusive. Learn more about tax inclusivity in \[this document]\(../tax-inclusive-pricing/page.mdx)
32960+
- is\_original\_price\_tax\_inclusive: (\`boolean\`) Whether the original price is tax inclusive. Learn more about tax inclusivity in \[this document]\(../tax-inclusive-pricing/page.mdx)
3295932961
- calculated\_price: (\`object\`) The calculated price's price details.
3296032962

3296132963
- id: (\`string\`) The ID of the price.
@@ -32964,9 +32966,9 @@ Both prices are returned in an object that has the following properties:
3296432966

3296532967
- price\_list\_type: (\`string\`) The price list's type. For example, \`sale\`.
3296632968

32967-
- min\_quantity: (\`number\`) The price's min quantity condition.
32969+
- min\_quantity: (\`number\`) The price's minimum quantity condition.
3296832970

32969-
- max\_quantity: (\`number\`) The price's max quantity condition.
32971+
- max\_quantity: (\`number\`) The price's maximum quantity condition.
3297032972
- original\_price: (\`object\`) The original price's price details.
3297132973

3297232974
- id: (\`string\`) The ID of the price.
@@ -32975,51 +32977,65 @@ Both prices are returned in an object that has the following properties:
3297532977

3297632978
- price\_list\_type: (\`string\`) The price list's type. For example, \`sale\`.
3297732979

32978-
- min\_quantity: (\`number\`) The price's min quantity condition.
32980+
- min\_quantity: (\`number\`) The price's minimum quantity condition.
32981+
32982+
- max\_quantity: (\`number\`) The price's maximum quantity condition.
32983+
32984+
### Original Price Selection Logic
3297932985

32980-
- max\_quantity: (\`number\`) The price's max quantity condition.
32986+
When the calculated price isn't from a price list of type `override`, the original price is selected based on the following logic:
32987+
32988+
![Diagram illustrating the original price selection logic](https://res.cloudinary.com/dza7lstvk/image/upload/v1757058523/Medusa%20Resources/original-price-calculation_sxjw3l.jpg)
32989+
32990+
1. If the context doesn't have any rules, select the default price (the price without any rules).
32991+
2. If the context has rules and there's a price that matches all the rules, select that price.
32992+
3. If the context has rules and there's no price that matches all the rules:
32993+
- Find all the prices whose rules match at least one rule in the context.
32994+
- Sort the matched prices by the number of matched rules in descending order.
32995+
- Select the first price in the sorted list (the one that matches the most rules).
3298132996

3298232997
***
3298332998

3298432999
## Examples
3298533000

32986-
Consider the following price set:
33001+
Consider the following price set, which has a default price, prices with rules, and tiered pricing:
3298733002

3298833003
```ts
3298933004
const priceSet = await pricingModuleService.createPriceSets({
3299033005
prices: [
3299133006
// default price
3299233007
{
3299333008
amount: 5,
32994-
currency_code: "EUR",
33009+
currency_code: "eur",
3299533010
rules: {},
3299633011
},
3299733012
// prices with rules
3299833013
{
3299933014
amount: 4,
33000-
currency_code: "EUR",
33015+
currency_code: "eur",
3300133016
rules: {
3300233017
region_id: "reg_123",
3300333018
},
3300433019
},
3300533020
{
3300633021
amount: 4.5,
33007-
currency_code: "EUR",
33022+
currency_code: "eur",
3300833023
rules: {
3300933024
city: "krakow",
3301033025
},
3301133026
},
3301233027
{
33013-
amount: 5,
33014-
currency_code: "EUR",
33028+
amount: 3.5,
33029+
currency_code: "eur",
3301533030
rules: {
3301633031
city: "warsaw",
3301733032
region_id: "reg_123",
3301833033
},
3301933034
},
33035+
// tiered price
3302033036
{
3302133037
amount: 2,
33022-
currency_code: "EUR",
33038+
currency_code: "eur",
3302333039
min_quantity: 100,
3302433040
},
3302533041
],
@@ -33035,15 +33051,34 @@ const price = await pricingModuleService.calculatePrices(
3303533051
{ id: [priceSet.id] },
3303633052
{
3303733053
context: {
33038-
currency_code: "EUR"
33054+
currency_code: "eur"
33055+
}
33056+
}
33057+
)
33058+
```
33059+
33060+
### Result
33061+
33062+
### Calculate Prices with Exact Match
33063+
33064+
### Code
33065+
33066+
```ts
33067+
const price = await pricingModuleService.calculatePrices(
33068+
{ id: [priceSet.id] },
33069+
{
33070+
context: {
33071+
currency_code: "eur",
33072+
region_id: "reg_123",
33073+
city: "warsaw"
3303933074
}
3304033075
}
3304133076
)
3304233077
```
3304333078

3304433079
### Result
3304533080

33046-
### Calculate Prices with Rules
33081+
### Calculate Prices with Partial Match
3304733082

3304833083
### Code
3304933084

@@ -33052,7 +33087,7 @@ const price = await pricingModuleService.calculatePrices(
3305233087
{ id: [priceSet.id] },
3305333088
{
3305433089
context: {
33055-
currency_code: "EUR",
33090+
currency_code: "eur",
3305633091
region_id: "reg_123",
3305733092
city: "krakow"
3305833093
}
@@ -33075,7 +33110,7 @@ const price = await pricingModuleService.calculatePrices(
3307533110
items: [
3307633111
{
3307733112
id: "item_1",
33078-
quantity: 2,
33113+
quantity: 150,
3307933114
// assuming the price set belongs to this variant
3308033115
variant_id: "variant_1",
3308133116
// ...
@@ -33101,29 +33136,29 @@ const priceList = pricingModuleService.createPriceLists([{
3310133136
starts_at: Date.parse("01/10/2023").toString(),
3310233137
ends_at: Date.parse("31/10/2023").toString(),
3310333138
rules: {
33104-
region_id: ['PL']
33139+
region_id: ['region_123', 'region_456'],
3310533140
},
3310633141
type: "sale",
3310733142
prices: [
3310833143
{
33109-
amount: 4,
33110-
currency_code: "EUR",
33144+
amount: 2,
33145+
currency_code: "eur",
3311133146
price_set_id: priceSet.id,
3311233147
},
3311333148
{
33114-
amount: 4.5,
33115-
currency_code: "EUR",
33149+
amount: 1.5,
33150+
currency_code: "usd",
3311633151
price_set_id: priceSet.id,
33117-
},
33152+
}
3311833153
],
3311933154
}]);
3312033155

3312133156
const price = await pricingModuleService.calculatePrices(
3312233157
{ id: [priceSet.id] },
3312333158
{
3312433159
context: {
33125-
currency_code: "EUR",
33126-
region_id: "PL",
33160+
currency_code: "eur",
33161+
region_id: "reg_123",
3312733162
city: "krakow"
3312833163
}
3312933164
}

www/apps/resources/app/commerce-modules/pricing/concepts/page.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Each of these prices is represented by the [Price data model](#price-data-model)
3030

3131
A [PriceList](/references/pricing/models/PriceList) is a group of prices that are only enabled when their conditions and rules are satisfied. For example, you can apply special prices to customers in the VIP group.
3232

33-
When the conditions are met, the prices in the price list override the default prices in a price set.
33+
When the conditions are met, the prices in the price list can override the default prices in a price set. Learn more in the [Price Calculation](../price-calculation/page.mdx) guide.
3434

3535
A price list has optional `start_date` and `end_date` properties that indicate the date range in which a price list can be applied.
3636

0 commit comments

Comments
 (0)