* adjusted configurations * enhancements to tool and configurations * change reference in docs * fixed issue in workflows reference * added project name * more optimizations * fix context error * added a types reference * resolved missing types * fix reference reflection types not having children * add an expand url parameter * added new option to the README * added details about new option
219 lines
6.4 KiB
Plaintext
219 lines
6.4 KiB
Plaintext
---
|
|
displayed_sidebar: pricingReference
|
|
badge:
|
|
variant: orange
|
|
text: Beta
|
|
slug: /references/pricing/calculatePrices
|
|
sidebar_label: calculatePrices
|
|
---
|
|
|
|
import ParameterTypes from "@site/src/components/ParameterTypes"
|
|
|
|
# calculatePrices - Pricing Module Reference
|
|
|
|
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:
|
|
|
|
```ts
|
|
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:
|
|
|
|
```ts
|
|
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:
|
|
|
|
```ts
|
|
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
|
|
|
|
<ParameterTypes parameters={[
|
|
{
|
|
"name": "filters",
|
|
"type": "[PricingFilters](../../pricing/interfaces/pricing.PricingFilters.mdx)",
|
|
"description": "The filters to apply on prices.",
|
|
"optional": false,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": [
|
|
{
|
|
"name": "id",
|
|
"type": "`string`[]",
|
|
"description": "IDs to filter prices.",
|
|
"optional": false,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"name": "context",
|
|
"type": "[PricingContext](../../pricing/interfaces/pricing.PricingContext.mdx)",
|
|
"description": "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\nwill be retrieved.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": [
|
|
{
|
|
"name": "context",
|
|
"type": "`Record<string, string \\| number>`",
|
|
"description": "an object whose keys are the name of the context attribute. Its value can be a string or a number. For example, you can pass the `currency_code` property with its value being the currency code to calculate the price in.\nAnother example is passing the `quantity` property to calculate the price for that specified quantity, which finds a price set whose `min_quantity` and `max_quantity` conditions match the specified quantity.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"name": "sharedContext",
|
|
"type": "[Context](../../pricing/interfaces/pricing.Context.mdx)",
|
|
"description": "A context used to share resources, such as transaction manager, between the application and the module.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": [
|
|
{
|
|
"name": "enableNestedTransactions",
|
|
"type": "`boolean`",
|
|
"description": "A boolean value indicating whether nested transactions are enabled.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
},
|
|
{
|
|
"name": "isolationLevel",
|
|
"type": "`string`",
|
|
"description": "A string indicating the isolation level of the context. Possible values are `READ UNCOMMITTED`, `READ COMMITTED`, `REPEATABLE READ`, or `SERIALIZABLE`.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
},
|
|
{
|
|
"name": "manager",
|
|
"type": "`TManager`",
|
|
"description": "An instance of a manager, typically an entity manager, of type `TManager`, which is a typed parameter passed to the context to specify the type of the `manager`.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
},
|
|
{
|
|
"name": "transactionId",
|
|
"type": "`string`",
|
|
"description": "A string indicating the ID of the current transaction.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
},
|
|
{
|
|
"name": "transactionManager",
|
|
"type": "`TManager`",
|
|
"description": "An instance of a transaction manager of type `TManager`, which is a typed parameter passed to the context to specify the type of the `transactionManager`.",
|
|
"optional": true,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
}
|
|
]
|
|
}
|
|
]} />
|
|
|
|
## Returns
|
|
|
|
<ParameterTypes parameters={[
|
|
{
|
|
"name": "Promise",
|
|
"type": "Promise<[CalculatedPriceSet](../../pricing/interfaces/pricing.CalculatedPriceSet.mdx)[]>",
|
|
"optional": false,
|
|
"defaultValue": "",
|
|
"description": "The calculated prices matching the context and filters provided.",
|
|
"expandable": false,
|
|
"children": [
|
|
{
|
|
"name": "CalculatedPriceSet[]",
|
|
"type": "[CalculatedPriceSet](../../pricing/interfaces/pricing.CalculatedPriceSet.mdx)[]",
|
|
"optional": false,
|
|
"defaultValue": "",
|
|
"description": "",
|
|
"expandable": false,
|
|
"children": [
|
|
{
|
|
"name": "CalculatedPriceSet",
|
|
"type": "`object`",
|
|
"description": "The calculated price for a specific price set and context.",
|
|
"optional": false,
|
|
"defaultValue": "",
|
|
"expandable": false,
|
|
"children": []
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]} />
|