docs: added examples for modules usage in Medusa (#5753)
* Add data migration script after running migrations * Update install-medusa.mdx * Update examples.md (#5751) * fix lint errors * fix comment in example * added examples for modules usage in Medusa --------- Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
@@ -1,257 +0,0 @@
|
|||||||
# Examples of Pricing Module
|
|
||||||
|
|
||||||
In this document, you’ll find common examples of how you can use the Pricing module in your application.
|
|
||||||
|
|
||||||
:::note
|
|
||||||
|
|
||||||
Examples in this section are in the context of a Next.js App Router.
|
|
||||||
|
|
||||||
:::
|
|
||||||
|
|
||||||
## Create a Price Set
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
const body = await request.json()
|
|
||||||
|
|
||||||
// A rule type with `rule_attribute=region_id` should
|
|
||||||
// already be present in the database
|
|
||||||
const priceSet = await pricingService.create([
|
|
||||||
{
|
|
||||||
rules: [{ rule_attribute: "region_id" }],
|
|
||||||
prices: [
|
|
||||||
{
|
|
||||||
currency_code: body.currency_code,
|
|
||||||
amount: body.amount,
|
|
||||||
rules: {
|
|
||||||
region_id: body.region_id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
return NextResponse.json({ price_set: priceSet })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## List Price Sets
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
|
|
||||||
const priceSets = await pricingService.list()
|
|
||||||
|
|
||||||
return NextResponse.json({ price_sets: priceSets })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Retrieve a Price Set by its ID
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
type ContextType = {
|
|
||||||
params: {
|
|
||||||
id: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: ContextType
|
|
||||||
) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
|
|
||||||
const priceSet = await pricingService.retrieve(params.id)
|
|
||||||
|
|
||||||
return NextResponse.json({ price_set: priceSet })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Create a Rule Type
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
const body = await request.json()
|
|
||||||
|
|
||||||
const ruleTypes = await pricingService.createRuleTypes([
|
|
||||||
{
|
|
||||||
name: body.name,
|
|
||||||
rule_attribute: body.rule_attribute,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
return NextResponse.json({ rule_types: ruleTypes })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Add Prices with Rules
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
const body = await request.json()
|
|
||||||
|
|
||||||
const priceSet = await pricingService.addPrices({
|
|
||||||
priceSetId: body.price_set_id,
|
|
||||||
prices: [
|
|
||||||
{
|
|
||||||
amount: 500,
|
|
||||||
currency_code: "USD",
|
|
||||||
rules: {
|
|
||||||
region_id: body.region_id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ price_set: priceSet })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Create a Currency
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
const body = await request.json()
|
|
||||||
|
|
||||||
const currencies = await pricingService.createCurrencies([{
|
|
||||||
code: "EUR",
|
|
||||||
symbol: "€",
|
|
||||||
symbol_native: "€",
|
|
||||||
name: "Euro",
|
|
||||||
}])
|
|
||||||
|
|
||||||
return NextResponse.json({ currencies })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## List Currencies
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
|
|
||||||
const currencies = await pricingService.listCurrencies()
|
|
||||||
|
|
||||||
return NextResponse.json({ currencies })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Create Price List
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
|
|
||||||
const priceLists = await pricingService.createPriceLists({
|
|
||||||
title: "My Sale",
|
|
||||||
type: "sale",
|
|
||||||
starts_at: Date.parse("01/10/2023"),
|
|
||||||
ends_at: Date.parse("31/10/2023"),
|
|
||||||
rules: {
|
|
||||||
region_id: ["DE", "DK"],
|
|
||||||
},
|
|
||||||
prices: [
|
|
||||||
{
|
|
||||||
amount: 400,
|
|
||||||
currency_code: "EUR",
|
|
||||||
price_set_id: priceSet.id,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ price_lists: priceLists })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Calculate Prices For a Currency
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializePricingModule,
|
|
||||||
} from "@medusajs/pricing"
|
|
||||||
|
|
||||||
type ContextType = {
|
|
||||||
params: {
|
|
||||||
id: string
|
|
||||||
currency_code: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: ContextType
|
|
||||||
) {
|
|
||||||
const pricingService = await initializePricingModule()
|
|
||||||
|
|
||||||
const price = await pricingService.calculatePrices({
|
|
||||||
id: [params.id],
|
|
||||||
}, {
|
|
||||||
context: {
|
|
||||||
currency_code: params.currency_code,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ price })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## More Examples
|
|
||||||
|
|
||||||
The [module interface reference](../../references/pricing/interfaces/IPricingModuleService.mdx) provides a reference to all the methods available for use with examples for each.
|
|
||||||
616
www/apps/docs/content/experimental/pricing/examples.mdx
Normal file
616
www/apps/docs/content/experimental/pricing/examples.mdx
Normal file
@@ -0,0 +1,616 @@
|
|||||||
|
|
||||||
|
import Tabs from '@theme/Tabs';
|
||||||
|
import TabItem from '@theme/TabItem';
|
||||||
|
|
||||||
|
# Examples of Pricing Module
|
||||||
|
|
||||||
|
In this document, you’ll find common examples of how you can use the Pricing module in your application.
|
||||||
|
|
||||||
|
## Create a Price Set
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
const body = await request.json()
|
||||||
|
|
||||||
|
// A rule type with `rule_attribute=region_id` should
|
||||||
|
// already be present in the database
|
||||||
|
const priceSet = await pricingService.create([
|
||||||
|
{
|
||||||
|
rules: [{ rule_attribute: "region_id" }],
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
currency_code: body.currency_code,
|
||||||
|
amount: body.amount,
|
||||||
|
rules: {
|
||||||
|
region_id: body.region_id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
return NextResponse.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A rule type with `rule_attribute=region_id` should
|
||||||
|
// already be present in the database
|
||||||
|
const priceSet = await pricingModuleService.create([
|
||||||
|
{
|
||||||
|
rules: [{ rule_attribute: "region_id" }],
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
currency_code: request.body.currency_code,
|
||||||
|
amount: request.body.amount,
|
||||||
|
rules: {
|
||||||
|
region_id: request.body.region_id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
response.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## List Price Sets
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
|
||||||
|
const priceSets = await pricingService.list()
|
||||||
|
|
||||||
|
return NextResponse.json({ price_sets: priceSets })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceSets = await pricingModuleService.list()
|
||||||
|
|
||||||
|
response.json({ price_sets: priceSets })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Retrieve a Price Set by its ID
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
type ContextType = {
|
||||||
|
params: {
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: ContextType
|
||||||
|
) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
|
||||||
|
const priceSet = await pricingService.retrieve(params.id)
|
||||||
|
|
||||||
|
return NextResponse.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceSet = await pricingModuleService.retrieve(request.params.id)
|
||||||
|
|
||||||
|
response.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Create a Rule Type
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
type ContextType = {
|
||||||
|
params: {
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: ContextType
|
||||||
|
) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
|
||||||
|
const priceSet = await pricingService.retrieve(params.id)
|
||||||
|
|
||||||
|
return NextResponse.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceSet = await pricingModuleService.retrieve(request.params.id)
|
||||||
|
|
||||||
|
response.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Add Prices with Rules
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
const body = await request.json()
|
||||||
|
|
||||||
|
const priceSet = await pricingService.addPrices({
|
||||||
|
priceSetId: body.price_set_id,
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
amount: 500,
|
||||||
|
currency_code: "USD",
|
||||||
|
rules: {
|
||||||
|
region_id: body.region_id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
return NextResponse.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceSet = await pricingModuleService.addPrices({
|
||||||
|
priceSetId: request.body.price_set_id,
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
amount: 500,
|
||||||
|
currency_code: "USD",
|
||||||
|
rules: {
|
||||||
|
region_id: request.body.region_id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
response.json({ price_set: priceSet })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Create a Currency
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
const body = await request.json()
|
||||||
|
|
||||||
|
const currencies = await pricingService.createCurrencies([{
|
||||||
|
code: "EUR",
|
||||||
|
symbol: "€",
|
||||||
|
symbol_native: "€",
|
||||||
|
name: "Euro",
|
||||||
|
}])
|
||||||
|
|
||||||
|
return NextResponse.json({ currencies })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const currencies = await pricingModuleService.createCurrencies([{
|
||||||
|
code: "EUR",
|
||||||
|
symbol: "€",
|
||||||
|
symbol_native: "€",
|
||||||
|
name: "Euro",
|
||||||
|
}])
|
||||||
|
|
||||||
|
response.json({ currencies })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## List Currencies
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
|
||||||
|
const currencies = await pricingService.listCurrencies()
|
||||||
|
|
||||||
|
return NextResponse.json({ currencies })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const currencies = await pricingModuleService.listCurrencies()
|
||||||
|
|
||||||
|
response.json({ currencies })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Create Price List
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
import { PriceListType } from "@medusajs/medusa";
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
|
||||||
|
const priceLists = await pricingService.createPriceLists([
|
||||||
|
{
|
||||||
|
title: "My Sale",
|
||||||
|
description: "This is my sale",
|
||||||
|
type: PriceListType.SALE,
|
||||||
|
starts_at: Date.parse("01/10/2023").toString(),
|
||||||
|
ends_at: Date.parse("31/10/2023").toString(),
|
||||||
|
rules: {
|
||||||
|
region_id: ["DE", "DK"],
|
||||||
|
},
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
amount: 400,
|
||||||
|
currency_code: "EUR",
|
||||||
|
price_set_id: "pset_124",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
|
return NextResponse.json({ price_lists: priceLists })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const priceLists = await pricingModuleService.createPriceLists([
|
||||||
|
{
|
||||||
|
title: "My Sale",
|
||||||
|
description: "This is my sale",
|
||||||
|
type: PriceListType.SALE,
|
||||||
|
starts_at: Date.parse("01/10/2023").toString(),
|
||||||
|
ends_at: Date.parse("31/10/2023").toString(),
|
||||||
|
rules: {
|
||||||
|
region_id: ["DE", "DK"],
|
||||||
|
},
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
amount: 400,
|
||||||
|
currency_code: "EUR",
|
||||||
|
price_set_id: "pset_124",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
|
response.json({ price_lists: priceLists })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Calculate Prices For a Currency
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializePricingModule,
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
type ContextType = {
|
||||||
|
params: {
|
||||||
|
id: string
|
||||||
|
currency_code: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: ContextType
|
||||||
|
) {
|
||||||
|
const pricingService = await initializePricingModule()
|
||||||
|
|
||||||
|
const price = await pricingService.calculatePrices({
|
||||||
|
id: [params.id],
|
||||||
|
}, {
|
||||||
|
context: {
|
||||||
|
currency_code: params.currency_code,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return NextResponse.json({ price })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
PricingModuleService
|
||||||
|
} from "@medusajs/pricing"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const pricingModuleService: PricingModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const price = await pricingModuleService.calculatePrices({
|
||||||
|
id: [request.params.id],
|
||||||
|
}, {
|
||||||
|
context: {
|
||||||
|
currency_code: request.params.currency_code,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
response.json({ price })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## More Examples
|
||||||
|
|
||||||
|
The [module interface reference](../../references/pricing/interfaces/IPricingModuleService.mdx) provides a reference to all the methods available for use with examples for each.
|
||||||
@@ -24,6 +24,10 @@ In `medusa-config.js`, add the pricing module to the exported object under the `
|
|||||||
```js title=medusa-config.js
|
```js title=medusa-config.js
|
||||||
module.exports = {
|
module.exports = {
|
||||||
// ...
|
// ...
|
||||||
|
featureFlags: {
|
||||||
|
// ...
|
||||||
|
medusa_v2: true,
|
||||||
|
},
|
||||||
modules: {
|
modules: {
|
||||||
// ...
|
// ...
|
||||||
pricingService: {
|
pricingService: {
|
||||||
@@ -37,10 +41,11 @@ module.exports = {
|
|||||||
|
|
||||||
## Step 3: Run Migrations
|
## Step 3: Run Migrations
|
||||||
|
|
||||||
Run the following command to reflect schema changes into your database:
|
Run the following commands to reflect schema changes into your database:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npx medusa migrations run
|
npx medusa migrations run
|
||||||
|
node node_modules/@medusajs/medusa/dist/scripts/migrate-to-pricing-module.js
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -69,9 +74,10 @@ For example:
|
|||||||
req: MedusaRequest,
|
req: MedusaRequest,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) {
|
) {
|
||||||
const pricingModuleService = req.scope.resolve(
|
const pricingModuleService: PricingModuleService =
|
||||||
"pricingModuleService"
|
req.scope.resolve(
|
||||||
)
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
pricings: pricingModuleService.list()
|
pricings: pricingModuleService.list()
|
||||||
@@ -97,9 +103,10 @@ For example:
|
|||||||
export default async function handleListPriceSets({
|
export default async function handleListPriceSets({
|
||||||
data, eventName, container, pluginOptions
|
data, eventName, container, pluginOptions
|
||||||
}: SubscriberArgs<Customer>) {
|
}: SubscriberArgs<Customer>) {
|
||||||
const pricingModuleService = container.resolve(
|
const pricingModuleService: PricingModuleService =
|
||||||
"pricingModuleService"
|
container.resolve(
|
||||||
)
|
"pricingModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
console.log(await pricingModuleService.list())
|
console.log(await pricingModuleService.list())
|
||||||
}
|
}
|
||||||
@@ -142,23 +149,10 @@ For example:
|
|||||||
</TabItem>
|
</TabItem>
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
||||||
:::tip
|
|
||||||
|
|
||||||
In the Examples or API Reference guides, you may see an initialization of the pricing module. This is only necessary if you're using the module outside the Medusa backend.
|
|
||||||
|
|
||||||
:::
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Up Next
|
## Start Development
|
||||||
|
|
||||||
<DocCard item={{
|
You can refer to the [Example Usages documentation page](./examples.mdx) for examples of using the Pricing module.
|
||||||
type: 'link',
|
|
||||||
href: '/references/pricing',
|
You can also refer to the [Module Interface Reference](../../references/pricing/interfaces/IPricingModuleService.mdx) for a detailed reference on all available methods.
|
||||||
label: 'Service Interface Reference',
|
|
||||||
customProps: {
|
|
||||||
icon: Icons['academic-cap-solid'],
|
|
||||||
description: 'Find a full reference of the module\' service interface\s methods.'
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|||||||
@@ -183,6 +183,6 @@ module.exports = nextConfig
|
|||||||
|
|
||||||
## Start Development
|
## Start Development
|
||||||
|
|
||||||
You can refer to the [Example Usages documentation page](./examples.md) for examples of using the Pricing module.
|
You can refer to the [Example Usages documentation page](./examples.mdx) for examples of using the Pricing module.
|
||||||
|
|
||||||
You can also refer to the [Module Interface Reference](../../references/pricing/interfaces/IPricingModuleService.mdx) for a detailed reference on all available methods.
|
You can also refer to the [Module Interface Reference](../../references/pricing/interfaces/IPricingModuleService.mdx) for a detailed reference on all available methods.
|
||||||
@@ -1,160 +0,0 @@
|
|||||||
# Examples of Product Module
|
|
||||||
|
|
||||||
In this document, you’ll find common examples of how you can use the Product module in your application.
|
|
||||||
|
|
||||||
:::note
|
|
||||||
|
|
||||||
Examples in this section are in the context of a Next.js App Router.
|
|
||||||
|
|
||||||
:::
|
|
||||||
|
|
||||||
## Create Product
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializeProductModule,
|
|
||||||
} from "@medusajs/product"
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
|
||||||
const productService = await initializeProductModule()
|
|
||||||
|
|
||||||
const products = await productService.create([
|
|
||||||
{
|
|
||||||
title: "Medusa Shirt",
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
title: "Color",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
variants: [
|
|
||||||
{
|
|
||||||
title: "Black Shirt",
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
value: "Black",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
return NextResponse.json({ products })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## List Products
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializeProductModule,
|
|
||||||
} from "@medusajs/product"
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const productService = await initializeProductModule()
|
|
||||||
|
|
||||||
const data = await productService.list()
|
|
||||||
|
|
||||||
return NextResponse.json({ products: data })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Retrieve a Product by its ID
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializeProductModule,
|
|
||||||
} from "@medusajs/product"
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Record<string, any> }) {
|
|
||||||
|
|
||||||
const { id } = params
|
|
||||||
const productService = await initializeProductModule()
|
|
||||||
|
|
||||||
const data = await productService.list({
|
|
||||||
id,
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ product: data[0] })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Retrieve a Product by its Handle
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializeProductModule,
|
|
||||||
} from "@medusajs/product"
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Record<string, any> }) {
|
|
||||||
|
|
||||||
const { handle } = params
|
|
||||||
const productService = await initializeProductModule()
|
|
||||||
|
|
||||||
const data = await productService.list({
|
|
||||||
handle,
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ product: data[0] })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Retrieve Categories
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializeProductModule,
|
|
||||||
} from "@medusajs/product"
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
|
||||||
const productService = await initializeProductModule()
|
|
||||||
|
|
||||||
const data = await productService.listCategories()
|
|
||||||
|
|
||||||
return NextResponse.json({ categories: data })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Retrieve Category by Handle
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { NextResponse } from "next/server"
|
|
||||||
|
|
||||||
import {
|
|
||||||
initialize as initializeProductModule,
|
|
||||||
} from "@medusajs/product"
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: Request,
|
|
||||||
{ params }: { params: Record<string, any> }) {
|
|
||||||
|
|
||||||
const { handle } = params
|
|
||||||
const productService = await initializeProductModule()
|
|
||||||
|
|
||||||
const data = await productService.listCategories({
|
|
||||||
handle,
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ category: data[0] })
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## More Examples
|
|
||||||
|
|
||||||
The [module interface reference](../../references/product/interfaces/IProductModuleService.mdx) provides a reference to all the methods available for use with examples for each.
|
|
||||||
386
www/apps/docs/content/experimental/product/examples.mdx
Normal file
386
www/apps/docs/content/experimental/product/examples.mdx
Normal file
@@ -0,0 +1,386 @@
|
|||||||
|
import Tabs from '@theme/Tabs';
|
||||||
|
import TabItem from '@theme/TabItem';
|
||||||
|
|
||||||
|
# Examples of Product Module
|
||||||
|
|
||||||
|
In this document, you’ll find common examples of how you can use the Product module in your application.
|
||||||
|
|
||||||
|
## Create Product
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializeProductModule,
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const productService = await initializeProductModule()
|
||||||
|
|
||||||
|
const products = await productService.create([
|
||||||
|
{
|
||||||
|
title: "Medusa Shirt",
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
title: "Color",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
title: "Black Shirt",
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
value: "Black",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
return NextResponse.json({ products })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
ProductModuleService
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const productModuleService: ProductModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const products = await productModuleService.create([
|
||||||
|
{
|
||||||
|
title: "Medusa Shirt",
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
title: "Color",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
title: "Black Shirt",
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
value: "Black",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
response.json({ products })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## List Products
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializeProductModule,
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const productService = await initializeProductModule()
|
||||||
|
|
||||||
|
const data = await productService.list()
|
||||||
|
|
||||||
|
return NextResponse.json({ products: data })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
ProductModuleService
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const productModuleService: ProductModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const data = await productModuleService.list()
|
||||||
|
|
||||||
|
response.json({ products: data })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Retrieve a Product by its ID
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializeProductModule,
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: Record<string, any> }) {
|
||||||
|
|
||||||
|
const { id } = params
|
||||||
|
const productService = await initializeProductModule()
|
||||||
|
|
||||||
|
const data = await productService.list({
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
|
||||||
|
return NextResponse.json({ product: data[0] })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
ProductModuleService
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const productModuleService: ProductModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const data = await productModuleService.list({
|
||||||
|
id: request.params.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
response.json({ product: data[0] })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Retrieve a Product by its Handle
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializeProductModule,
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: Record<string, any> }) {
|
||||||
|
|
||||||
|
const { handle } = params
|
||||||
|
const productService = await initializeProductModule()
|
||||||
|
|
||||||
|
const data = await productService.list({
|
||||||
|
handle,
|
||||||
|
})
|
||||||
|
|
||||||
|
return NextResponse.json({ product: data[0] })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
ProductModuleService
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const productModuleService: ProductModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const data = await productModuleService.list({
|
||||||
|
handle: request.params.handle,
|
||||||
|
})
|
||||||
|
|
||||||
|
response.json({ product: data[0] })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Retrieve Categories
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializeProductModule,
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const productService = await initializeProductModule()
|
||||||
|
|
||||||
|
const data = await productService.listCategories()
|
||||||
|
|
||||||
|
return NextResponse.json({ categories: data })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
ProductModuleService
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const productModuleService: ProductModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const data = await productModuleService.listCategories()
|
||||||
|
|
||||||
|
response.json({ categories: data })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
## Retrieve Category by Handle
|
||||||
|
|
||||||
|
<Tabs groupId="app-type" isCodeTabs={true}>
|
||||||
|
<TabItem value="nextjs" label="Next.js App Router" default>
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
import {
|
||||||
|
initialize as initializeProductModule,
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: Record<string, any> }) {
|
||||||
|
|
||||||
|
const { handle } = params
|
||||||
|
const productService = await initializeProductModule()
|
||||||
|
|
||||||
|
const data = await productService.listCategories({
|
||||||
|
handle,
|
||||||
|
})
|
||||||
|
|
||||||
|
return NextResponse.json({ category: data[0] })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="medusa" label="Medusa API Router">
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {
|
||||||
|
MedusaRequest,
|
||||||
|
MedusaResponse,
|
||||||
|
PriceListType
|
||||||
|
} from "@medusajs/medusa";
|
||||||
|
import {
|
||||||
|
ProductModuleService
|
||||||
|
} from "@medusajs/product"
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: MedusaRequest,
|
||||||
|
response: MedusaResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const productModuleService: ProductModuleService =
|
||||||
|
request.scope.resolve(
|
||||||
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
|
const data = await productModuleService.listCategories({
|
||||||
|
handle: request.params.handle,
|
||||||
|
})
|
||||||
|
|
||||||
|
response.json({ category: data[0] })
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## More Examples
|
||||||
|
|
||||||
|
The [module interface reference](../../references/product/interfaces/IProductModuleService.mdx) provides a reference to all the methods available for use with examples for each.
|
||||||
@@ -69,9 +69,10 @@ For example:
|
|||||||
req: MedusaRequest,
|
req: MedusaRequest,
|
||||||
res: MedusaResponse
|
res: MedusaResponse
|
||||||
) {
|
) {
|
||||||
const productModuleService = req.scope.resolve(
|
const productModuleService: ProductModuleService =
|
||||||
"productModuleService"
|
req.scope.resolve(
|
||||||
)
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
products: productModuleService.list()
|
products: productModuleService.list()
|
||||||
@@ -97,9 +98,10 @@ For example:
|
|||||||
export default async function handleListProducts({
|
export default async function handleListProducts({
|
||||||
data, eventName, container, pluginOptions
|
data, eventName, container, pluginOptions
|
||||||
}: SubscriberArgs<Customer>) {
|
}: SubscriberArgs<Customer>) {
|
||||||
const productModuleService = container.resolve(
|
const productModuleService: ProductModuleService =
|
||||||
"productModuleService"
|
container.resolve(
|
||||||
)
|
"productModuleService"
|
||||||
|
)
|
||||||
|
|
||||||
console.log(await productModuleService.list())
|
console.log(await productModuleService.list())
|
||||||
}
|
}
|
||||||
@@ -142,23 +144,10 @@ For example:
|
|||||||
</TabItem>
|
</TabItem>
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
||||||
:::tip
|
|
||||||
|
|
||||||
In the Examples or API Reference guides, you may see an initialization of the product module. This is only necessary if you're using the module outside the Medusa backend.
|
|
||||||
|
|
||||||
:::
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Up Next
|
## Start Development
|
||||||
|
|
||||||
<DocCard item={{
|
You can refer to the [Example Usages documentation page](./examples.mdx) for examples of using the Product module.
|
||||||
type: 'link',
|
|
||||||
href: '/references/product',
|
You can also refer to the [Module Interface Reference](../../references/product/interfaces/IProductModuleService.mdx) for a detailed reference on all available methods.
|
||||||
label: 'Service Interface Reference',
|
|
||||||
customProps: {
|
|
||||||
icon: Icons['academic-cap-solid'],
|
|
||||||
description: 'Find a full reference of the module\' service interface\s methods.'
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
@@ -193,6 +193,6 @@ module.exports = nextConfig
|
|||||||
|
|
||||||
## Start Development
|
## Start Development
|
||||||
|
|
||||||
You can refer to the [Example Usages documentation page](./examples.md) for examples of using the Product module.
|
You can refer to the [Example Usages documentation page](./examples.mdx) for examples of using the Product module.
|
||||||
|
|
||||||
You can also refer to the [Module Interface Reference](../../references/product/interfaces/IProductModuleService.mdx) for a detailed reference on all available methods.
|
You can also refer to the [Module Interface Reference](../../references/product/interfaces/IProductModuleService.mdx) for a detailed reference on all available methods.
|
||||||
|
|||||||
@@ -2751,6 +2751,11 @@ module.exports = {
|
|||||||
label: "Install in Node.js",
|
label: "Install in Node.js",
|
||||||
id: "experimental/product/install-nodejs",
|
id: "experimental/product/install-nodejs",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: "doc",
|
||||||
|
label: "Examples",
|
||||||
|
id: "experimental/product/examples",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: "html",
|
type: "html",
|
||||||
value: "References",
|
value: "References",
|
||||||
|
|||||||
Reference in New Issue
Block a user