1 import { CodeTabs, CodeTab, Table, Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `Links between Loyalty Module and Other Modules`,
5 }
6
7 # {metadata.title}
8
9 This document showcases the module links defined between the Loyalty Module and other Commerce Modules.
10
11 <Prerequisites
12 items={[
13 {
14 text: "Loyalty Plugin Installed",
15 link: "../page.mdx#1-install-the-loyalty-plugin",
16 }
17 ]}
18 />
19
20 ## Summary
21
22 The Loyalty Module has the following links to other modules:
23
24 <Table>
25 <Table.Header>
26 <Table.Row>
27 <Table.HeaderCell>
28 First Data Model
29 </Table.HeaderCell>
30 <Table.HeaderCell>
31 Second Data Model
32 </Table.HeaderCell>
33 <Table.HeaderCell>
34 Type
35 </Table.HeaderCell>
36 <Table.HeaderCell>
37 Description
38 </Table.HeaderCell>
39 </Table.Row>
40 </Table.Header>
41 <Table.Body>
42 <Table.Row>
43 <Table.Cell>
44 [Cart](/references/cart/models/Cart) in [Cart Module](../../cart/page.mdx)
45 </Table.Cell>
46 <Table.Cell>
47 `GiftCard` in [Loyalty Module](../page.mdx)
48 </Table.Cell>
49 <Table.Cell>
50 Stored - many-to-many
51 </Table.Cell>
52 <Table.Cell>
53 [Learn more](#cart-module)
54 </Table.Cell>
55 </Table.Row>
56 <Table.Row>
57 <Table.Cell>
58 [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx)
59 </Table.Cell>
60 <Table.Cell>
61 `GiftCard` in [Loyalty Module](../page.mdx)
62 </Table.Cell>
63 <Table.Cell>
64 Stored - many-to-many
65 </Table.Cell>
66 <Table.Cell>
67 [Learn more](#order-module)
68 </Table.Cell>
69 </Table.Row>
70 <Table.Row>
71 <Table.Cell>
72 `GiftCard` in [Loyalty Module](../page.mdx)
73 </Table.Cell>
74 <Table.Cell>
75 [OrderLineItem](/references/order/models/OrderLineItem) in [Order Module](../../order/page.mdx)
76 </Table.Cell>
77 <Table.Cell>
78 Read-only
79 </Table.Cell>
80 <Table.Cell>
81 [Learn more](#gift-card-and-order-line-item)
82 </Table.Cell>
83 </Table.Row>
84 <Table.Row>
85 <Table.Cell>
86 `GiftCard` in [Loyalty Module](../page.mdx)
87 </Table.Cell>
88 <Table.Cell>
89 `StoreCreditAccount` in [Store Credit Module](../../store-credit/page.mdx)
90 </Table.Cell>
91 <Table.Cell>
92 Stored - many-to-one
93 </Table.Cell>
94 <Table.Cell>
95 [Learn more](#store-credit-module)
96 </Table.Cell>
97 </Table.Row>
98 </Table.Body>
99 </Table>
100
101 ---
102
103 ## Cart Module
104
105 When a customer applies a gift card during checkout, Medusa defines a link between the `Cart` and `GiftCard` data models.
106
107 This allows you to retrieve the gift cards applied on a cart.
108
109 ### Retrieve with Query
110
111 To retrieve the gift cards of a cart with [Query](!docs!/learn/fundamentals/query), pass `gift_cards.*` in `fields`:
112
113 <CodeTabs group="relation-query">
114 <CodeTab label="query.graph" value="method">
115
116 ```ts
117 const { data: carts } = await query.graph({
118 entity: "cart",
119 fields: [
120 "gift_cards.*",
121 ],
122 })
123
124 // carts[0].gift_cards
125 ```
126
127 </CodeTab>
128 <CodeTab label="useQueryGraphStep" value="step">
129
130 ```ts
131 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
132
133 // ...
134
135 const { data: carts } = useQueryGraphStep({
136 entity: "cart",
137 fields: [
138 "gift_cards.*",
139 ],
140 })
141
142 // carts[0].gift_cards
143 ```
144
145 </CodeTab>
146 </CodeTabs>
147
148 ### Manage with Link
149
150 To manage the gift cards of a cart, use [Link](!docs!/learn/fundamentals/module-links/link):
151
152 <CodeTabs group="relation-link">
153 <CodeTab label="link.create" value="method">
154
155 ```ts
156 import { Modules } from "@medusajs/framework/utils"
157
158 // ...
159
160 await link.create({
161 [Modules.CART]: {
162 cart_id: "cart_123",
163 },
164 loyalty: {
165 gift_card_id: "gcard_123",
166 },
167 })
168 ```
169
170 </CodeTab>
171 <CodeTab label="createRemoteLinkStep" value="step">
172
173 ```ts
174 import { Modules } from "@medusajs/framework/utils"
175 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
176
177 // ...
178
179 createRemoteLinkStep({
180 [Modules.CART]: {
181 cart_id: "cart_123",
182 },
183 loyalty: {
184 gift_card_id: "gcard_123",
185 },
186 })
187 ```
188
189 </CodeTab>
190 </CodeTabs>
191
192 ---
193
194 ## Order Module
195
196 ### Order and Gift Cards
197
198 When a customer places an order that includes gift cards, Medusa defines a link between the `Order` and `GiftCard` data models.
199
200 This allows you to retrieve the gift cards applied on an order.
201
202 #### Retrieve with Query
203
204 To retrieve the gift cards of an order with [Query](!docs!/learn/fundamentals/query), pass `gift_cards.*` in `fields`:
205
206 <CodeTabs group="relation-query">
207 <CodeTab label="query.graph" value="method">
208
209 ```ts
210 const { data: orders } = await query.graph({
211 entity: "order",
212 fields: [
213 "gift_cards.*",
214 ],
215 })
216
217 // orders[0].gift_cards
218 ```
219
220 </CodeTab>
221 <CodeTab label="useQueryGraphStep" value="step">
222
223 ```ts
224 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
225
226 // ...
227
228 const { data: orders } = useQueryGraphStep({
229 entity: "order",
230 fields: [
231 "gift_cards.*",
232 ],
233 })
234
235 // orders[0].gift_cards
236 ```
237
238 </CodeTab>
239 </CodeTabs>
240
241 #### Manage with Link
242
243 To manage the gift cards of an order, use [Link](!docs!/learn/fundamentals/module-links/link):
244
245 <CodeTabs group="relation-link">
246 <CodeTab label="link.create" value="method">
247
248 ```ts
249 import { Modules } from "@medusajs/framework/utils"
250
251 // ...
252
253 await link.create({
254 [Modules.ORDER]: {
255 order_id: "order_123",
256 },
257 loyalty: {
258 gift_card_id: "gcard_123",
259 },
260 })
261 ```
262
263 </CodeTab>
264 <CodeTab label="createRemoteLinkStep" value="step">
265
266 ```ts
267 import { Modules } from "@medusajs/framework/utils"
268 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
269
270 // ...
271
272 createRemoteLinkStep({
273 [Modules.ORDER]: {
274 order_id: "order_123",
275 },
276 loyalty: {
277 gift_card_id: "gcard_123",
278 },
279 })
280 ```
281
282 </CodeTab>
283 </CodeTabs>
284
285 ### Gift Card and Order Line Item
286
287 Medusa defines a read-only link between the `GiftCard` data model and the [Order Module](../../order/page.mdx)'s `OrderLineItem` data model. This means you can retrieve the order line item where the gift card was purchased, but you don't manage the links in a pivot table in the database. The line item of a gift card is determined by the `line_item_id` property of the `GiftCard` data model.
288
289 #### Retrieve with Query
290
291 To retrieve the order line item of a gift card with [Query](!docs!/learn/fundamentals/query), pass `line_item.*` in `fields`:
292
293 <CodeTabs group="relation-query">
294 <CodeTab label="query.graph" value="method">
295
296 ```ts
297 const { data: giftCards } = await query.graph({
298 entity: "gift_card",
299 fields: [
300 "line_item.*",
301 ],
302 })
303
304 // giftCards[0].line_item
305 ```
306
307 </CodeTab>
308 <CodeTab label="useQueryGraphStep" value="step">
309
310 ```ts
311 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
312
313 // ...
314
315 const { data: giftCards } = useQueryGraphStep({
316 entity: "gift_card",
317 fields: [
318 "line_item.*",
319 ],
320 })
321
322 // giftCards[0].line_item
323 ```
324
325 </CodeTab>
326 </CodeTabs>
327
328 ---
329
330 ## Store Credit Module
331
332 Medusa defines a link between the `GiftCard` and `StoreCreditAccount` from the [Store Credit Module](../../store-credit/page.mdx). This link is created when a gift card is created (alongside its store credit account) or when a gift card is redeemed.
333
334 This allows you to retrieve the store credit account associated with a gift card.
335
336 ### Retrieve with Query
337
338 To retrieve the store credit account of a gift card with [Query](!docs!/learn/fundamentals/query), pass `store_credit_account.*` in `fields`:
339
340 <CodeTabs group="relation-query">
341 <CodeTab label="query.graph" value="method">
342
343 ```ts
344 const { data: giftCards } = await query.graph({
345 entity: "gift_card",
346 fields: [
347 "store_credit_account.*",
348 ],
349 })
350
351 // giftCards[0].store_credit_account
352 ```
353
354 </CodeTab>
355 <CodeTab label="useQueryGraphStep" value="step">
356
357 ```ts
358 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
359
360 // ...
361
362 const { data: giftCards } = useQueryGraphStep({
363 entity: "gift_card",
364 fields: [
365 "store_credit_account.*",
366 ],
367 })
368
369 // giftCards[0].store_credit_account
370 ```
371
372 </CodeTab>
373 </CodeTabs>
374
375 ### Manage with Link
376
377 To manage the store credit account of a gift card, use [Link](!docs!/learn/fundamentals/module-links/link):
378
379 <CodeTabs group="relation-link">
380 <CodeTab label="link.create" value="method">
381
382 ```ts
383 // ...
384
385 await link.create({
386 loyalty: {
387 gift_card_id: "gcard_123",
388 },
389 store_credit: {
390 store_credit_account_id: "sc_acc_123",
391 },
392 })
393 ```
394
395 </CodeTab>
396 <CodeTab label="createRemoteLinkStep" value="step">
397
398 ```ts
399 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
400
401 // ...
402
403 createRemoteLinkStep({
404 loyalty: {
405 gift_card_id: "gcard_123",
406 },
407 store_credit: {
408 store_credit_account_id: "sc_acc_123",
409 },
410 })
411 ```
412
413 </CodeTab>
414 </CodeTabs>