- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
Get Product Variant Prices using Query
In this document, you'll learn how to retrieve product variant prices in the Medusa application using Query.
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:
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.
To retrieve calculated prices of variants based on a context, retrieve the products using Query and:
- Pass
variants.calculated_price.*
in thefields
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:
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.