* docs improvements and changes * updated module definition * modules + dml changes * fix build * fix vale error * fix lint errors * fixes to stripe docs * fix condition * fix condition * fix module defintion * fix checkout * disable UI action * change oas preview action * flatten provider module options * fix lint errors * add module link docs * pr comments fixes * fix vale error * change node engine version * links -> linkable * add note about database name * small fixes * link fixes * fix response code in api reference * added migrations step
260 lines
5.9 KiB
Plaintext
260 lines
5.9 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/utils"
|
||
|
||
export async function POST(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||
ModuleRegistrationName.PROMOTION
|
||
)
|
||
|
||
const promotion = await promotionModuleService.createPromotions({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
currency_code: "usd",
|
||
},
|
||
})
|
||
|
||
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.createPromotions({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
currency_code: "usd",
|
||
},
|
||
})
|
||
|
||
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/utils"
|
||
|
||
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"),
|
||
})
|
||
|
||
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"),
|
||
})
|
||
|
||
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/utils"
|
||
|
||
export async function POST(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||
ModuleRegistrationName.PROMOTION
|
||
)
|
||
|
||
const promotion = await promotionModuleService.createPromotions({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
currency_code: "usd",
|
||
},
|
||
rules: [
|
||
{
|
||
attribute: "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.createPromotions({
|
||
code: "10%OFF",
|
||
type: "standard",
|
||
application_method: {
|
||
type: "percentage",
|
||
target_type: "order",
|
||
value: "10",
|
||
currency_code: "usd",
|
||
},
|
||
rules: [
|
||
{
|
||
attribute: "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/utils"
|
||
|
||
export async function GET(
|
||
request: MedusaRequest,
|
||
res: MedusaResponse
|
||
): Promise<void> {
|
||
const promotionModuleService: IPromotionModuleService = request.scope.resolve(
|
||
ModuleRegistrationName.PROMOTION
|
||
)
|
||
|
||
res.json({
|
||
promotions: await promotionModuleService.listPromotions(),
|
||
})
|
||
}
|
||
```
|
||
|
||
</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 GET(request: Request) {
|
||
const promotionModuleService = await initializePromotionModule()
|
||
|
||
return NextResponse.json({
|
||
promotions: await promotionModuleService.listPromotions(),
|
||
})
|
||
}
|
||
```
|
||
|
||
</CodeTab>
|
||
</CodeTabs>
|
||
|
||
---
|
||
|
||
## More Examples
|
||
|
||
The [Promotion Module's main service reference](/references/promotion) provides a reference to all the methods available for use with examples for each.
|