Prices Calculation

In this guide, 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 the ID of one or more price sets and a context as parameters.

It returns a price object with the best-matching price for each price set.

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.

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 it belongs to a price list of type override;
    • Otherwise, a price that doesn't belong to a price list and best matches the specified context.

Both prices are returned in an object with 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 for comparing with the calculated_amount, such as to check for a discounted value. It's null when the price set has no default price and only prices from price lists of type sale match the context.
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.

Original Price Selection Logic#

When the calculated price isn't from a price list of type override, the original price is selected based on the following logic:

  1. If the context doesn't have any rules, select the default price (the price without any rules).
  2. If the context has rules and there's a price that matches all the rules, select that price.
  3. If the context has rules and there's no price that matches all the rules:
    • Find all the prices whose rules match at least one rule in the context.
    • Sort the matched prices by the number of matched rules in descending order.
    • Select the first price in the sorted list (the one that matches the most rules).

Prices from price lists of type sale are never selected as the original price, since the original price represents the customer's reference (non-sale) price.

When the Original Price is Null#

The original_amount and original_price are null when the calculation finds no original price. This happens when both of the following are true:

  • The price set has no default price (a price that doesn't belong to a price list) matching the context.
  • Only prices from price lists of type sale match the context. If a price from a price list of type override matches, it's used as the original price instead.

In this case, the calculated_amount still holds the matching sale price, but there's no reference price to compare it against.


Found a problem with this page?Report Issue
Edited Nov 14·Edit this page