Price Selection Strategy
In this document, you’ll learn what a price selection strategy is.
If you’re interested to learn how to override the price selection strategy, check out this documentation instead.
What's a Price Selection Strategy
Medusa provides many features and different ways to control the price of a product variant. This includes price lists and their different conditions, products’ original prices, and taxes.
Medusa uses the PriceSelectionStrategy
class to retrieve the best price for a product variant for a specific context. This strategy is used whenever products and line items are retrieved or manipulated on the storefront.
PriceSelectionStrategy Overview
The PriceSelectionStrategy
class extends the AbstractPriceSelectionStrategy
class. Its main method is the calculateVariantPrice
.
calculateVariantPrice Method
Medusa uses this method to retrieve one or more product variants' prices. This method is used when retrieving product variants or their associated line items. It's also used when retrieving other entities that product variants and line items belong to, such as products and carts respectively.
This method accepts two parameters:
- The first parameter is an array of objects, each object having the following properties:
variantId
: a string indicating the ID of the variant to calculate the price for.quantity
: an optional number indicating the quantity of the variant.
- A context object.
The method retrieves all the available prices of the variant based on the conditions in the context object.
It returns an object with the following properties:
originalPrice
: The original price of the variant which depends on the selected region or currency code in the context object. If both region ID and currency code are available in the context object, the region has higher precedence.originalPriceIncludesTax
: A boolean value indicating whether the original price includes taxes or not. This is only available for Tax-Inclusive Pricing.calculatedPrice
: The lowest price among the prices of the product variant retrieved using the context object.calculatedPriceIncludesTax
: A boolean value indicating whether the calculated price includes taxes or not. This is only available for Tax-Inclusive Pricing.calculatedPriceType
: Eitherdefault
if thecalculatedPrice
is the original price, or the type of the price list applied.prices
: an array of all the prices of the variant retrieved using the context object. It can include its original price and its price lists if there are any.
You can learn more about price lists and how they’re used in this documentation.
Context Object
The context that is passed to the calculateVariantPrice
method is an object that has the following optional properties:
cart_id
: A string indicating the ID of the customer’s cart. This is used when the prices are being retrieved for the variant of a line item, as it is used to determine the current region and currency code of the context.customer_id
: A string indicating the ID of the customer. This is used to filter out price lists for a customer group that this customer doesn’t belong to.quantity
: A number indicating the quantity of the item in the cart. This is used to filter out price lists that havemin_quantity
ormax_quantity
conditions set.region_id
: A string indicating the ID of the region the customer is using.currency_code
: A string indicating the currency code the customer is using.include_discount_prices
: A boolean value indicating whether price list prices should be retrieved or not.tax_rates
: An array of objects indicating the tax rates to be applied. This is only used for Tax-Inclusive Pricing.ignore_cache
: a boolean value indicating whether to calculate the prices even if the value of an earlier price calculation is available in the cache.