Skip to main content
Skip to main content

calculatePrices - Pricing Module Reference

Beta

This documentation provides a reference to the calculatePrices method. This belongs to the Pricing Module.

This method is used to calculate prices based on the provided filters and context.

Example

When you calculate prices, you must at least specify the currency code:

import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function calculatePrice (priceSetId: string, currencyCode: string) {
const pricingService = await initializePricingModule()

const price = await pricingService.calculatePrices(
{ id: [priceSetId] },
{
context: {
currency_code: currencyCode
}
}
)

// do something with the price or return it
}

To calculate prices for specific minimum and/or maximum quantity:

import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function calculatePrice (priceSetId: string, currencyCode: string) {
const pricingService = await initializePricingModule()

const price = await pricingService.calculatePrices(
{ id: [priceSetId] },
{
context: {
currency_code: currencyCode,
min_quantity: 4
}
}
)

// do something with the price or return it
}

To calculate prices for custom rule types:

import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function calculatePrice (priceSetId: string, currencyCode: string) {
const pricingService = await initializePricingModule()

const price = await pricingService.calculatePrices(
{ id: [priceSetId] },
{
context: {
currency_code: currencyCode,
region_id: "US"
}
}
)

// do something with the price or return it
}

Parameters

filtersPricingFiltersRequired
The filters to apply on prices.
The context used to select the prices. For example, you can specify the region ID in this context, and only prices having the same value will be retrieved.
sharedContextContext
A context used to share resources, such as transaction manager, between the application and the module.

Returns

PromisePromise<CalculatedPriceSet[]>Required
The calculated prices matching the context and filters provided.
Was this section helpful?