Get Product Variant Prices using Query

In this document, you'll learn how to retrieve product variant prices in the Medusa application using Query.

Why use Query?The Product Module doesn't provide pricing functionalities. The Medusa application links the Product Module's ProductVariant data model to the Pricing Module's PriceSet data model.So, to retrieve data across the linked records of the two modules, you use Query.

Retrieve All Product Variant Prices#

To retrieve all product variant prices, retrieve the product using Query and include among its fields variants.prices.*.

For example:

Code
1const { data: products } = await query.graph({2  entity: "product",3  fields: [4    "*",5    "variants.*",6    "variants.prices.*",7  ],8  filters: {9    id: [10      "prod_123",11    ],12  },13})

Each variant in the retrieved products has a prices array property with all the product variant prices. Each price object has the properties of the Pricing Module's Price data model.


Retrieve Calculated Price for a Context#

The Pricing Module can calculate prices of a variant based on a context, such as the region ID or the currency code.

TipLearn more about prices calculation in this Pricing Module documentation.

To retrieve calculated prices of variants based on a context, retrieve the products using Query and:

  • Pass variants.calculated_price.* in the fields property.
  • Pass a context property in the object parameter. Its value is an object of objects that sets the context for the retrieved fields.

For example:

Code
1import { QueryContext } from "@medusajs/framework/utils"2
3// ...4
5const { data: products } = await query.graph({6  entity: "product",7  fields: [8    "*",9    "variants.*",10    "variants.calculated_price.*",11  ],12  filters: {13    id: "prod_123",14  },15  context: {16    variants: {17      calculated_price: QueryContext({18        region_id: "reg_01J3MRPDNXXXDSCC76Y6YCZARS",19        currency_code: "eur",20      }),21    },22  },23})

For the context of the product variant's calculated price, you pass an object to context with the property variants, whose value is another object with the property calculated_price.

calculated_price's value is created using QueryContext from the Modules SDK, passing it a calculation context object.

Each variant in the retrieved products has a calculated_price object. Learn more about its properties in this Pricing Module guide.

Was this page helpful?
Edit this page