1 ---
2 products:
3 - customer
4 - order
5 - payment
6 - product
7 - promotion
8 - region
9 - sales channel
10 ---
11
12 import { CodeTabs, CodeTab, Table } from "docs-ui"
13
14 export const metadata = {
15 title: `Links between Cart Module and Other Modules`,
16 }
17
18 # {metadata.title}
19
20 This document showcases the module links defined between the Cart Module and other Commerce Modules.
21
22 ## Summary
23
24 The Cart Module has the following links to other modules:
25
26 <Note title="Tip">
27
28 Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database.
29
30 </Note>
31
32 <Table>
33 <Table.Header>
34 <Table.Row>
35 <Table.HeaderCell>
36 First Data Model
37 </Table.HeaderCell>
38 <Table.HeaderCell>
39 Second Data Model
40 </Table.HeaderCell>
41 <Table.HeaderCell>
42 Type
43 </Table.HeaderCell>
44 <Table.HeaderCell>
45 Description
46 </Table.HeaderCell>
47 </Table.Row>
48 </Table.Header>
49 <Table.Body>
50 <Table.Row>
51 <Table.Cell>
52 [Cart](/references/cart/models/Cart)
53 </Table.Cell>
54 <Table.Cell>
55 [Customer](/references/customer/models/Customer) in [Customer Module](../../customer/page.mdx)
56 </Table.Cell>
57 <Table.Cell>
58 Read-only - has one
59 </Table.Cell>
60 <Table.Cell>
61 [Learn more](#customer-module)
62 </Table.Cell>
63 </Table.Row>
64 <Table.Row>
65 <Table.Cell>
66 [ShippingMethod](/references/cart/models/ShippingMethod)
67 </Table.Cell>
68 <Table.Cell>
69 [ShippingOption](/references/fulfillment/models/ShippingOption) in [Fulfillment Module](../../fulfillment/page.mdx)
70 </Table.Cell>
71 <Table.Cell>
72 Read-only - has one
73 </Table.Cell>
74 <Table.Cell>
75 [Learn more](#fulfillment-module)
76 </Table.Cell>
77 </Table.Row>
78 <Table.Row>
79 <Table.Cell>
80 [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx)
81 </Table.Cell>
82 <Table.Cell>
83 [Cart](/references/cart/models/Cart)
84 </Table.Cell>
85 <Table.Cell>
86 Stored - one-to-one
87 </Table.Cell>
88 <Table.Cell>
89 [Learn more](#order-module)
90 </Table.Cell>
91 </Table.Row>
92 <Table.Row>
93 <Table.Cell>
94 [Cart](/references/cart/models/Cart)
95 </Table.Cell>
96 <Table.Cell>
97 [PaymentCollection](/references/payment/models/PaymentCollection) in [Payment Module](../../payment/page.mdx)
98 </Table.Cell>
99 <Table.Cell>
100 Stored - one-to-one
101 </Table.Cell>
102 <Table.Cell>
103 [Learn more](#payment-module)
104 </Table.Cell>
105 </Table.Row>
106 <Table.Row>
107 <Table.Cell>
108 [LineItem](/references/cart/models/LineItem)
109 </Table.Cell>
110 <Table.Cell>
111 [Product](/references/product/models/Product) in [Product Module](../../product/page.mdx)
112 </Table.Cell>
113 <Table.Cell>
114 Read-only - has one
115 </Table.Cell>
116 <Table.Cell>
117 [Learn more](#product-module)
118 </Table.Cell>
119 </Table.Row>
120 <Table.Row>
121 <Table.Cell>
122 [LineItem](/references/cart/models/LineItem)
123 </Table.Cell>
124 <Table.Cell>
125 [ProductVariant](/references/product/models/ProductVariant) in [Product Module](../../product/page.mdx)
126 </Table.Cell>
127 <Table.Cell>
128 Read-only - has one
129 </Table.Cell>
130 <Table.Cell>
131 [Learn more](#product-module)
132 </Table.Cell>
133 </Table.Row>
134 <Table.Row>
135 <Table.Cell>
136 [Cart](/references/cart/models/Cart)
137 </Table.Cell>
138 <Table.Cell>
139 [Promotion](/references/promotion/models/Promotion) in [Promotion Module](../../promotion/page.mdx)
140 </Table.Cell>
141 <Table.Cell>
142 Stored - many-to-many
143 </Table.Cell>
144 <Table.Cell>
145 [Learn more](#promotion-module)
146 </Table.Cell>
147 </Table.Row>
148 <Table.Row>
149 <Table.Cell>
150 [Cart](/references/cart/models/Cart)
151 </Table.Cell>
152 <Table.Cell>
153 [Region](/references/region/models/Region) in [Region Module](../../region/page.mdx)
154 </Table.Cell>
155 <Table.Cell>
156 Read-only - has one
157 </Table.Cell>
158 <Table.Cell>
159 [Learn more](#region-module)
160 </Table.Cell>
161 </Table.Row>
162 <Table.Row>
163 <Table.Cell>
164 [Cart](/references/cart/models/Cart)
165 </Table.Cell>
166 <Table.Cell>
167 [SalesChannel](/references/sales-channel/models/SalesChannel) in [Sales Channel Module](../../sales-channel/page.mdx)
168 </Table.Cell>
169 <Table.Cell>
170 Read-only - has one
171 </Table.Cell>
172 <Table.Cell>
173 [Learn more](#sales-channel-module)
174 </Table.Cell>
175 </Table.Row>
176 </Table.Body>
177 </Table>
178
179
180
181 ---
182
183 ## Customer Module
184
185 Medusa defines a read-only link between the `Cart` data model and the [Customer Module](../../customer/page.mdx)'s `Customer` data model. This means you can retrieve the details of a cart's customer, but you don't manage the links in a pivot table in the database. The customer of a cart is determined by the `customer_id` property of the `Cart` data model.
186
187 ### Retrieve with Query
188
189 To retrieve the customer of a cart with [Query](!docs!/learn/fundamentals/query), pass `customer.*` in `fields`:
190
191 <CodeTabs group="relation-query">
192 <CodeTab label="query.graph" value="method">
193
194 ```ts
195 const { data: carts } = await query.graph({
196 entity: "cart",
197 fields: [
198 "customer.*",
199 ],
200 })
201
202 // carts[0].customer
203 ```
204
205 </CodeTab>
206 <CodeTab label="useQueryGraphStep" value="step">
207
208 ```ts
209 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
210
211 // ...
212
213 const { data: carts } = useQueryGraphStep({
214 entity: "cart",
215 fields: [
216 "customer.*",
217 ],
218 })
219
220 // carts[0].customer
221 ```
222
223 </CodeTab>
224 </CodeTabs>
225
226 ---
227
228 ## Fulfillment Module
229
230 Medusa defines a read-only link between the `ShippingMethod` data model and the [Fulfillment Module](../../fulfillment/page.mdx)'s `ShippingOption` data model. This means you can retrieve the details of a shipping method's shipping option, but you don't manage the links in a pivot table in the database. The shipping option of a shipping method is determined by the `shipping_option_id` property of the `ShippingMethod` data model.
231
232 This link allows you to retrieve the shipping option that a shipping method was created from.
233
234 <Note>
235
236 This read-only link was added in [Medusa v2.10.0](https://github.com/medusajs/medusa/releases/tag/v2.10.0)
237
238 </Note>
239
240 ### Retrieve with Query
241
242 To retrieve the shipping option of a shipping method with [Query](!docs!/learn/fundamentals/query), pass `shipping_option.*` in `fields`:
243
244 <CodeTabs group="relation-query">
245 <CodeTab label="query.graph" value="method">
246
247 ```ts
248 const { data: shippingMethods } = await query.graph({
249 entity: "shipping_method",
250 fields: [
251 "shipping_option.*",
252 ],
253 })
254
255 // shippingMethods[0].shipping_option
256 ```
257
258 </CodeTab>
259 <CodeTab label="useQueryGraphStep" value="step">
260
261 ```ts
262 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
263
264 // ...
265
266 const { data: shippingMethods } = useQueryGraphStep({
267 entity: "shipping_method",
268 fields: [
269 "shipping_option.*",
270 ],
271 })
272
273 // shippingMethods[0].shipping_option
274 ```
275
276 </CodeTab>
277 </CodeTabs>
278
279 ## Order Module
280
281 The [Order Module](../../order/page.mdx) provides order-management features.
282
283 Medusa defines a link between the `Cart` and `Order` data models. The cart is linked to the order created once the cart is completed.
284
285 ![A diagram showcasing an example of how data models from the Cart and Order modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1728375735/Medusa%20Resources/cart-order_ijwmfs.jpg)
286
287 ### Retrieve with Query
288
289 To retrieve the order of a cart with [Query](!docs!/learn/fundamentals/query), pass `order.*` in `fields`:
290
291 <CodeTabs group="relation-query">
292 <CodeTab label="query.graph" value="method">
293
294 ```ts
295 const { data: carts } = await query.graph({
296 entity: "cart",
297 fields: [
298 "order.*",
299 ],
300 })
301
302 // carts[0].order
303 ```
304
305 </CodeTab>
306 <CodeTab label="useQueryGraphStep" value="step">
307
308 ```ts
309 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
310
311 // ...
312
313 const { data: carts } = useQueryGraphStep({
314 entity: "cart",
315 fields: [
316 "order.*",
317 ],
318 })
319
320 // carts[0].order
321 ```
322
323 </CodeTab>
324 </CodeTabs>
325
326 ### Manage with Link
327
328 To manage the order of a cart, use [Link](!docs!/learn/fundamentals/module-links/link):
329
330 <CodeTabs group="relation-link">
331 <CodeTab label="link.create" value="method">
332
333 ```ts
334 import { Modules } from "@medusajs/framework/utils"
335
336 // ...
337
338 await link.create({
339 [Modules.CART]: {
340 cart_id: "cart_123",
341 },
342 [Modules.ORDER]: {
343 order_id: "order_123",
344 },
345 })
346 ```
347
348 </CodeTab>
349 <CodeTab label="createRemoteLinkStep" value="step">
350
351 ```ts
352 import { Modules } from "@medusajs/framework/utils"
353 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
354
355 // ...
356
357 createRemoteLinkStep({
358 [Modules.CART]: {
359 cart_id: "cart_123",
360 },
361 [Modules.ORDER]: {
362 order_id: "order_123",
363 },
364 })
365 ```
366
367 </CodeTab>
368 </CodeTabs>
369
370 ---
371
372 ## Payment Module
373
374 The [Payment Module](../../payment/page.mdx) handles payment processing and management.
375
376 Medusa defines a link between the `Cart` and `PaymentCollection` data models. A cart has a payment collection which holds all the authorized payment sessions and payments made related to the cart.
377
378 ![A diagram showcasing an example of how data models from the Cart and Payment modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711537849/Medusa%20Resources/cart-payment_ixziqm.jpg)
379
380 ### Retrieve with Query
381
382 To retrieve the payment collection of a cart with [Query](!docs!/learn/fundamentals/query), pass `payment_collection.*` in `fields`:
383
384 <CodeTabs group="relation-query">
385 <CodeTab label="query.graph" value="method">
386
387 ```ts
388 const { data: carts } = await query.graph({
389 entity: "cart",
390 fields: [
391 "payment_collection.*",
392 ],
393 })
394
395 // carts[0].payment_collection
396 ```
397
398 </CodeTab>
399 <CodeTab label="useQueryGraphStep" value="step">
400
401 ```ts
402 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
403
404 // ...
405
406 const { data: carts } = useQueryGraphStep({
407 entity: "cart",
408 fields: [
409 "payment_collection.*",
410 ],
411 })
412
413 // carts[0].payment_collection
414 ```
415
416 </CodeTab>
417 </CodeTabs>
418
419 ### Manage with Link
420
421 To manage the payment collection of a cart, use [Link](!docs!/learn/fundamentals/module-links/link):
422
423 <CodeTabs group="relation-link">
424 <CodeTab label="link.create" value="method">
425
426 ```ts
427 import { Modules } from "@medusajs/framework/utils"
428
429 // ...
430
431 await link.create({
432 [Modules.CART]: {
433 cart_id: "cart_123",
434 },
435 [Modules.PAYMENT]: {
436 payment_collection_id: "paycol_123",
437 },
438 })
439 ```
440
441 </CodeTab>
442 <CodeTab label="createRemoteLinkStep" value="step">
443
444 ```ts
445 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
446
447 // ...
448
449 createRemoteLinkStep({
450 [Modules.CART]: {
451 cart_id: "cart_123",
452 },
453 [Modules.PAYMENT]: {
454 payment_collection_id: "paycol_123",
455 },
456 })
457 ```
458
459 </CodeTab>
460 </CodeTabs>
461
462 ---
463
464 ## Product Module
465
466 Medusa defines read-only links between:
467
468 - the `LineItem` data model and the [Product Module](../../product/page.mdx)'s `Product` data model. This means you can retrieve the details of a line item's product, but you don't manage the links in a pivot table in the database. The product of a line item is determined by the `product_id` property of the `LineItem` data model.
469 - the `LineItem` data model and the [Product Module](../../product/page.mdx)'s `ProductVariant` data model. This means you can retrieve the details of a line item's variant, but you don't manage the links in a pivot table in the database. The variant of a line item is determined by the `variant_id` property of the `LineItem` data model.
470
471 ### Retrieve with Query
472
473 To retrieve the variant of a line item with [Query](!docs!/learn/fundamentals/query), pass `variant.*` in `fields`:
474
475 <Note>
476
477 To retrieve the product, pass `product.*` in `fields`.
478
479 </Note>
480
481 <CodeTabs group="relation-query">
482 <CodeTab label="query.graph" value="method">
483
484 ```ts
485 const { data: lineItems } = await query.graph({
486 entity: "line_item",
487 fields: [
488 "variant.*",
489 ],
490 })
491
492 // lineItems.variant
493 ```
494
495 </CodeTab>
496 <CodeTab label="useQueryGraphStep" value="step">
497
498 ```ts
499 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
500
501 // ...
502
503 const { data: lineItems } = useQueryGraphStep({
504 entity: "line_item",
505 fields: [
506 "variant.*",
507 ],
508 })
509
510 // lineItems.variant
511 ```
512
513 </CodeTab>
514 </CodeTabs>
515
516 ---
517
518 ## Promotion Module
519
520 The [Promotion Module](../../promotion/page.mdx) provides discount features.
521
522 Medusa defines a link between the `Cart` and `Promotion` data models. This indicates the promotions applied on a cart.
523
524 ![A diagram showcasing an example of how data models from the Cart and Promotion modules are linked](https://res.cloudinary.com/dza7lstvk/image/upload/v1711538015/Medusa%20Resources/cart-promotion_kuh9vm.jpg)
525
526 Medusa also defines a read-only link between the `LineItemAdjustment` and `Promotion` data models. This means you can retrieve the details of the promotion applied on a line item, but you don't manage the links in a pivot table in the database. The promotion of a line item is determined by the `promotion_id` property of the `LineItemAdjustment` data model.
527
528 ### Retrieve with Query
529
530 To retrieve the promotions of a cart with [Query](!docs!/learn/fundamentals/query), pass `promotions.*` in `fields`:
531
532 <Note>
533
534 To retrieve the promotion of a line item adjustment, pass `promotion.*` in `fields`.
535
536 </Note>
537
538 <CodeTabs group="relation-query">
539 <CodeTab label="query.graph" value="method">
540
541 ```ts
542 const { data: carts } = await query.graph({
543 entity: "cart",
544 fields: [
545 "promotions.*",
546 ],
547 })
548
549 // carts[0].promotions
550 ```
551
552 </CodeTab>
553 <CodeTab label="useQueryGraphStep" value="step">
554
555 ```ts
556 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
557
558 // ...
559
560 const { data: carts } = useQueryGraphStep({
561 entity: "cart",
562 fields: [
563 "promotions.*",
564 ],
565 })
566
567 // carts[0].promotions
568 ```
569
570 </CodeTab>
571 </CodeTabs>
572
573 ### Manage with Link
574
575 To manage the promotions of a cart, use [Link](!docs!/learn/fundamentals/module-links/link):
576
577 <CodeTabs group="relation-link">
578 <CodeTab label="link.create" value="method">
579
580 ```ts
581 import { Modules } from "@medusajs/framework/utils"
582
583 // ...
584
585 await link.create({
586 [Modules.CART]: {
587 cart_id: "cart_123",
588 },
589 [Modules.PROMOTION]: {
590 promotion_id: "promo_123",
591 },
592 })
593 ```
594
595 </CodeTab>
596 <CodeTab label="createRemoteLinkStep" value="step">
597
598 ```ts
599 import { Modules } from "@medusajs/framework/utils"
600 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
601
602 // ...
603
604 createRemoteLinkStep({
605 [Modules.CART]: {
606 cart_id: "cart_123",
607 },
608 [Modules.PROMOTION]: {
609 promotion_id: "promo_123",
610 },
611 })
612 ```
613
614 </CodeTab>
615 </CodeTabs>
616
617 ---
618
619 ## Region Module
620
621 Medusa defines a read-only link between the `Cart` data model and the [Region Module](../../region/page.mdx)'s `Region` data model. This means you can retrieve the details of a cart's region, but you don't manage the links in a pivot table in the database. The region of a cart is determined by the `region_id` property of the `Cart` data model.
622
623 ### Retrieve with Query
624
625 To retrieve the region of a cart with [Query](!docs!/learn/fundamentals/query), pass `region.*` in `fields`:
626
627 <CodeTabs group="relation-query">
628 <CodeTab label="query.graph" value="method">
629
630 ```ts
631 const { data: carts } = await query.graph({
632 entity: "cart",
633 fields: [
634 "region.*",
635 ],
636 })
637
638 // carts[0].region
639 ```
640
641 </CodeTab>
642 <CodeTab label="useQueryGraphStep" value="step">
643
644 ```ts
645 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
646
647 // ...
648
649 const { data: carts } = useQueryGraphStep({
650 entity: "cart",
651 fields: [
652 "region.*",
653 ],
654 })
655
656 // carts[0].region
657 ```
658
659 </CodeTab>
660 </CodeTabs>
661
662 ---
663
664 ## Sales Channel Module
665
666 Medusa defines a read-only link between the `Cart` data model and the [Sales Channel Module](../../sales-channel/page.mdx)'s `SalesChannel` data model. This means you can retrieve the details of a cart's sales channel, but you don't manage the links in a pivot table in the database. The sales channel of a cart is determined by the `sales_channel_id` property of the `Cart` data model.
667
668 ### Retrieve with Query
669
670 To retrieve the sales channel of a cart with [Query](!docs!/learn/fundamentals/query), pass `sales_channel.*` in `fields`:
671
672 <CodeTabs group="relation-query">
673 <CodeTab label="query.graph" value="method">
674
675 ```ts
676 const { data: carts } = await query.graph({
677 entity: "cart",
678 fields: [
679 "sales_channel.*",
680 ],
681 })
682
683 // carts[0].sales_channel
684 ```
685
686 </CodeTab>
687 <CodeTab label="useQueryGraphStep" value="step">
688
689 ```ts
690 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
691
692 // ...
693
694 const { data: carts } = useQueryGraphStep({
695 entity: "cart",
696 fields: [
697 "sales_channel.*",
698 ],
699 })
700
701 // carts[0].sales_channel
702 ```
703
704 </CodeTab>
705 </CodeTabs>