IPricingModuleService
Methods
addPrices
**addPrices**(data, sharedContext?): Promise<[PriceSetDTO](/references/services/interfaces/PriceSetDTO)>
This method adds prices to a price set.
Example
To add a default price to a price set, don't pass it any rules and make sure to pass it the currency_code
:
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function addPricesToPriceSet (priceSetId: string) {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.addPrices({
priceSetId,
prices: [
{
amount: 500,
currency_code: "USD",
rules: {},
},
],
})
// do something with the price set or return it
}
To add prices with rules:
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function addPricesToPriceSet (priceSetId: string) {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.addPrices({
priceSetId,
prices: [
{
amount: 300,
currency_code: "EUR",
rules: {
region_id: "PL",
city: "krakow"
},
},
{
amount: 400,
currency_code: "EUR",
min_quantity: 0,
max_quantity: 4,
rules: {
region_id: "PL"
},
},
{
amount: 450,
currency_code: "EUR",
rules: {
city: "krakow"
},
}
],
})
// do something with the price set or return it
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetDTO>
**addPrices**(data, sharedContext?): Promise<[PriceSetDTO](/references/services/interfaces/PriceSetDTO)[]>
This method adds prices to multiple price sets.
Example
To add a default price to a price set, don't pass it any rules and make sure to pass it the currency_code
:
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function addPricesToPriceSet (priceSetId: string) {
const pricingService = await initializePricingModule()
const priceSets = await pricingService.addPrices([{
priceSetId,
prices: [
{
amount: 500,
currency_code: "USD",
rules: {},
},
],
}])
// do something with the price sets or return them
}
To add prices with rules:
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function addPricesToPriceSet (priceSetId: string) {
const pricingService = await initializePricingModule()
const priceSets = await pricingService.addPrices([{
priceSetId,
prices: [
{
amount: 300,
currency_code: "EUR",
rules: {
region_id: "PL",
city: "krakow"
},
},
{
amount: 400,
currency_code: "EUR",
min_quantity: 0,
max_quantity: 4,
rules: {
region_id: "PL"
},
},
{
amount: 450,
currency_code: "EUR",
rules: {
city: "krakow"
},
}
],
}])
// do something with the price sets or return them
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetDTO[]>
addRules
**addRules**(data, sharedContext?): Promise<[PriceSetDTO](/references/services/interfaces/PriceSetDTO)>
This method adds rules to a price set.
Example
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function addRulesToPriceSet (priceSetId: string) {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.addRules({
priceSetId,
rules: [{
attribute: "region_id"
}]
})
// do something with the price set or return it
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetDTO>
**addRules**(data, sharedContext?): Promise<[PriceSetDTO](/references/services/interfaces/PriceSetDTO)[]>
This method adds rules to multiple price sets.
Example
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function addRulesToPriceSet (priceSetId: string) {
const pricingService = await initializePricingModule()
const priceSets = await pricingService.addRules([{
priceSetId,
rules: [{
attribute: "region_id"
}]
}])
// do something with the price sets or return them
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetDTO[]>
calculatePrices
**calculatePrices**(filters, context?, sharedContext?): Promise<[CalculatedPriceSetDTO](/references/services/interfaces/CalculatedPriceSetDTO)>
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
context
PricingContextsharedContext
ContextReturns
Promise<CalculatedPriceSetDTO>
create
**create**(data, sharedContext?): Promise<[PriceSetDTO](/references/services/interfaces/PriceSetDTO)>
This method is used to create a new price set.
Example
To create a default price set, don't pass any rules. For example:
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function createPriceSet() {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.create({
rules: [],
prices: [
{
amount: 500,
currency_code: "USD",
min_quantity: 0,
max_quantity: 4,
rules: {},
},
{
amount: 400,
currency_code: "USD",
min_quantity: 5,
max_quantity: 10,
rules: {},
},
],
})
// do something with the price set or return it
}
To create a price set and associate it with rule types:
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function createPriceSet() {
const pricingService = await initializePricingModule()
const priceSet = await pricingService.create({
rules: [{ rule_attribute: "region_id" }, { rule_attribute: "city" }],
prices: [
{
amount: 300,
currency_code: "EUR",
rules: {
region_id: "PL",
city: "krakow",
},
},
{
amount: 400,
currency_code: "EUR",
rules: {
region_id: "PL",
},
},
{
amount: 450,
currency_code: "EUR",
rules: {
city: "krakow",
},
},
],
})
// do something with the price set or return it
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetDTO>
**create**(data, sharedContext?): Promise<[PriceSetDTO](/references/services/interfaces/PriceSetDTO)[]>
This method is used to create multiple price sets.
Example
To create price sets with a default price, don't pass any rules and make sure to pass the currency_code
of the price. For example:
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function createPriceSets() {
const pricingService = await initializePricingModule()
const priceSets = await pricingService.create([
{
rules: [],
prices: [
{
amount: 500,
currency_code: "USD",
rules: {},
},
],
},
])
// do something with the price sets or return them
}
To create price sets and associate them with rule types:
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function createPriceSets() {
const pricingService = await initializePricingModule()
const priceSets = await pricingService.create([
{
rules: [{ rule_attribute: "region_id" }, { rule_attribute: "city" }],
prices: [
{
amount: 300,
currency_code: "EUR",
rules: {
region_id: "PL",
city: "krakow",
},
},
{
amount: 400,
currency_code: "EUR",
min_quantity: 0,
max_quantity: 4,
rules: {
region_id: "PL",
},
},
{
amount: 450,
currency_code: "EUR",
rules: {
city: "krakow",
},
},
],
},
])
// do something with the price sets or return them
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetDTO[]>
createCurrencies
**createCurrencies**(data, sharedContext?): Promise<[CurrencyDTO](/references/services/interfaces/CurrencyDTO)[]>
This method is used to create new currencies.
Example
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function createCurrencies() {
const pricingService = await initializePricingModule()
const currencies = await pricingService.createCurrencies([
{
code: "USD",
symbol: "$",
symbol_native: "$",
name: "US Dollar",
},
])
// do something with the currencies or return them
}
Parameters
sharedContext
ContextReturns
Promise<CurrencyDTO[]>
createMoneyAmounts
**createMoneyAmounts**(data, sharedContext?): Promise<[MoneyAmountDTO](/references/services/interfaces/MoneyAmountDTO)[]>
This method creates money amounts.
Example
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function retrieveMoneyAmounts() {
const pricingService = await initializePricingModule()
const moneyAmounts = await pricingService.createMoneyAmounts([
{
amount: 500,
currency_code: "USD",
},
{
amount: 400,
currency_code: "USD",
min_quantity: 0,
max_quantity: 4,
},
])
// do something with the money amounts or return them
}
Parameters
sharedContext
ContextReturns
Promise<MoneyAmountDTO[]>
createPriceRules
**createPriceRules**(data, sharedContext?): Promise<[PriceRuleDTO](/references/services/interfaces/PriceRuleDTO)[]>
This method is used to create new price rules based on the provided data.
Example
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function createPriceRules (
id: string,
priceSetId: string,
ruleTypeId: string,
value: string,
priceSetMoneyAmountId: string,
priceListId: string
) {
const pricingService = await initializePricingModule()
const priceRules = await pricingService.createPriceRules([
{
id,
price_set_id: priceSetId,
rule_type_id: ruleTypeId,
value,
price_set_money_amount_id: priceSetMoneyAmountId,
price_list_id: priceListId
}
])
// do something with the price rules or return them
}
Parameters
sharedContext
ContextReturns
Promise<PriceRuleDTO[]>
createPriceSetMoneyAmountRules
**createPriceSetMoneyAmountRules**(data, sharedContext?): Promise<[PriceSetMoneyAmountRulesDTO](/references/services/interfaces/PriceSetMoneyAmountRulesDTO)[]>
This method is used to create new price set money amount rules. A price set money amount rule creates an association between a price set money amount and a rule type.
Example
import {
initialize as initializePricingModule,
} from "@medusajs/pricing"
async function createPriceSetMoneyAmountRules (priceSetMoneyAmountId: string, ruleTypeId: string, value: string) {
const pricingService = await initializePricingModule()
const priceSetMoneyAmountRules = await pricingService.createPriceSetMoneyAmountRules([
{
price_set_money_amount: priceSetMoneyAmountId,
rule_type: ruleTypeId,
value
}
])
// do something with the price set money amount rules or return them
}
Parameters
sharedContext
ContextReturns
Promise<PriceSetMoneyAmountRulesDTO[]>
createRuleTypes
**createRuleTypes**(data, sharedContext?): Promise<[RuleTypeDTO](/references/services/interfaces/RuleTypeDTO)[]>
This method is used to create new rule types.
Example
import { initialize as initializePricingModule } from "@medusajs/pricing"
async function createRuleTypes() {
const pricingService = await initializePricingModule()
const ruleTypes = await pricingService.createRuleTypes([
{
name: "Region",
rule_attribute: "region_id",
},
])
// do something with the rule types or return them
}
Parameters
sharedContext
ContextReturns
Promise<RuleTypeDTO[]>
delete
**delete**(ids, sharedContext?): Promise<void>
This method deletes price sets by their IDs.
Example
Parameters
ids
string[]RequiredsharedContext
ContextReturns
Promise<void>
Promise
Promise<void>RequireddeleteCurrencies
**deleteCurrencies**(currencyCodes, sharedContext?): Promise<void>
This method is used to delete currencies based on their currency code.