on GitHub" data-tooltip-id=":R1blcldb:">v2.5.1·Edited Oct 9·
In this document, you'll learn how prices are calculated when you use the calculatePrices method of the Pricing Module's main service.
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.
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.
For each price set, the calculatePrices
method selects two prices:
override
;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
objectoriginal_price
objectConsider 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})
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)