Back to Medusa

calculatePrices - Pricing Module Reference

www/apps/resources/references/pricing/IPricingModuleService/methods/pricing.IPricingModuleService.calculatePrices/page.mdx

2.14.213.1 KB
Original Source

import { TypeList } from "docs-ui"

calculatePrices - Pricing Module Reference

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

<Note>

You should only use this methods when implementing complex customizations. For common cases, check out available workflows instead.

</Note>

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
const price = await pricingModuleService.calculatePrices(
  {
    id: ["pset_123", "pset_321"],
  },
  {
    context: {
      currency_code: "usd",
    },
  }
)

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

ts
const price = await pricingModuleService.calculatePrices(
  {
    id: ["pset_123", "pset_321"],
  },
  {
    context: {
      currency_code: "usd",
      min_quantity: 4,
    },
  }
)

To calculate prices for custom rule types:

ts
const price = await pricingModuleService.calculatePrices(
  {
    id: ["pset_123", "pset_321"],
  },
  {
    context: {
      currency_code: "usd",
      region_id: "US",
    },
  }
)

Parameters

<TypeList types={[{"name":"filters","type":"PricingFilters","description":"The filters to apply on prices.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"string[]","description":"IDs of the price sets to use in the\ncalculation.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"context","type":"PricingContext","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, [BigNumberInput](../../../types/pricing.BigNumberInput/page.mdx)>","description":"an object whose keys are the name of the context attribute. Its value can be a string or a BigNumberInput. 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":"value","type":"string \| number","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"numeric","type":"number","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw","type":"BigNumberRawValue","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"bigNumber","type":"BigNumber","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"name":"sharedContext","type":"Context","description":"A context used to share resources, such as transaction manager, between the application and the module.","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":[]},{"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":"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":"enableNestedTransactions","type":"boolean","description":"A boolean value indicating whether nested transactions are enabled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"eventGroupId","type":"string","description":"A string indicating the ID of the group to aggregate the events to be emitted at a later point.","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":"runId","type":"string","description":"A string indicating the ID of the current run.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"messageAggregator","type":"IMessageAggregator","description":"An instance of a message aggregator, which is used to aggregate messages to be emitted at a later point.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"requestId","type":"string","description":"A string indicating the ID of the current request.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"idempotencyKey","type":"string","description":"A string indicating the idempotencyKey of the current workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"parentStepIdempotencyKey","type":"string","description":"A string indicating the idempotencyKey of the parent workflow execution.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"preventReleaseEvents","type":"boolean","description":"preventReleaseEvents","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"isCancelling","type":"boolean","description":"A boolean value indicating whether the current workflow execution is being cancelled.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"cancelingFromParentStep","type":"boolean","description":"Weither or not a sub workflow cancellation is being triggered from a parent step.\nIf true, the parent step will not be triggered by the sub workflow.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="calculatePrices"/>

Returns

<TypeList types={[{"name":"Promise","type":"Promise<CalculatedPriceSet[]>","optional":false,"defaultValue":"","description":"The calculated prices matching the context and filters provided.","expandable":false,"children":[{"name":"CalculatedPriceSet[]","type":"CalculatedPriceSet[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[{"name":"id","type":"string","description":"The ID of the price set.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"calculated_amount","type":"null \| BigNumberValue","description":"The amount of the calculated price, or null if there isn't a calculated price.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"numeric","type":"number","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw","type":"BigNumberRawValue","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"bigNumber","type":"BigNumber","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"raw_calculated_amount","type":"null \| BigNumberRawValue","description":"","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"value","type":"string \| number","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"original_amount","type":"null \| BigNumberValue","description":"The amount of the original price, or null if there isn't a calculated price.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"numeric","type":"number","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"raw","type":"BigNumberRawValue","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"bigNumber","type":"BigNumber","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"raw_original_amount","type":"null \| BigNumberRawValue","description":"","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"value","type":"string \| number","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"currency_code","type":"null \| string","description":"The currency code of the calculated price, or null if there isn't a calculated price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"is_calculated_price_price_list","type":"boolean","description":"Whether the calculated price is associated with a price list. During the calculation process, if no valid price list is found,\nthe calculated price is set to the original price, which doesn't belong to a price list. In that case, the value of this property is false.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"is_calculated_price_tax_inclusive","type":"boolean","description":"Whether the calculated price is tax inclusive or not.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"is_original_price_price_list","type":"boolean","description":"Whether the original price is associated with a price list. During the calculation process, if the price list of the calculated price is of type override,\nthe original price will be the same as the calculated price. In that case, the value of this property is true.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"is_original_price_tax_inclusive","type":"boolean","description":"Whether the original price is tax inclusive or not.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"calculated_price","type":"object","description":"The details of the calculated price.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"null \| string","description":"The ID of the price selected as the calculated price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"price_list_id","type":"null \| string","description":"The ID of the associated price list, if any.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"price_list_type","type":"null \| string","description":"The type of the associated price list, if any.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"min_quantity","type":"null \| BigNumberValue","description":"The min_quantity field defined on a price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"max_quantity","type":"null \| BigNumberValue","description":"The max_quantity field defined on a price.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"name":"original_price","type":"object","description":"The details of the original price.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"null \| string","description":"The ID of the price selected as the original price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"price_list_id","type":"null \| string","description":"The ID of the associated price list, if any.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"price_list_type","type":"null \| string","description":"The type of the associated price list, if any.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"min_quantity","type":"null \| BigNumberValue","description":"The min_quantity field defined on a price.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"max_quantity","type":"null \| BigNumberValue","description":"The max_quantity field defined on a price.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]}]}]} expandUrl="https://docs.medusajs.com/learn/fundamentals/data-models/manage-relationships#retrieve-records-of-relation" sectionTitle="calculatePrices"/>