|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Loyalty Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Loyalty Module and how to use it in your application. |
|
14
|
|
|
15
|
<Note> |
|
16
|
|
|
17
|
The Loyalty Module is part of the Loyalty Plugin. It's compatible with Medusa v2.14.0+. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
<Note title="Looking for no-code docs?"> |
|
22
|
|
|
23
|
Refer to the [Medusa Admin User Guide](!user-guide!/loyalty) to learn how to manage gift cards using the dashboard. |
|
24
|
|
|
25
|
</Note> |
|
26
|
|
|
27
|
Medusa has loyalty related features available through the Loyalty 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 Loyalty Module. |
|
28
|
|
|
29
|
<Note> |
|
30
|
|
|
31
|
Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation). |
|
32
|
|
|
33
|
</Note> |
|
34
|
|
|
35
|
## Features |
|
36
|
|
|
37
|
- [Gift Card Products](./gift-cards/page.mdx#gift-card-products): Create physical or digital gift cards as products that customers can purchase |
|
38
|
- [Gift Card Management](!user-guide!/loyalty/gift-cards): Issue, redeem, and track gift cards with unique codes |
|
39
|
- [Cart Integration](./gift-cards/page.mdx#2-apply-to-cart): Apply gift cards as payment during checkout |
|
40
|
|
|
41
|
--- |
|
42
|
|
|
43
|
## How to Use the Loyalty Module |
|
44
|
|
|
45
|
### 1. Install the Loyalty Plugin |
|
46
|
|
|
47
|
The Loyalty Module is part of the Loyalty Plugin. So, install the loyalty plugin: |
|
48
|
|
|
49
|
```bash npm2yarn |
|
50
|
npm install @medusajs/loyalty-plugin |
|
51
|
``` |
|
52
|
|
|
53
|
Then, add it to your `medusa-config.js`: |
|
54
|
|
|
55
|
```js title="medusa-config.js" |
|
56
|
module.exports = defineConfig({ |
|
57
|
// ... other configurations |
|
58
|
plugins: [ |
|
59
|
{ |
|
60
|
resolve: `@medusajs/loyalty-plugin`, |
|
61
|
options: {}, |
|
62
|
}, |
|
63
|
// ... other plugins |
|
64
|
], |
|
65
|
}) |
|
66
|
``` |
|
67
|
|
|
68
|
Refer to the [Plugin Options](./module-options/page.mdx) guide for a full list of options you can pass to the Loyalty Plugin. |
|
69
|
|
|
70
|
### 2. Run Migrations |
|
71
|
|
|
72
|
After installing the plugin, run the migrations to create the necessary tables in the database: |
|
73
|
|
|
74
|
```bash npx2yarn |
|
75
|
npx medusa db:migrate |
|
76
|
``` |
|
77
|
|
|
78
|
### 3. Use Loyalty Features in Your Application |
|
79
|
|
|
80
|
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. |
|
81
|
|
|
82
|
You can build custom workflows and steps. You can also re-use the Loyalty Plugin's workflows and steps. |
|
83
|
|
|
84
|
For example: |
|
85
|
|
|
86
|
export const highlights = [ |
|
87
|
["11", "loyalty", "Resolve the module in a step."] |
|
88
|
] |
|
89
|
|
|
90
|
```ts title="src/workflows/create-gift-card.ts" highlights={highlights} |
|
91
|
import { |
|
92
|
createWorkflow, |
|
93
|
WorkflowResponse, |
|
94
|
createStep, |
|
95
|
StepResponse, |
|
96
|
} from "@medusajs/framework/workflows-sdk" |
|
97
|
|
|
98
|
const createGiftCardStep = createStep( |
|
99
|
"create-gift-card", |
|
100
|
async ({}, { container }) => { |
|
101
|
const loyaltyModuleService = container.resolve("loyalty") |
|
102
|
|
|
103
|
const giftCard = await loyaltyModuleService.createGiftCards({ |
|
104
|
code: "test-code", |
|
105
|
value: 50, |
|
106
|
currency_code: "usd", |
|
107
|
expires_at: null, |
|
108
|
reference_id: "order_123", |
|
109
|
reference: "order", |
|
110
|
line_item_id: "litem_123", |
|
111
|
customer_id: "customer_123", |
|
112
|
}) |
|
113
|
|
|
114
|
return new StepResponse({ giftCard }, giftCard.id) |
|
115
|
}, |
|
116
|
async (giftCardId, { container }) => { |
|
117
|
const loyaltyModuleService = container.resolve("loyalty") |
|
118
|
|
|
119
|
await loyaltyModuleService.deleteGiftCards([giftCardId]) |
|
120
|
} |
|
121
|
) |
|
122
|
|
|
123
|
export const createGiftCardWorkflow = createWorkflow( |
|
124
|
"create-gift-card", |
|
125
|
() => { |
|
126
|
const { giftCard } = createGiftCardStep() |
|
127
|
|
|
128
|
return new WorkflowResponse({ |
|
129
|
giftCard, |
|
130
|
}) |
|
131
|
} |
|
132
|
) |
|
133
|
``` |
|
134
|
|
|
135
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
136
|
|
|
137
|
<CodeTabs group="resource-types"> |
|
138
|
<CodeTab label="API Route" value="api-route"> |
|
139
|
|
|
140
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
141
|
import type { |
|
142
|
MedusaRequest, |
|
143
|
MedusaResponse, |
|
144
|
} from "@medusajs/framework/http" |
|
145
|
import { createGiftCardWorkflow } from "../../workflows/create-gift-card" |
|
146
|
|
|
147
|
export async function GET( |
|
148
|
req: MedusaRequest, |
|
149
|
res: MedusaResponse |
|
150
|
) { |
|
151
|
const { result } = await createGiftCardWorkflow(req.scope) |
|
152
|
.run() |
|
153
|
|
|
154
|
res.send(result) |
|
155
|
} |
|
156
|
``` |
|
157
|
|
|
158
|
</CodeTab> |
|
159
|
<CodeTab label="Subscriber" value="subscriber"> |
|
160
|
|
|
161
|
```ts title="src/subscribers/order-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
162
|
import { |
|
163
|
type SubscriberConfig, |
|
164
|
type SubscriberArgs, |
|
165
|
} from "@medusajs/framework" |
|
166
|
import { createGiftCardWorkflow } from "../workflows/create-gift-card" |
|
167
|
|
|
168
|
export default async function handleOrderCreated({ |
|
169
|
event: { data }, |
|
170
|
container, |
|
171
|
}: SubscriberArgs<{ id: string }>) { |
|
172
|
const { result } = await createGiftCardWorkflow(container) |
|
173
|
.run() |
|
174
|
|
|
175
|
console.log(result) |
|
176
|
} |
|
177
|
|
|
178
|
export const config: SubscriberConfig = { |
|
179
|
event: "order.created", |
|
180
|
} |
|
181
|
``` |
|
182
|
|
|
183
|
</CodeTab> |
|
184
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
185
|
|
|
186
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
187
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
188
|
import { createGiftCardWorkflow } from "../workflows/create-gift-card" |
|
189
|
|
|
190
|
export default async function myCustomJob( |
|
191
|
container: MedusaContainer |
|
192
|
) { |
|
193
|
const { result } = await createGiftCardWorkflow(container) |
|
194
|
.run() |
|
195
|
|
|
196
|
console.log(result) |
|
197
|
} |
|
198
|
|
|
199
|
export const config = { |
|
200
|
name: "run-once-a-day", |
|
201
|
schedule: `0 0 * * *`, |
|
202
|
} |
|
203
|
``` |
|
204
|
|
|
205
|
</CodeTab> |
|
206
|
</CodeTabs> |
|
207
|
|
|
208
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
209
|
|
|
210
|
--- |
|
211
|
|
|
212
|
<CommerceModuleSections /> |