docs: added guide on how to get product variant prices (#8282)

This commit is contained in:
Shahed Nasser
2024-07-26 10:09:41 +03:00
committed by GitHub
parent 639ecdfd74
commit ebd290b531
5 changed files with 125 additions and 6 deletions
@@ -0,0 +1,96 @@
---
sidebar_label: "Get Product Variant Prices"
---
export const metadata = {
title: `Get Product Variant Prices using Remote Query`,
}
# {metadata.title}
In this document, you'll learn how to retrieve product variant prices in the Medusa application using the [remote query](!docs!/advanced-development/modules/remote-query).
<Note title="Why use the Remote 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 the remote query.
</Note>
## Retrieve All Product Variant Prices
To retrieve all product variant prices, retrieve the product using the remote query and include among its fields `variants.prices.*`.
For example:
```ts highlights={[["6"]]}
const query = remoteQueryObjectFromString({
entryPoint: "product",
fields: [
"*",
"variants.*",
"variants.prices.*"
],
variables: {
filters: {
id: [
"prod_123"
]
},
}
})
// `result` is array of products
const result = await remoteQuery(query)
```
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](/references/pricing/models/Price).
---
## Retrieve Calculated Price for a Context
The Pricing Module can calculate prices of a variant based on a [context](../../../pricing/price-calculation/page.mdx#calculation-context), such as the region ID or the currency code.
<Note title="Tip">
Learn more about prices calculation in [this Pricing Module documentation](../../../pricing/price-calculation/page.mdx).
</Note>
To retrieve calculated prices of variants based on a context, retrieve the products using remote query and:
- Pass `variants.calculated_price.*` in the `fields` property.
- Pass in the `variables` property a `variants.calculated_price` property whose value is the [calculation context object](../../../pricing/price-calculation/page.mdx#calculation-context).
For example:
```ts highlights={[["6"], ["12"], ["13"], ["14"], ["15"], ["16"], ["17"]]}
const query = remoteQueryObjectFromString({
entryPoint: "product",
fields: [
"*",
"variants.*",
"variants.calculated_price.*"
],
variables: {
filters: {
id
},
"variants.calculated_price": {
context: {
region_id: "reg_01J3MRPDNXXXDSCC76Y6YCZARS",
currency_code: "eur"
}
}
}
})
// `result` is array of products
const result = await remoteQuery(query)
```
The `variants.calculated_price` property of `variables` is an object that has a `context` property. `context`'s value is an object whose keys are price rules, such as `region_id`, and value is the rule's value in this context, such as the customer's region's ID.
Each variant in the retrieved products has a `calculated_price` object. Learn more about its properties in [this Pricing Module guide](../../../pricing/price-calculation/page.mdx#returned-price-object).