278 lines
6.3 KiB
Plaintext
278 lines
6.3 KiB
Plaintext
import { CodeTabs, CodeTab } from "docs-ui"
|
||
|
||
export const metadata = {
|
||
title: `Examples of the Promotion Module`,
|
||
}
|
||
|
||
# {metadata.title}
|
||
|
||
In this document, you’ll find common examples of how you can use the Promotion Module in your application.
|
||
|
||
## Create a Promotion
|
||
|
||
<CodeTabs groupId="app-type">
|
||
<CodeTab label="Medusa API Router" value="medusa">
|
||
|
||
```ts
|
||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||
import { IPromotionModuleService } from "@medusajs/types"
|
||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||
|
||
export async function POST(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService =
|
||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||
|
||
const promotion = await promotionModuleService.create({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
},
|
||
})
|
||
|
||
res.json({ promotion })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
import { NextResponse } from "next/server"
|
||
|
||
import {
|
||
initialize as initializePromotionModule,
|
||
} from "@medusajs/promotion"
|
||
|
||
export async function POST(request: Request) {
|
||
const promotionModuleService =
|
||
await initializePromotionModule()
|
||
const body = await request.json()
|
||
|
||
const promotion = await promotionModuleService.create({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
},
|
||
})
|
||
|
||
return NextResponse.json({ promotion })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## Create a Campaign
|
||
|
||
<CodeTabs groupId="app-type">
|
||
<CodeTab label="Medusa API Router" value="medusa">
|
||
|
||
```ts
|
||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||
import { IPromotionModuleService } from "@medusajs/types"
|
||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||
|
||
export async function POST(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService =
|
||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||
|
||
const campaign = await promotionModuleService.createCampaigns(
|
||
{
|
||
name: "Summer Discounts",
|
||
campaign_identifier: "G-123445",
|
||
starts_at: new Date("2024-05-02"),
|
||
ends_at: new Date("2024-07-20"),
|
||
promotions: [
|
||
{
|
||
id: "promo_123",
|
||
},
|
||
],
|
||
}
|
||
)
|
||
|
||
res.json({ campaign })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
import { NextResponse } from "next/server"
|
||
|
||
import {
|
||
initialize as initializePromotionModule,
|
||
} from "@medusajs/promotion"
|
||
|
||
export async function POST(request: Request) {
|
||
const promotionModuleService =
|
||
await initializePromotionModule()
|
||
const body = await request.json()
|
||
|
||
const campaign = await promotionModuleService.createCampaigns(
|
||
{
|
||
name: "Summer Discounts",
|
||
campaign_identifier: "G-123445",
|
||
starts_at: new Date("2024-05-02"),
|
||
ends_at: new Date("2024-07-20"),
|
||
promotions: [
|
||
{
|
||
id: "promo_123",
|
||
},
|
||
],
|
||
}
|
||
)
|
||
|
||
return NextResponse.json({ campaign })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## Create a Promotion with Flexible Rules
|
||
|
||
<CodeTabs groupId="app-type">
|
||
<CodeTab label="Medusa API Router" value="medusa">
|
||
|
||
```ts
|
||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||
import { IPromotionModuleService } from "@medusajs/types"
|
||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||
|
||
export async function POST(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService =
|
||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||
|
||
const promotion = await promotionModuleService.create({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
},
|
||
rules: [
|
||
{
|
||
field: "customer_group_id",
|
||
operator: "eq",
|
||
values: ["VIP"],
|
||
},
|
||
],
|
||
})
|
||
|
||
res.json({ promotion })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
import { NextResponse } from "next/server"
|
||
|
||
import {
|
||
initialize as initializePromotionModule,
|
||
} from "@medusajs/promotion"
|
||
|
||
export async function POST(request: Request) {
|
||
const promotionModuleService =
|
||
await initializePromotionModule()
|
||
|
||
const promotion = await promotionModuleService.create({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
},
|
||
rules: [
|
||
{
|
||
field: "customer_group_id",
|
||
operator: "eq",
|
||
values: ["VIP"],
|
||
},
|
||
],
|
||
})
|
||
|
||
return NextResponse.json({ promotion })
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## List Promotions
|
||
|
||
<CodeTabs groupId="app-type">
|
||
<CodeTab label="Medusa API Router" value="medusa">
|
||
|
||
```ts
|
||
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
|
||
import { IPromotionModuleService } from "@medusajs/types"
|
||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||
|
||
export async function GET(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService =
|
||
request.scope.resolve(ModuleRegistrationName.PROMOTION)
|
||
|
||
res.json({
|
||
promotions: await promotionModuleService.list(),
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
<CodeTab label="Next.js App Router" value="nextjs">
|
||
|
||
```ts
|
||
import { NextResponse } from "next/server"
|
||
|
||
import {
|
||
initialize as initializePromotionModule,
|
||
} from "@medusajs/promotion"
|
||
|
||
export async function POST(request: Request) {
|
||
const promotionModuleService =
|
||
await initializePromotionModule()
|
||
|
||
return NextResponse.json({
|
||
promotions: await promotionModuleService.list(),
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## More Examples
|
||
|
||
The [module interface reference](/references/promotion) provides a reference to all the methods available for use with examples for each.
|