1 import { ChildDocs, CodeTabs, CodeTab } from "docs-ui"
2
3 export const metadata = {
4 title: `Loyalty Module Workflows`,
5 }
6
7 # {metadata.title}
8
9 [Workflows](!docs!/learn/fundamentals/workflows) are a series of queries and actions, called steps, that complete a task. By using workflows, you benefit from features like data consistency and a reliable roll-back mechanism.
10
11 The Loyalty Module provides workflows and steps to manage gift cards and their usage in carts. These are provided by the `@medusajs/loyalty-plugin` package.
12
13 ## Gift Card Management Workflows
14
15 ### Create Gift Cards Workflow
16
17 Create new gift cards for customers or orders:
18
19 ```ts
20 import { createGiftCardsWorkflow } from "@medusajs/loyalty-plugin/workflows"
21
22 const { result } = await createGiftCardsWorkflow(container)
23 .run({
24 input: {
25 code: "GC-XXXX",
26 value: 100,
27 currency_code: "usd",
28 customer_id: "cust_123",
29 line_item_id: "litem_123",
30 expires_at: null,
31 },
32 })
33 ```
34
35 ### Update Gift Cards Workflow
36
37 Update existing gift cards:
38
39 ```ts
40 import { updateGiftCardsWorkflow } from "@medusajs/loyalty-plugin/workflows"
41
42 const { result } = await updateGiftCardsWorkflow(container)
43 .run({
44 input: {
45 id: "gc_123",
46 value: 150,
47 status: "pending",
48 },
49 })
50 ```
51
52 ### Delete Gift Card Workflow
53
54 Delete a gift card:
55
56 ```ts
57 import { deleteGiftCardWorkflow } from "@medusajs/loyalty-plugin/workflows"
58
59 await deleteGiftCardWorkflow(container)
60 .run({
61 input: {
62 id: "gc_123",
63 },
64 })
65 ```
66
67 ## Cart Integration Workflows
68
69 ### Add Gift Card to Cart
70
71 Apply a gift card to a cart as a credit line. This workflow validates that the gift card has not expired before adding it:
72
73 ```ts
74 import { addGiftCardToCartWorkflow } from "@medusajs/loyalty-plugin/workflows"
75
76 await addGiftCardToCartWorkflow(container)
77 .run({
78 input: {
79 code: "GC-XXXX-XXXX",
80 cart_id: "cart_123",
81 },
82 })
83 ```
84
85 #### validate Hook
86
87 <Note>
88
89 This hook is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
90
91 </Note>
92
93 `addGiftCardToCartWorkflow` exposes a `validate` hook that runs after the gift card's built-in validations and before any credit line is created. Use it to add custom validation logic. Throw an error inside the hook to reject the gift card and stop the workflow.
94
95 ```ts
96 import { addGiftCardToCartWorkflow } from "@medusajs/loyalty-plugin/workflows"
97
98 addGiftCardToCartWorkflow.hooks.validate(
99 async ({ input, cart, giftCards }, { container }) => {
100 // throw an error to reject the gift card
101 if (giftCards.some((gc) => gc.currency_code !== cart.currency_code)) {
102 throw new Error("Gift card currency does not match cart currency.")
103 }
104 }
105 )
106 ```
107
108 The hook receives the workflow `input`, the resolved `cart`, and the resolved `giftCards` array. You can resolve services from `container` to perform database lookups or any other custom checks.
109
110 ### Remove Gift Card from Cart
111
112 Remove a gift card from a cart:
113
114 ```ts
115 import { removeGiftCardFromCartWorkflow } from "@medusajs/loyalty-plugin/workflows"
116
117 await removeGiftCardFromCartWorkflow(container)
118 .run({
119 input: {
120 code: "GC-XXXX-XXXX",
121 cart_id: "cart_123",
122 },
123 })
124 ```
125
126 ### Refresh Cart Gift Cards
127
128 Refresh gift card credit lines on a cart to account for balance or cart total changes. Expired gift cards are silently removed from the cart during a refresh rather than causing an error, so the cart can continue to be updated:
129
130 ```ts
131 import { refreshCartGiftCardsWorkflow } from "@medusajs/loyalty-plugin/workflows"
132
133 await refreshCartGiftCardsWorkflow(container)
134 .run({
135 input: {
136 cart_id: "cart_123",
137 },
138 })
139 ```
140
141 ### Confirm Cart Credit Lines
142
143 Confirm and debit gift card balances during checkout. This workflow also validates that none of the gift cards applied to the cart have expired before debiting any balances:
144
145 ```ts
146 import { confirmCartCreditLinesWorkflow } from "@medusajs/loyalty-plugin/workflows"
147
148 await confirmCartCreditLinesWorkflow(container)
149 .run({
150 input: {
151 cart_id: "cart_123",
152 },
153 })
154 ```
155
156 ## Order Integration Workflows
157
158 ### Link Gift Cards to Order
159
160 Link gift cards to an order after purchase:
161
162 ```ts
163 import { linkGiftCardsToOrderWorkflow } from "@medusajs/loyalty-plugin/workflows"
164
165 await linkGiftCardsToOrderWorkflow(container)
166 .run({
167 input: {
168 order_id: "order_123",
169 gift_card_codes: ["GC-XXXX", "GC-YYYY"],
170 },
171 })
172 ```
173
174 ### Claim Gift Card
175
176 Claim a gift card for a customer:
177
178 ```ts
179 import { claimGiftCardWorkflow } from "@medusajs/loyalty-plugin/workflows"
180
181 const { result } = await claimGiftCardWorkflow(container)
182 .run({
183 input: {
184 code: "GC-XXXX",
185 customer_id: "cust_123",
186 },
187 })
188 ```
189
190 ### Redeem Gift Card
191
192 Redeem a gift card by debiting its balance:
193
194 ```ts
195 import { redeemGiftCardWorkflow } from "@medusajs/loyalty-plugin/workflows"
196
197 const { result } = await redeemGiftCardWorkflow(container)
198 .run({
199 input: {
200 code: "GC-XXXX",
201 amount: 50,
202 reference: "cart",
203 reference_id: "cart_123",
204 },
205 })
206 ```
207
208 ---
209
210 ## Reference Documentation
211
212 For complete API references and additional workflows, refer to the [Medusa Core Workflows reference](/medusa-workflows-reference).
213
214 <ChildDocs type="item" defaultItemsPerRow={2} />