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
@@ -60,7 +60,7 @@ Both prices are returned in an object along with the following properties:
{
name: "calculated_amount",
type: "`number`",
description: "The amount of the calculated price, or `null` if there isn't a calculated price."
description: "The amount of the calculated price, or `null` if there isn't a calculated price. This is the amount shown to the customer."
},
{
name: "is_original_price_price_list",
@@ -70,7 +70,7 @@ Both prices are returned in an object along with the following properties:
{
name: "original_amount",
type: "`number`",
description: "The amount of the original price, or `null` if there isn't an original price."
description: "The amount of the original price, or `null` if there isn't an original price. This amount is useful to compare with the `calculated_amount`, such as to check for discounted value."
},
{
name: "currency_code",
@@ -105,7 +105,7 @@ Both prices are returned in an object along with the following properties:
{
name: "price_list_type",
type: "`string`",
description: "The price list's type."
description: "The price list's type. For example, `sale`."
},
{
name: "min_quantity",
@@ -137,7 +137,7 @@ Both prices are returned in an object along with the following properties:
{
name: "price_list_type",
type: "`string`",
description: "The price list's type."
description: "The price list's type. For example, `sale`."
},
{
name: "min_quantity",
@@ -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).
@@ -447,6 +447,10 @@ export const filesMap = [
"filePath": "/www/apps/resources/app/commerce-modules/product/examples/page.mdx",
"pathname": "/commerce-modules/product/examples"
},
{
"filePath": "/www/apps/resources/app/commerce-modules/product/guides/price/page.mdx",
"pathname": "/commerce-modules/product/guides/price"
},
{
"filePath": "/www/apps/resources/app/commerce-modules/product/page.mdx",
"pathname": "/commerce-modules/product"
+17 -2
View File
@@ -4232,6 +4232,21 @@ export const generatedSidebar = [
}
]
},
{
"loaded": true,
"isPathHref": true,
"title": "Guides",
"autogenerate_path": "/commerce-modules/product/guides",
"children": [
{
"loaded": true,
"isPathHref": true,
"path": "/commerce-modules/product/guides/price",
"title": "Get Product Variant Prices",
"children": []
}
]
},
{
"loaded": true,
"isPathHref": true,
@@ -7957,7 +7972,7 @@ export const generatedSidebar = [
"loaded": true,
"isPathHref": true,
"path": "/deployment/admin/general",
"title": "General Deployment Guide for the Medusa Admin",
"title": "General Guide",
"children": []
},
{
@@ -7980,7 +7995,7 @@ export const generatedSidebar = [
"loaded": true,
"isPathHref": true,
"path": "/deployment/storefront/general",
"title": "General Deployment Guide for the Next.js Starter",
"title": "General Guide",
"children": []
},
{
+4
View File
@@ -763,6 +763,10 @@ export const sidebar = sidebarAttachHrefCommonOptions([
},
],
},
{
title: "Guides",
autogenerate_path: "/commerce-modules/product/guides",
},
{
title: "References",
children: [