402 lines
9.5 KiB
Plaintext
402 lines
9.5 KiB
Plaintext
import { Tabs, TabsList, TabsTrigger, TabsContent, TabsContentWrapper, TypeList } from "docs-ui"
|
|
|
|
export const metadata = {
|
|
title: `Prices Calculation Strategy`,
|
|
}
|
|
|
|
# {metadata.title}
|
|
|
|
In this document, you'll learn how prices are calculated when you use the `calculatePrices` method of the Pricing Module's main service.
|
|
|
|
## calculatePrices Method
|
|
|
|
The [calculatePrices method](/references/pricing/calculatePrices) accepts as parameters the ID of one or more price sets and a context.
|
|
|
|
It returns a price object with the best matching price for each price set.
|
|
|
|
---
|
|
|
|
## Calculation Context
|
|
|
|
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.
|
|
|
|
For example:
|
|
|
|
```ts
|
|
const price = await pricingModuleService.calculatePrices(
|
|
{ id: [priceSetId] },
|
|
{
|
|
context: {
|
|
currency_code: currencyCode,
|
|
region_id: "reg_123",
|
|
},
|
|
}
|
|
)
|
|
```
|
|
|
|
---
|
|
|
|
## Returned Price Object
|
|
|
|
For each price set, the method selects two prices:
|
|
|
|
- The calculated price: Either the best context-matching price that belongs to a price list or the same as the original price.
|
|
- The original price: Either the same as the calculated price if its price list is of type `override`, or the best context-matching price that doesn't belong to a price list.
|
|
|
|
Both prices are returned in an object along with the following properties:
|
|
|
|
<TypeList
|
|
types={[
|
|
{
|
|
name: "id",
|
|
type: "`string`",
|
|
description: "The ID of the price set from which the price was selected."
|
|
},
|
|
{
|
|
name: "is_calculated_price_price_list",
|
|
type: "`boolean`",
|
|
description: "Whether the calculated price belongs to a price list."
|
|
},
|
|
{
|
|
name: "calculated_amount",
|
|
type: "`number`",
|
|
description: "The amount of the calculated price, or `null` if there isn't a calculated price."
|
|
},
|
|
{
|
|
name: "is_original_price_price_list",
|
|
type: "`boolean`",
|
|
description: "Whether the original price belongs to a price list."
|
|
},
|
|
{
|
|
name: "original_amount",
|
|
type: "`number`",
|
|
description: "The amount of the original price, or `null` if there isn't an original price."
|
|
},
|
|
{
|
|
name: "currency_code",
|
|
type: "`string`",
|
|
description: "The currency code of the calculated price, or `null` if there isn't a calculated price."
|
|
},
|
|
{
|
|
name: "calculated_price",
|
|
type: "`object`",
|
|
description: "The calculated price's price details.",
|
|
children: [
|
|
{
|
|
name: "id",
|
|
type: "`string`",
|
|
description: "The ID of the price."
|
|
},
|
|
{
|
|
name: "price_list_id",
|
|
type: "`string`",
|
|
description: "The ID of the associated price list."
|
|
},
|
|
{
|
|
name: "price_list_type",
|
|
type: "`string`",
|
|
description: "The price list's type."
|
|
},
|
|
{
|
|
name: "min_quantity",
|
|
type: "`number`",
|
|
description: "The price's min quantity condition."
|
|
},
|
|
{
|
|
name: "max_quantity",
|
|
type: "`number`",
|
|
description: "The price's max quantity condition."
|
|
}
|
|
]
|
|
},
|
|
{
|
|
name: "original_price",
|
|
type: "`object`",
|
|
description: "The original price's price details.",
|
|
children: [
|
|
{
|
|
name: "id",
|
|
type: "`string`",
|
|
description: "The ID of the price."
|
|
},
|
|
{
|
|
name: "price_list_id",
|
|
type: "`string`",
|
|
description: "The ID of the associated price list."
|
|
},
|
|
{
|
|
name: "price_list_type",
|
|
type: "`string`",
|
|
description: "The price list's type."
|
|
},
|
|
{
|
|
name: "min_quantity",
|
|
type: "`number`",
|
|
description: "The price's min quantity condition."
|
|
},
|
|
{
|
|
name: "max_quantity",
|
|
type: "`number`",
|
|
description: "The price's max quantity condition."
|
|
}
|
|
]
|
|
}
|
|
]}
|
|
sectionTitle="Returned Calculated Price"
|
|
/>
|
|
|
|
---
|
|
|
|
## Examples
|
|
|
|
Consider the following price set:
|
|
|
|
```ts
|
|
const priceSet = await pricingModuleService.createPriceSets({
|
|
prices: [
|
|
// default price
|
|
{
|
|
amount: 500,
|
|
currency_code: "EUR",
|
|
rules: {},
|
|
},
|
|
// prices with rules
|
|
{
|
|
amount: 400,
|
|
currency_code: "EUR",
|
|
rules: {
|
|
region_id: "reg_123",
|
|
},
|
|
},
|
|
{
|
|
amount: 450,
|
|
currency_code: "EUR",
|
|
rules: {
|
|
city: "krakow",
|
|
},
|
|
},
|
|
{
|
|
amount: 500,
|
|
currency_code: "EUR",
|
|
rules: {
|
|
city: "warsaw",
|
|
region_id: "reg_123",
|
|
},
|
|
},
|
|
],
|
|
})
|
|
```
|
|
|
|
### Default Price Selection
|
|
|
|
<Tabs defaultValue="code">
|
|
<TabsList>
|
|
<TabsTrigger value="code">Code</TabsTrigger>
|
|
<TabsTrigger value="result">Result</TabsTrigger>
|
|
</TabsList>
|
|
<TabsContentWrapper>
|
|
<TabsContent value="code">
|
|
|
|
```ts
|
|
const price = await pricingModuleService.calculatePrices(
|
|
{ id: [priceSet.id] },
|
|
{
|
|
context: {
|
|
currency_code: "EUR"
|
|
}
|
|
}
|
|
)
|
|
```
|
|
|
|
</TabsContent>
|
|
<TabsContent value="result">
|
|
|
|
The returned price is:
|
|
|
|
```ts
|
|
const price = {
|
|
id: "<PRICE_SET_ID>",
|
|
is_calculated_price_price_list: false,
|
|
calculated_amount: 500,
|
|
|
|
is_original_price_price_list: false,
|
|
original_amount: 500,
|
|
|
|
currency_code: "EUR",
|
|
|
|
calculated_price: {
|
|
price_id: "<DEFAULT_PRICE_ID>",
|
|
price_list_id: null,
|
|
price_list_type: null,
|
|
min_quantity: null,
|
|
max_quantity: null,
|
|
},
|
|
|
|
original_price: {
|
|
price_id: "<DEFAULT_PRICE_ID>",
|
|
price_list_id: null,
|
|
price_list_type: null,
|
|
min_quantity: null,
|
|
max_quantity: null,
|
|
},
|
|
}
|
|
```
|
|
|
|
- Original price selection: since there are no provided rules in the context, the original price is the default price.
|
|
- Calculated price selection: since there are no associated price lists, the calculated price is set to the original price.
|
|
|
|
</TabsContent>
|
|
</TabsContentWrapper>
|
|
</Tabs>
|
|
|
|
### Calculate Prices with Rules
|
|
|
|
<Tabs defaultValue="code">
|
|
<TabsList>
|
|
<TabsTrigger value="code">Code</TabsTrigger>
|
|
<TabsTrigger value="result">Result</TabsTrigger>
|
|
</TabsList>
|
|
<TabsContentWrapper>
|
|
<TabsContent value="code">
|
|
|
|
```ts
|
|
const price = await pricingModuleService.calculatePrices(
|
|
{ id: [priceSet.id] },
|
|
{
|
|
context: {
|
|
currency_code: "EUR",
|
|
region_id: "reg_123",
|
|
city: "krakow"
|
|
}
|
|
}
|
|
)
|
|
```
|
|
|
|
</TabsContent>
|
|
<TabsContent value="result">
|
|
|
|
The returned price is:
|
|
|
|
```ts
|
|
const price = {
|
|
id: "<PRICE_SET_ID>",
|
|
is_calculated_price_price_list: false,
|
|
calculated_amount: 500,
|
|
|
|
is_original_price_price_list: false,
|
|
original_amount: 500,
|
|
|
|
currency_code: "EUR",
|
|
|
|
calculated_price: {
|
|
price_id: "<FOURTH_PRICE_ID>",
|
|
price_list_id: null,
|
|
price_list_type: null,
|
|
min_quantity: null,
|
|
max_quantity: null,
|
|
},
|
|
|
|
original_price: {
|
|
price_id: "<FOURTH_PRICE_ID>",
|
|
price_list_id: null,
|
|
price_list_type: null,
|
|
min_quantity: null,
|
|
max_quantity: null,
|
|
},
|
|
}
|
|
```
|
|
|
|
- Original price selection: The fourth price in the price list is selected as the best price.
|
|
- Calculated price selection: since there are no associated price lists, the calculated price is set to the original price.
|
|
|
|
</TabsContent>
|
|
</TabsContentWrapper>
|
|
</Tabs>
|
|
|
|
### Price Selection with Price List
|
|
|
|
<Tabs defaultValue="code">
|
|
<TabsList>
|
|
<TabsTrigger value="code">Code</TabsTrigger>
|
|
<TabsTrigger value="result">Result</TabsTrigger>
|
|
</TabsList>
|
|
<TabsContentWrapper>
|
|
<TabsContent value="code">
|
|
|
|
```ts
|
|
const priceList = pricingModuleService.createPriceLists([{
|
|
title: "Summer Price List",
|
|
description: "Price list for summer sale",
|
|
starts_at: Date.parse("01/10/2023").toString(),
|
|
ends_at: Date.parse("31/10/2023").toString(),
|
|
rules: {
|
|
region_id: ['PL']
|
|
},
|
|
type: "sale",
|
|
prices: [
|
|
{
|
|
amount: 400,
|
|
currency_code: "EUR",
|
|
price_set_id: priceSet.id,
|
|
},
|
|
{
|
|
amount: 450,
|
|
currency_code: "EUR",
|
|
price_set_id: priceSet.id,
|
|
},
|
|
],
|
|
}]);
|
|
|
|
const price = await pricingModuleService.calculatePrices(
|
|
{ id: [priceSet.id] },
|
|
{
|
|
context: {
|
|
currency_code: "EUR",
|
|
region_id: "PL",
|
|
city: "krakow"
|
|
}
|
|
}
|
|
)
|
|
```
|
|
|
|
</TabsContent>
|
|
<TabsContent value="result">
|
|
|
|
The returned price is:
|
|
|
|
```ts
|
|
const price = {
|
|
id: "<PRICE_SET_ID>",
|
|
is_calculated_price_price_list: true,
|
|
calculated_amount: 400,
|
|
|
|
is_original_price_price_list: false,
|
|
original_amount: 500,
|
|
|
|
currency_code: "EUR",
|
|
|
|
calculated_price: {
|
|
price_id: "<FOURTH_PRICE_ID>",
|
|
price_list_id: null,
|
|
price_list_type: null,
|
|
min_quantity: null,
|
|
max_quantity: null,
|
|
},
|
|
|
|
original_price: {
|
|
price_id: "<PL_PRICE_ID_1>",
|
|
price_list_id: "<PRICE_LIST_ID>",
|
|
price_list_type: "sale",
|
|
min_quantity: null,
|
|
max_quantity: null,
|
|
},
|
|
}
|
|
```
|
|
|
|
- Original price selection: The fourth price in the price list is selected as the best price.
|
|
- Calculated price selection: The first price of the price list is selected as the best price.
|
|
|
|
</TabsContent>
|
|
</TabsContentWrapper>
|
|
</Tabs>
|