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:

Code
1const price = await pricingModuleService.calculatePrices(2  { id: [priceSetId] },3  {4    context: {5      currency_code: currencyCode,6      region_id: "reg_123",7    },8  }9)

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.

Both prices are returned in an object that has the following properties:

idstring
The ID of the price set from which the price was selected.
is_calculated_price_price_listboolean
Whether the calculated price belongs to a price list.
calculated_amountnumber
The amount of the calculated price, or null if there isn't a calculated price. This is the amount shown to the customer.
is_original_price_price_listboolean
Whether the original price belongs to a price list.
original_amountnumber
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.
currency_codestring
The currency code of the calculated price, or null if there isn't a calculated price.
is_calculated_price_tax_inclusiveboolean
Whether the calculated price is tax inclusive. Learn more about tax-inclusivity in this document
is_original_price_tax_inclusiveboolean
Whether the original price is tax inclusive. Learn more about tax-inclusivity in this document
calculated_priceobject
The calculated price's price details.
original_priceobject
The original price's price details.

Examples#

Consider the following price set:

Code
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#

Code
1const price = await pricingModuleService.calculatePrices(2  { id: [priceSet.id] },3  {4    context: {5      currency_code: "EUR"6    }7  }8)

Calculate Prices with Rules#

Code
1const price = await pricingModuleService.calculatePrices(2  { id: [priceSet.id] },3  {4    context: {5      currency_code: "EUR",6      region_id: "reg_123",7      city: "krakow"8    }9  }10)

Price Selection with Price List#

Code
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)
Was this page helpful?
Edit this page