Raw
1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `Promotion Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Promotion Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the [Medusa Admin User Guide](!user-guide!/promotions) to learn how to manage promotions using the dashboard.
18
19 </Note>
20
21 Medusa has promotion related features available out-of-the-box through the Promotion Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Promotion Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## Promotion Features
30
31 - [Discount Functionalities](./concepts/page.mdx): A promotion discounts an amount or percentage of a cart's items, shipping methods, or the entire order.
32 - [Flexible Promotion Rules](./concepts/page.mdx#flexible-rules): A promotion has rules that restricts when the promotion is applied.
33 - [Campaign Management](./campaign/page.mdx): A campaign combines promotions under the same conditions, such as start and end dates, and budget configurations.
34 - [Apply Promotion on Carts and Orders](./actions/page.mdx): Apply promotions on carts and orders to discount items, shipping methods, or the entire order.
35
36 ---
37
38 ## How to Use the Promotion Module
39
40 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
41
42 You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package.
43
44 For example:
45
46 export const highlights = [
47 ["12", "Modules.PROMOTION", "Resolve the module in a step."]
48 ]
49
50 ```ts title="src/workflows/create-promotion.ts" highlights={highlights}
51 import {
52 createWorkflow,
53 WorkflowResponse,
54 createStep,
55 StepResponse,
56 } from "@medusajs/framework/workflows-sdk"
57 import { Modules } from "@medusajs/framework/utils"
58
59 const createPromotionStep = createStep(
60 "create-promotion",
61 async ({}, { container }) => {
62 const promotionModuleService = container.resolve(Modules.PROMOTION)
63
64 const promotion = await promotionModuleService.createPromotions({
65 code: "10%OFF",
66 type: "standard",
67 application_method: {
68 type: "percentage",
69 target_type: "order",
70 value: 10,
71 currency_code: "usd",
72 },
73 })
74
75 return new StepResponse({ promotion }, promotion.id)
76 },
77 async (promotionId, { container }) => {
78 if (!promotionId) {
79 return
80 }
81 const promotionModuleService = container.resolve(Modules.PROMOTION)
82
83 await promotionModuleService.deletePromotions(promotionId)
84 }
85 )
86
87 export const createPromotionWorkflow = createWorkflow(
88 "create-promotion",
89 () => {
90 const { promotion } = createPromotionStep()
91
92 return new WorkflowResponse({
93 promotion,
94 })
95 }
96 )
97 ```
98
99 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
100
101 <CodeTabs group="resource-types">
102 <CodeTab label="API Route" value="api-route">
103
104 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
105 import type {
106 MedusaRequest,
107 MedusaResponse,
108 } from "@medusajs/framework/http"
109 import { createPromotionWorkflow } from "../../workflows/create-cart"
110
111 export async function GET(
112 req: MedusaRequest,
113 res: MedusaResponse
114 ) {
115 const { result } = await createPromotionWorkflow(req.scope)
116 .run()
117
118 res.send(result)
119 }
120 ```
121
122 </CodeTab>
123 <CodeTab label="Subscriber" value="subscriber">
124
125 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
126 import {
127 type SubscriberConfig,
128 type SubscriberArgs,
129 } from "@medusajs/framework"
130 import { createPromotionWorkflow } from "../workflows/create-cart"
131
132 export default async function handleUserCreated({
133 event: { data },
134 container,
135 }: SubscriberArgs<{ id: string }>) {
136 const { result } = await createPromotionWorkflow(container)
137 .run()
138
139 console.log(result)
140 }
141
142 export const config: SubscriberConfig = {
143 event: "user.created",
144 }
145 ```
146
147 </CodeTab>
148 <CodeTab label="Scheduled Job" value="scheduled-job">
149
150 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
151 import { MedusaContainer } from "@medusajs/framework/types"
152 import { createPromotionWorkflow } from "../workflows/create-cart"
153
154 export default async function myCustomJob(
155 container: MedusaContainer
156 ) {
157 const { result } = await createPromotionWorkflow(container)
158 .run()
159
160 console.log(result)
161 }
162
163 export const config = {
164 name: "run-once-a-day",
165 schedule: `0 0 * * *`,
166 }
167 ```
168
169 </CodeTab>
170 </CodeTabs>
171
172 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
173
174 ---
175
176 <CommerceModuleSections />