- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
Prices Calculation
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 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:
In this example, you retrieve the prices in a price set for the specified currency code and region ID.
Returned Price Object#
For each price set, the calculatePrices
method selects two prices:
- 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.
- An original price, which is either:
- The same price as the calculated price if the price list it belongs to is of type
override
; - Or a price that doesn't belong to a price list and best matches the specified context.
- The same price as the calculated price if the price list it belongs to is of type
Both prices are returned in an object that has the following properties:
id
stringis_calculated_price_price_list
booleancalculated_amount
numbernull
if there isn't a calculated price. This is the amount shown to the customer.is_original_price_price_list
booleanoriginal_amount
numbernull
if there isn't an original price. This amount is useful to compare with the calculated_amount
, such as to check for discounted value.currency_code
stringnull
if there isn't a calculated price.is_calculated_price_tax_inclusive
booleanis_original_price_tax_inclusive
booleancalculated_price
objectThe calculated price's price details.
calculated_price
objectoriginal_price
objectThe original price's price details.
original_price
objectExamples#
Consider the following price set:
1const priceSet = await pricingModuleService.createPriceSets({2 prices: [3 // default price4 {5 amount: 500,6 currency_code: "EUR",7 rules: {},8 },9 // prices with rules10 {11 amount: 400,12 currency_code: "EUR",13 rules: {14 region_id: "reg_123",15 },16 },17 {18 amount: 450,19 currency_code: "EUR",20 rules: {21 city: "krakow",22 },23 },24 {25 amount: 500,26 currency_code: "EUR",27 rules: {28 city: "warsaw",29 region_id: "reg_123",30 },31 },32 ],33})
Default Price Selection#
Calculate Prices with Rules#
Price Selection with Price List#
1const priceList = pricingModuleService.createPriceLists([{2 title: "Summer Price List",3 description: "Price list for summer sale",4 starts_at: Date.parse("01/10/2023").toString(),5 ends_at: Date.parse("31/10/2023").toString(),6 rules: {7 region_id: ['PL']8 },9 type: "sale",10 prices: [11 {12 amount: 400,13 currency_code: "EUR",14 price_set_id: priceSet.id,15 },16 {17 amount: 450,18 currency_code: "EUR",19 price_set_id: priceSet.id,20 },21 ],22}]);23 24const price = await pricingModuleService.calculatePrices(25 { id: [priceSet.id] },26 {27 context: {28 currency_code: "EUR",29 region_id: "PL",30 city: "krakow"31 }32 }33)