From ebd290b53192010d854a5e73242e710fd05202cc Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Fri, 26 Jul 2024 10:09:41 +0300 Subject: [PATCH] docs: added guide on how to get product variant prices (#8282) --- .../pricing/price-calculation/page.mdx | 8 +- .../product/guides/price/page.mdx | 96 +++++++++++++++++++ www/apps/resources/generated/files-map.mjs | 4 + www/apps/resources/generated/sidebar.mjs | 19 +++- www/apps/resources/sidebar.mjs | 4 + 5 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 www/apps/resources/app/commerce-modules/product/guides/price/page.mdx diff --git a/www/apps/resources/app/commerce-modules/pricing/price-calculation/page.mdx b/www/apps/resources/app/commerce-modules/pricing/price-calculation/page.mdx index 82a4c1a35e..406b80235d 100644 --- a/www/apps/resources/app/commerce-modules/pricing/price-calculation/page.mdx +++ b/www/apps/resources/app/commerce-modules/pricing/price-calculation/page.mdx @@ -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", diff --git a/www/apps/resources/app/commerce-modules/product/guides/price/page.mdx b/www/apps/resources/app/commerce-modules/product/guides/price/page.mdx new file mode 100644 index 0000000000..ba271dbdab --- /dev/null +++ b/www/apps/resources/app/commerce-modules/product/guides/price/page.mdx @@ -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). + + + +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. + + + +## 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. + + + +Learn more about prices calculation in [this Pricing Module documentation](../../../pricing/price-calculation/page.mdx). + + + +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). diff --git a/www/apps/resources/generated/files-map.mjs b/www/apps/resources/generated/files-map.mjs index ce8d49f81e..50242c3d99 100644 --- a/www/apps/resources/generated/files-map.mjs +++ b/www/apps/resources/generated/files-map.mjs @@ -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" diff --git a/www/apps/resources/generated/sidebar.mjs b/www/apps/resources/generated/sidebar.mjs index 7b3f059aca..762730971f 100644 --- a/www/apps/resources/generated/sidebar.mjs +++ b/www/apps/resources/generated/sidebar.mjs @@ -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": [] }, { diff --git a/www/apps/resources/sidebar.mjs b/www/apps/resources/sidebar.mjs index e3b3af6065..c42382bb87 100644 --- a/www/apps/resources/sidebar.mjs +++ b/www/apps/resources/sidebar.mjs @@ -763,6 +763,10 @@ export const sidebar = sidebarAttachHrefCommonOptions([ }, ], }, + { + title: "Guides", + autogenerate_path: "/commerce-modules/product/guides", + }, { title: "References", children: [