|
1
|
--- |
|
2
|
sidebar_label: "Add Gift Message" |
|
3
|
tags: |
|
4
|
- cart |
|
5
|
- order |
|
6
|
- server |
|
7
|
- nextjs |
|
8
|
- tutorial |
|
9
|
products: |
|
10
|
- cart |
|
11
|
- order |
|
12
|
--- |
|
13
|
|
|
14
|
import { Github } from "@medusajs/icons" |
|
15
|
import { Prerequisites, WorkflowDiagram, CardList } from "docs-ui" |
|
16
|
|
|
17
|
export const metadata = { |
|
18
|
title: `Add Gift Message to Line Items in Medusa`, |
|
19
|
} |
|
20
|
|
|
21
|
# {metadata.title} |
|
22
|
|
|
23
|
In this tutorial, you will learn how to add a gift message to items in carts and orders in Medusa. |
|
24
|
|
|
25
|
When you install a Medusa application, you get a fully-fledged commerce platform with a Framework for customization. The Medusa application's commerce features are built around [Commerce Modules](../../../commerce-modules/page.mdx), which are available out-of-the-box. These features include cart and order management capabilities. |
|
26
|
|
|
27
|
You can customize the Medusa application and storefront to add a gift message to items in the cart. This feature allows customers to add a personalized message to their gifts, enhancing the shopping experience. |
|
28
|
|
|
29
|
## Summary |
|
30
|
|
|
31
|
By following this tutorial, you will learn how to: |
|
32
|
|
|
33
|
- Install and set up Medusa and the Next.js Starter Storefront. |
|
34
|
- Customize the storefront to support gift messages on cart items during checkout. |
|
35
|
- Customize the Medusa Admin to show gift items with messages in an order. |
|
36
|
|
|
37
|
You can follow this tutorial whether you're new to Medusa or an advanced Medusa developer. |
|
38
|
|
|
39
|
<Card |
|
40
|
title="View on Github" |
|
41
|
icon={Github} |
|
42
|
href="https://github.com/medusajs/examples/tree/main/order-gift-message" |
|
43
|
text="Find the full code for this tutorial." |
|
44
|
/> |
|
45
|
|
|
46
|
--- |
|
47
|
|
|
48
|
## Step 1: Install a Medusa Application |
|
49
|
|
|
50
|
<Prerequisites items={[ |
|
51
|
{ |
|
52
|
text: "Node.js v20.19.0+ or v22.12.0+", |
|
53
|
link: "https://nodejs.org/en/download" |
|
54
|
}, |
|
55
|
{ |
|
56
|
text: "Git CLI tool", |
|
57
|
link: "https://git-scm.com/downloads" |
|
58
|
}, |
|
59
|
{ |
|
60
|
text: "PostgreSQL", |
|
61
|
link: "https://www.postgresql.org/download/" |
|
62
|
} |
|
63
|
]} /> |
|
64
|
|
|
65
|
Start by installing the Medusa application on your machine with the following command: |
|
66
|
|
|
67
|
```bash npx2yarnExec |
|
68
|
npx create-medusa-app@latest |
|
69
|
``` |
|
70
|
|
|
71
|
First, you'll be asked for the project's name. Then, when prompted about installing the [Next.js Starter Storefront](../../../nextjs-starter/page.mdx), choose "Yes." |
|
72
|
|
|
73
|
Afterward, the installation process will start, which will install the Medusa application as a monorepository in a directory with your project's name. The backend is installed in the `apps/backend` directory, and the Next.js Starter Storefront is installed in the `apps/storefront` directory. |
|
74
|
|
|
75
|
Once the installation finishes successfully, the Medusa Admin dashboard will open with a form to create a new user. Enter the user's credentials and submit the form. Afterward, you can log in with the new user and explore the dashboard. |
|
76
|
|
|
77
|
<Note title="Ran into Errors?"> |
|
78
|
|
|
79
|
Check out the [troubleshooting guides](../../../troubleshooting/create-medusa-app-errors/page.mdx) for help. |
|
80
|
|
|
81
|
</Note> |
|
82
|
|
|
83
|
<Note> |
|
84
|
|
|
85
|
In this guide, all file paths of backend customizations are relative to the `apps/backend` directory of your Medusa project. |
|
86
|
|
|
87
|
</Note> |
|
88
|
|
|
89
|
--- |
|
90
|
|
|
91
|
## Step 2: Add Gift Inputs to Cart Item |
|
92
|
|
|
93
|
In this step, you'll customize the Next.js Starter Storefront to allow customers to specify that an item is a gift and add a gift message to it. |
|
94
|
|
|
95
|
You'll store the gift option and message in the cart item's `metadata` property, which is a key-value `jsonb` object that can hold any additional information about the item. When the customer places the order, the `metadata` is copied to the `metadata` of the order's line items. |
|
96
|
|
|
97
|
So, you only need to customize the storefront to add the gift message input and update the cart item metadata. |
|
98
|
|
|
99
|
### a. Changes to Update Item Function |
|
100
|
|
|
101
|
The Next.js Starter Storefront has an `updateLineItem` function that sends a request to the Medusa server to update the cart item. However, it doesn't support updating the `metadata` property. |
|
102
|
|
|
103
|
So, in `src/lib/data/cart.ts`, find the `updateLineItem` function and add a `metadata` property to its object parameter: |
|
104
|
|
|
105
|
```ts title="src/lib/data/cart.ts" badgeLabel="Storefront" badgeColor="blue" highlights={[["4"], ["8"]]} |
|
106
|
export async function updateLineItem({ |
|
107
|
lineId, |
|
108
|
quantity, |
|
109
|
metadata, |
|
110
|
}: { |
|
111
|
lineId: string |
|
112
|
quantity: number |
|
113
|
metadata?: Record<string, any> |
|
114
|
}) { |
|
115
|
// ... |
|
116
|
} |
|
117
|
``` |
|
118
|
|
|
119
|
Next, change the usage of `await sdk.store.cart.updateLineItem` in the function to pass the `metadata` property: |
|
120
|
|
|
121
|
```ts title="src/lib/data/cart.ts" badgeLabel="Storefront" badgeColor="blue" |
|
122
|
const updateData: any = { quantity } |
|
123
|
if (metadata) { |
|
124
|
updateData.metadata = metadata |
|
125
|
} |
|
126
|
|
|
127
|
await sdk.store.cart |
|
128
|
.updateLineItem(cartId, lineId, updateData, {}, headers) |
|
129
|
// ... |
|
130
|
``` |
|
131
|
|
|
132
|
You pass the `metadata` property to the Medusa server, which will update the cart item with the new metadata. |
|
133
|
|
|
134
|
### b. Add Gift Inputs |
|
135
|
|
|
136
|
Next, you'll modify the cart item component that's shown in the cart and checkout pages to show two inputs: one to specify that the item is a gift and another to add a gift message. |
|
137
|
|
|
138
|
In `src/modules/cart/components/item/index.tsx`, add the following imports at the top of the file: |
|
139
|
|
|
140
|
```tsx title="src/modules/cart/components/item/index.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
141
|
import { Checkbox, Textarea, Button, Label } from "@medusajs/ui" |
|
142
|
``` |
|
143
|
|
|
144
|
You import components from the [Medusa UI library](!ui!) that will be useful for the gift inputs. |
|
145
|
|
|
146
|
Next, in the `Item` component, add the following variables before the `changeQuantity` function: |
|
147
|
|
|
148
|
export const giftVarsHighlights = [ |
|
149
|
["1", "giftUpdating", "Track whether the gift message is being updated"], |
|
150
|
["2", "newGiftMessage", "Hold the new gift message input value."], |
|
151
|
["5", "isEditingGiftMessage", "Track whether the gift message input is being edited."], |
|
152
|
["7", "isGift", "Indicate whether the item is a gift."], |
|
153
|
["8", "giftMessage", "Current gift message from item's metadata."] |
|
154
|
] |
|
155
|
|
|
156
|
```tsx title="src/modules/cart/components/item/index.tsx" badgeLabel="Storefront" badgeColor="blue" highlights={giftVarsHighlights} |
|
157
|
const [giftUpdating, setGiftUpdating] = useState(false) |
|
158
|
const [newGiftMessage, setNewGiftMessage] = useState( |
|
159
|
item.metadata?.gift_message as string || "" |
|
160
|
) |
|
161
|
const [isEditingGiftMessage, setIsEditingGiftMessage] = useState(false) |
|
162
|
|
|
163
|
const isGift = item.metadata?.is_gift === "true" |
|
164
|
const giftMessage = item.metadata?.gift_message as string |
|
165
|
``` |
|
166
|
|
|
167
|
You define the following variables: |
|
168
|
|
|
169
|
- `giftUpdating`: A state variable to track whether the gift message is being updated. This will be useful to handle loading and disabled states. |
|
170
|
- `newGiftMessage`: A state variable to hold the new gift message input value. |
|
171
|
- `isEditingGiftMessage`: A state variable to track whether the gift message input is being edited. This will be useful to show or hide the input field. |
|
172
|
- `isGift`: A boolean indicating whether the item is a gift based on the `metadata.is_gift` property. |
|
173
|
- `giftMessage`: The current gift message from the item's `metadata.gift_message` property. |
|
174
|
|
|
175
|
Next, add the following functions before the `return` statement to handle updates to the gift inputs: |
|
176
|
|
|
177
|
export const giftFunctionsHighlights = [ |
|
178
|
["1", "handleGiftToggle", "Handle the gift checkbox toggle."], |
|
179
|
["22", "handleSaveGiftMessage", "Handle saving the gift message."], |
|
180
|
["44", "handleStartEdit", "Enable editing the gift message input."], |
|
181
|
["48", "handleCancelEdit", "Cancel editing the gift message input."] |
|
182
|
] |
|
183
|
|
|
184
|
```tsx title="src/modules/cart/components/item/index.tsx" badgeLabel="Storefront" badgeColor="blue" highlights={giftFunctionsHighlights} |
|
185
|
const handleGiftToggle = async (checked: boolean) => { |
|
186
|
setGiftUpdating(true) |
|
187
|
|
|
188
|
try { |
|
189
|
const newMetadata = { |
|
190
|
is_gift: checked.toString(), |
|
191
|
gift_message: checked ? newGiftMessage : "", |
|
192
|
} |
|
193
|
|
|
194
|
await updateLineItem({ |
|
195
|
lineId: item.id, |
|
196
|
quantity: item.quantity, |
|
197
|
metadata: newMetadata, |
|
198
|
}) |
|
199
|
} catch (error) { |
|
200
|
console.error("Error updating gift status:", error) |
|
201
|
} finally { |
|
202
|
setGiftUpdating(false) |
|
203
|
} |
|
204
|
} |
|
205
|
|
|
206
|
const handleSaveGiftMessage = async () => { |
|
207
|
setGiftUpdating(true) |
|
208
|
|
|
209
|
try { |
|
210
|
const newMetadata = { |
|
211
|
is_gift: "true", |
|
212
|
gift_message: newGiftMessage, |
|
213
|
} |
|
214
|
|
|
215
|
await updateLineItem({ |
|
216
|
lineId: item.id, |
|
217
|
quantity: item.quantity, |
|
218
|
metadata: newMetadata, |
|
219
|
}) |
|
220
|
setIsEditingGiftMessage(false) |
|
221
|
} catch (error) { |
|
222
|
console.error("Error updating gift message:", error) |
|
223
|
} finally { |
|
224
|
setGiftUpdating(false) |
|
225
|
} |
|
226
|
} |
|
227
|
|
|
228
|
const handleStartEdit = () => { |
|
229
|
setIsEditingGiftMessage(true) |
|
230
|
} |
|
231
|
|
|
232
|
const handleCancelEdit = () => { |
|
233
|
setNewGiftMessage(giftMessage || "") |
|
234
|
setIsEditingGiftMessage(false) |
|
235
|
} |
|
236
|
``` |
|
237
|
|
|
238
|
You define the following functions: |
|
239
|
|
|
240
|
- `handleGiftToggle`: Used when the gift checkbox is toggled. It updates the cart item's metadata to set the `is_gift` and `gift_message` properties based on the checkbox state. |
|
241
|
- `handleSaveGiftMessage`: Used to save the gift message when the customer clicks the "Save" button. It updates the cart item's metadata with the new gift message. |
|
242
|
- `handleStartEdit`: Used to start editing the gift message input by setting the `isEditingGiftMessage` state to `true`. |
|
243
|
- `handleCancelEdit`: Used to cancel the gift message editing and reset the input value to the current gift message. |
|
244
|
|
|
245
|
Finally, you'll change the `return` statement to include the gift inputs. Replace the existing return statement with the following: |
|
246
|
|
|
247
|
```tsx title="src/modules/cart/components/item/index.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
248
|
return ( |
|
249
|
<div className="bg-white border border-gray-200 rounded-lg p-4"> |
|
250
|
<div className="flex gap-4"> |
|
251
|
{/* Product Image */} |
|
252
|
<div className="flex-shrink-0"> |
|
253
|
<LocalizedClientLink |
|
254
|
href={`/products/${item.product_handle}`} |
|
255
|
className={clx("flex", { |
|
256
|
"w-16": type === "preview", |
|
257
|
"w-20": type === "full", |
|
258
|
})} |
|
259
|
> |
|
260
|
<Thumbnail |
|
261
|
thumbnail={item.thumbnail} |
|
262
|
images={item.variant?.product?.images} |
|
263
|
size="square" |
|
264
|
/> |
|
265
|
</LocalizedClientLink> |
|
266
|
</div> |
|
267
|
|
|
268
|
{/* Product Details */} |
|
269
|
<div className="flex-1 min-w-0"> |
|
270
|
<div className="flex justify-between items-start mb-2"> |
|
271
|
<div className="flex-1 min-w-0"> |
|
272
|
<Text |
|
273
|
className="txt-medium-plus text-ui-fg-base truncate" |
|
274
|
data-testid="product-title" |
|
275
|
> |
|
276
|
{item.product_title} |
|
277
|
</Text> |
|
278
|
<LineItemOptions variant={item.variant} data-testid="product-variant" /> |
|
279
|
</div> |
|
280
|
</div> |
|
281
|
|
|
282
|
{/* Gift Options */} |
|
283
|
<div className="mb-3"> |
|
284
|
<div |
|
285
|
className="inline-flex items-center gap-2 px-3 py-1.5 bg-gray-50 rounded-full border hover:bg-gray-100 transition-colors disabled:opacity-50 disabled:cursor-not-allowed" |
|
286
|
> |
|
287
|
<Checkbox |
|
288
|
checked={isGift} |
|
289
|
onCheckedChange={handleGiftToggle} |
|
290
|
disabled={giftUpdating} |
|
291
|
className="w-4 h-4" |
|
292
|
id="is-gift" |
|
293
|
/> |
|
294
|
<Label htmlFor="is-gift"> |
|
295
|
This is a gift |
|
296
|
</Label> |
|
297
|
</div> |
|
298
|
|
|
299
|
{isGift && ( |
|
300
|
<div className="mt-3"> |
|
301
|
{isEditingGiftMessage ? ( |
|
302
|
<div className="space-y-2"> |
|
303
|
<div className="flex items-center gap-2"> |
|
304
|
<Text className="text-sm font-medium text-ui-fg-base"> |
|
305
|
Gift Message: |
|
306
|
</Text> |
|
307
|
<Text className="text-xs text-ui-fg-subtle">(optional)</Text> |
|
308
|
</div> |
|
309
|
<Textarea |
|
310
|
placeholder="Add a personal message..." |
|
311
|
value={newGiftMessage} |
|
312
|
onChange={(e) => setNewGiftMessage(e.target.value)} |
|
313
|
disabled={giftUpdating} |
|
314
|
className="w-full" |
|
315
|
rows={2} |
|
316
|
/> |
|
317
|
<div className="flex justify-end gap-2"> |
|
318
|
<Button |
|
319
|
size="small" |
|
320
|
variant="secondary" |
|
321
|
onClick={handleCancelEdit} |
|
322
|
disabled={giftUpdating} |
|
323
|
className="text-xs px-3 py-1" |
|
324
|
> |
|
325
|
Cancel |
|
326
|
</Button> |
|
327
|
<Button |
|
328
|
size="small" |
|
329
|
variant="primary" |
|
330
|
onClick={handleSaveGiftMessage} |
|
331
|
disabled={giftUpdating || newGiftMessage === giftMessage} |
|
332
|
className="text-xs px-3 py-1" |
|
333
|
> |
|
334
|
{giftUpdating ? <Spinner /> : "Save"} |
|
335
|
</Button> |
|
336
|
</div> |
|
337
|
</div> |
|
338
|
) : ( |
|
339
|
<div className="flex items-center justify-between"> |
|
340
|
<div className="flex items-center gap-2"> |
|
341
|
<Text className="text-sm font-medium text-ui-fg-base"> |
|
342
|
Gift Message: |
|
343
|
</Text> |
|
344
|
{giftMessage ? ( |
|
345
|
<Text className="text-sm text-ui-fg-subtle"> |
|
346
|
{giftMessage} |
|
347
|
</Text> |
|
348
|
) : ( |
|
349
|
<Text className="text-sm text-ui-fg-subtle italic"> |
|
350
|
No message added |
|
351
|
</Text> |
|
352
|
)} |
|
353
|
</div> |
|
354
|
<Button |
|
355
|
size="small" |
|
356
|
variant="secondary" |
|
357
|
onClick={handleStartEdit} |
|
358
|
className="text-xs px-2 py-1" |
|
359
|
> |
|
360
|
{giftMessage ? "Edit" : "Add"} |
|
361
|
</Button> |
|
362
|
</div> |
|
363
|
)} |
|
364
|
</div> |
|
365
|
)} |
|
366
|
</div> |
|
367
|
|
|
368
|
{/* Quantity and Actions */} |
|
369
|
{type === "full" && ( |
|
370
|
<div className="flex items-center justify-between"> |
|
371
|
<div className="flex items-center gap-2"> |
|
372
|
<DeleteButton id={item.id} data-testid="product-delete-button" /> |
|
373
|
<CartItemSelect |
|
374
|
value={item.quantity} |
|
375
|
onChange={(value) => changeQuantity(parseInt(value.target.value))} |
|
376
|
className="w-16 h-8 p-2" |
|
377
|
data-testid="product-select-button" |
|
378
|
> |
|
379
|
{/* TODO: Update this with the v2 way of managing inventory */} |
|
380
|
{Array.from( |
|
381
|
{ |
|
382
|
length: Math.min(maxQuantity, 10), |
|
383
|
}, |
|
384
|
(_, i) => ( |
|
385
|
<option value={i + 1} key={i}> |
|
386
|
{i + 1} |
|
387
|
</option> |
|
388
|
) |
|
389
|
)} |
|
390
|
</CartItemSelect> |
|
391
|
{updating && <Spinner />} |
|
392
|
</div> |
|
393
|
</div> |
|
394
|
)} |
|
395
|
|
|
396
|
{/* Preview Mode */} |
|
397
|
{type === "preview" && ( |
|
398
|
<div className="flex items-center justify-between"> |
|
399
|
<Text className="text-sm text-ui-fg-subtle"> |
|
400
|
Qty: {item.quantity} |
|
401
|
</Text> |
|
402
|
<LineItemUnitPrice |
|
403
|
item={item} |
|
404
|
style="tight" |
|
405
|
currencyCode={currencyCode} |
|
406
|
/> |
|
407
|
</div> |
|
408
|
)} |
|
409
|
|
|
410
|
<ErrorMessage error={error} data-testid="product-error-message" /> |
|
411
|
</div> |
|
412
|
</div> |
|
413
|
</div> |
|
414
|
) |
|
415
|
``` |
|
416
|
|
|
417
|
You replace the previous table row design with a card. In the card, you show the item's image, title, variant options, quantity, and price. |
|
418
|
|
|
419
|
You also show a checkbox to toggle the gift status of the item. If the item is a gift, you show a text area to add or edit the gift message. You only show the gift message input if the user clicks an "Add" or "Edit" button. |
|
420
|
|
|
421
|
The gift message input has a "Save" button to save the message and a "Cancel" button to cancel the editing. |
|
422
|
|
|
423
|
### c. Update Cart and Checkout Templates |
|
424
|
|
|
425
|
The items are previously shown on the cart and checkout pages in a table. However, since you've changed the item component to a card, you'll need to update the cart and checkout templates to replace the table with a list of items. |
|
426
|
|
|
427
|
In `src/modules/cart/templates/items.tsx`, which shows the items on the cart page, change the `return` statement to the following: |
|
428
|
|
|
429
|
```tsx title="src/modules/cart/templates/items.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
430
|
return ( |
|
431
|
<div> |
|
432
|
<div className="pb-3 flex items-center"> |
|
433
|
<Heading className="text-[2rem] leading-[2.75rem]">Cart</Heading> |
|
434
|
</div> |
|
435
|
<div className="space-y-4"> |
|
436
|
{items |
|
437
|
? items |
|
438
|
.sort((a, b) => { |
|
439
|
return (a.created_at ?? "") > (b.created_at ?? "") ? -1 : 1 |
|
440
|
}) |
|
441
|
.map((item) => { |
|
442
|
return ( |
|
443
|
<Item |
|
444
|
key={item.id} |
|
445
|
item={item} |
|
446
|
currencyCode={cart?.currency_code} |
|
447
|
/> |
|
448
|
) |
|
449
|
}) |
|
450
|
: repeat(5).map((i) => { |
|
451
|
return <SkeletonLineItem key={i} /> |
|
452
|
})} |
|
453
|
</div> |
|
454
|
</div> |
|
455
|
) |
|
456
|
``` |
|
457
|
|
|
458
|
You replace the table that was wrapping the items with a `div` that contains a list of `Item` components. |
|
459
|
|
|
460
|
Next, in `src/modules/cart/templates/preview.tsx` that shows the items in the checkout summary, change the `return` statement to the following: |
|
461
|
|
|
462
|
```tsx title="src/modules/cart/templates/preview.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
463
|
return ( |
|
464
|
<div |
|
465
|
className={clx("space-y-3 mt-4", { |
|
466
|
"pl-[1px] overflow-y-scroll overflow-x-hidden no-scrollbar max-h-[420px]": |
|
467
|
hasOverflow, |
|
468
|
})} |
|
469
|
> |
|
470
|
{items |
|
471
|
? items |
|
472
|
.sort((a, b) => { |
|
473
|
return (a.created_at ?? "") > (b.created_at ?? "") ? -1 : 1 |
|
474
|
}) |
|
475
|
.map((item) => { |
|
476
|
return ( |
|
477
|
<Item |
|
478
|
key={item.id} |
|
479
|
item={item} |
|
480
|
type="preview" |
|
481
|
currencyCode={cart.currency_code} |
|
482
|
/> |
|
483
|
) |
|
484
|
}) |
|
485
|
: repeat(5).map((i) => { |
|
486
|
return <SkeletonLineItem key={i} /> |
|
487
|
})} |
|
488
|
</div> |
|
489
|
) |
|
490
|
``` |
|
491
|
|
|
492
|
Similarly, you replace the table that was wrapping the items with a `div` that contains a list of `Item` components. |
|
493
|
|
|
494
|
### Test the Changes |
|
495
|
|
|
496
|
You can now test the gift inputs during checkout. |
|
497
|
|
|
498
|
First, start the Medusa application by running the following command in the Medusa application directory: |
|
499
|
|
|
500
|
```bash npm2yarn badgeLabel="Medusa Application" badgeColor="green" |
|
501
|
npm run dev |
|
502
|
``` |
|
503
|
|
|
504
|
Then, start the Next.js Starter Storefront by running the following command in the storefront directory: |
|
505
|
|
|
506
|
```bash npm2yarn badgeLabel="Storefront" badgeColor="blue" |
|
507
|
npm run dev |
|
508
|
``` |
|
509
|
|
|
510
|
Open the storefront in your browser at `http://localhost:8000`. Add a product to the cart, and proceed to the checkout page. You'll find a checkbox to mark the item as a gift for each item. |
|
511
|
|
|
512
|
 |
|
513
|
|
|
514
|
If you check the box, you can also add and edit the gift message. The gift message will be saved in the cart item's `metadata`. |
|
515
|
|
|
516
|
 |
|
517
|
|
|
518
|
--- |
|
519
|
|
|
520
|
## Step 3: Show Gift Options in Order Confirmation Page |
|
521
|
|
|
522
|
Next, you'll customize the storefront to show the gift message of items in the order confirmation page. |
|
523
|
|
|
524
|
In `src/modules/order/components/item/index.tsx`, add in the `Item` component the following variables: |
|
525
|
|
|
526
|
```tsx title="src/modules/order/components/item/index.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
527
|
const isGift = item.metadata?.is_gift === "true" |
|
528
|
const giftMessage = item.metadata?.gift_message as string |
|
529
|
``` |
|
530
|
|
|
531
|
You define the following variables: |
|
532
|
|
|
533
|
- `isGift`: A boolean indicating whether the item is a gift based on the `metadata.is_gift` property. |
|
534
|
- `giftMessage`: The item's gift message from the `metadata.gift_message` property. |
|
535
|
|
|
536
|
Next, in the `return` statement, add the following below the `LineItemOptions` component: |
|
537
|
|
|
538
|
```tsx title="src/modules/order/components/item/index.tsx" badgeLabel="Storefront" badgeColor="blue" |
|
539
|
{isGift && <Text |
|
540
|
className="inline-block txt-medium text-ui-fg-subtle w-full overflow-hidden text-ellipsis" |
|
541
|
> |
|
542
|
Gift Message: {giftMessage || "No gift message provided"} |
|
543
|
</Text>} |
|
544
|
``` |
|
545
|
|
|
546
|
If the item is a gift, you show the gift message below the variant options. If no gift message is provided, you show "No gift message provided." |
|
547
|
|
|
548
|
### Test the Changes |
|
549
|
|
|
550
|
To test this change, place an order with a gift item. On the order confirmation page, you should see the gift message displayed below the variant options. |
|
551
|
|
|
552
|
 |
|
553
|
|
|
554
|
--- |
|
555
|
|
|
556
|
## Step 4: Show Gift Options in Admin Dashboard |
|
557
|
|
|
558
|
In this step, you'll customize the Medusa Admin dashboard to show the gift items with their messages in an order. |
|
559
|
|
|
560
|
### What is a Widget? |
|
561
|
|
|
562
|
The Medusa Admin dashboard's pages are customizable to insert [widgets](!docs!/learn/fundamentals/admin/widgets) of custom content in predefined injection zones. You create these widgets as React components that allow admin users to perform custom actions. |
|
563
|
|
|
564
|
So, to show the gift items with their messages in an order, you'll create a custom widget that shows the gift items in the order details page. |
|
565
|
|
|
566
|
### Create the Widget |
|
567
|
|
|
568
|
You create a widget in a `.tsx` file under the `src/admin/widgets` directory. So, in the Medusa application directory, create the file `src/admin/widgets/order-gift-items-widget.tsx` with the following content: |
|
569
|
|
|
570
|
```tsx title="src/admin/widgets/order-gift-items-widget.tsx" badgeLabel="Medusa Application" badgeColor="green" |
|
571
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
572
|
import { Container, Text } from "@medusajs/ui" |
|
573
|
import { DetailWidgetProps, AdminOrder } from "@medusajs/framework/types" |
|
574
|
|
|
575
|
const OrderGiftItemsWidget = ({ data }: DetailWidgetProps<AdminOrder>) => { |
|
576
|
const giftItems = data.items?.filter( |
|
577
|
(item: any) => item.metadata?.is_gift === "true" |
|
578
|
) |
|
579
|
|
|
580
|
if (!giftItems?.length) { |
|
581
|
return null |
|
582
|
} |
|
583
|
|
|
584
|
return ( |
|
585
|
<Container className="mb-4"> |
|
586
|
<Text className="font-medium h2-core mb-2">Gift Items & Messages</Text> |
|
587
|
<div className="flex flex-col gap-4"> |
|
588
|
{giftItems.map((item: any) => ( |
|
589
|
<div key={item.id} className="border-b last:border-b-0 pb-2"> |
|
590
|
<Text className="font-medium">{item.title} (x{item.quantity})</Text> |
|
591
|
<Text className="text-sm text-gray-600"> |
|
592
|
Gift Message: {item.metadata?.gift_message || "(No message)"} |
|
593
|
</Text> |
|
594
|
</div> |
|
595
|
))} |
|
596
|
</div> |
|
597
|
</Container> |
|
598
|
) |
|
599
|
} |
|
600
|
|
|
601
|
export const config = defineWidgetConfig({ |
|
602
|
zone: "order.details.side", |
|
603
|
}) |
|
604
|
|
|
605
|
export default OrderGiftItemsWidget |
|
606
|
``` |
|
607
|
|
|
608
|
A widget file must export: |
|
609
|
|
|
610
|
- A React component that renders the widget content. |
|
611
|
- A configuration object created with `defineWidgetConfig` that defines the widget's [injection zone](/admin-widget-injection-zones). |
|
612
|
|
|
613
|
You define the `OrderGiftItemsWidget` component that is injected in the `order.details.side` zone. Because it's injected in the order details page, it receives the order details as a `data` prop. |
|
614
|
|
|
615
|
In the component, you filter the order items to get only the gift items by checking if the `metadata.is_gift` property is set to `"true"`. Then, you render the gift items with their messages in a new "Gift Items & Messages" section. |
|
616
|
|
|
617
|
### Test Medusa Admin Changes |
|
618
|
|
|
619
|
To test out the Medusa Admin widget, start the Medusa application by running the following command in the Medusa application directory: |
|
620
|
|
|
621
|
```bash npm2yarn badgeLabel="Medusa Application" badgeColor="green" |
|
622
|
npm run dev |
|
623
|
``` |
|
624
|
|
|
625
|
Then, open the Medusa Admin at `http://localhost:9000/app` and log in with the user you created earlier. |
|
626
|
|
|
627
|
Go to the Orders page and click on an order that has gift items. You'll find a new section called "Gift Items & Messages" that shows the gift items with their messages. |
|
628
|
|
|
629
|
 |
|
630
|
|
|
631
|
--- |
|
632
|
|
|
633
|
## Optional: Handle Gift Items in Fulfillment Provider |
|
634
|
|
|
635
|
If you have a custom fulfillment provider and you want to handle gift items in it, you can do so in the `createFulfillment` method of the [Fulfillment Module Provider's service](/references/fulfillment/provider). |
|
636
|
|
|
637
|
For example: |
|
638
|
|
|
639
|
export const createFulfillmentHighlights = [ |
|
640
|
["9", "itemsWithGiftMessage", "Filter order items to find gift items."], |
|
641
|
] |
|
642
|
|
|
643
|
```ts title="Fulfillment Module Provider's Service" badgeLabel="Medusa Application" badgeColor="green" highlights={createFulfillmentHighlights} |
|
644
|
class ManualFulfillmentService extends utils_1.AbstractFulfillmentProviderService { |
|
645
|
// ... |
|
646
|
async createFulfillment( |
|
647
|
data: Record<string, unknown>, |
|
648
|
items: Partial<Omit<FulfillmentItemDTO, "fulfillment">>[], |
|
649
|
order: Partial<FulfillmentOrderDTO> | undefined, |
|
650
|
fulfillment: Partial<Omit<FulfillmentDTO, "provider_id" | "data" | "items">> |
|
651
|
): Promise<CreateFulfillmentResult> { |
|
652
|
const itemsWithGiftMessage = order.items?.filter((lineItem) => { |
|
653
|
const isInFulfillment = items.find( |
|
654
|
(item) => item.line_item_id === lineItem.id |
|
655
|
) |
|
656
|
if (!isInFulfillment) { |
|
657
|
return false |
|
658
|
} |
|
659
|
return lineItem.metadata?.is_gift === "true" |
|
660
|
}) |
|
661
|
|
|
662
|
// TODO pass gift items to third-party provider |
|
663
|
} |
|
664
|
} |
|
665
|
``` |
|
666
|
|
|
667
|
You filter the order items to find the items that are part of the fulfillment and are gift items. You can then process or pass them differently to your third-party provider. |
|
668
|
|
|
669
|
--- |
|
670
|
|
|
671
|
## Next Steps |
|
672
|
|
|
673
|
You've now added gift messages to items in carts and orders in Medusa. |
|
674
|
|
|
675
|
If you're new to Medusa, check out the [main documentation](!docs!/learn), where you'll get a more in-depth understanding of all the concepts you've used in this guide and more. |
|
676
|
|
|
677
|
To learn more about the commerce features that Medusa provides, check out Medusa's [Commerce Modules](../../../commerce-modules/page.mdx). |
|
678
|
|
|
679
|
### Troubleshooting |
|
680
|
|
|
681
|
If you encounter issues during your development, check out the [troubleshooting guides](../../../troubleshooting/page.mdx). |
|
682
|
|
|
683
|
### Getting Help |
|
684
|
|
|
685
|
If you encounter issues not covered in the troubleshooting guides: |
|
686
|
|
|
687
|
1. Visit the [Medusa GitHub repository](https://github.com/medusajs/medusa) to report issues or ask questions. |
|
688
|
2. Join the [Medusa Discord community](https://discord.gg/medusajs) for real-time support from community members. |