1 ---
2 products:
3 - payment
4 - cart
5 - order
6 ---
7
8 import { CodeTabs, CodeTab, Table } from "docs-ui"
9
10 export const metadata = {
11 title: `Links between Customer Module and Other Modules`,
12 }
13
14 # {metadata.title}
15
16 This document showcases the module links defined between the Customer Module and other Commerce Modules.
17
18 ## Summary
19
20 The Customer Module has the following links to other modules:
21
22 <Note title="Tip">
23
24 Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database.
25
26 </Note>
27
28 <Table>
29 <Table.Header>
30 <Table.Row>
31 <Table.HeaderCell>
32 First Data Model
33 </Table.HeaderCell>
34 <Table.HeaderCell>
35 Second Data Model
36 </Table.HeaderCell>
37 <Table.HeaderCell>
38 Type
39 </Table.HeaderCell>
40 <Table.HeaderCell>
41 Description
42 </Table.HeaderCell>
43 </Table.Row>
44 </Table.Header>
45 <Table.Body>
46 <Table.Row>
47 <Table.Cell>
48 [Customer](/references/customer/models/Customer)
49 </Table.Cell>
50 <Table.Cell>
51 [AccountHolder](/references/payment/models/AccountHolder) in [Payment Module](../../payment/page.mdx)
52 </Table.Cell>
53 <Table.Cell>
54 Stored - many-to-many
55 </Table.Cell>
56 <Table.Cell>
57 [Learn more](#payment-module)
58 </Table.Cell>
59 </Table.Row>
60 <Table.Row>
61 <Table.Cell>
62 [Cart](/references/cart/models/Cart) in [Cart Module](../../cart/page.mdx)
63 </Table.Cell>
64 <Table.Cell>
65 [Customer](/references/customer/models/Customer)
66 </Table.Cell>
67 <Table.Cell>
68 Read-only - has one
69 </Table.Cell>
70 <Table.Cell>
71 [Learn more](#cart-module)
72 </Table.Cell>
73 </Table.Row>
74 <Table.Row>
75 <Table.Cell>
76 [Order](/references/order/models/Order) in [Order Module](../../order/page.mdx)
77 </Table.Cell>
78 <Table.Cell>
79 [Customer](/references/customer/models/Customer)
80 </Table.Cell>
81 <Table.Cell>
82 Read-only - has one
83 </Table.Cell>
84 <Table.Cell>
85 [Learn more](#order-module)
86 </Table.Cell>
87 </Table.Row>
88 </Table.Body>
89 </Table>
90
91
92 ---
93
94 ## Payment Module
95
96 Medusa defines a link between the `Customer` and `AccountHolder` data models, allowing payment providers to save payment methods for a customer, if the payment provider supports it.
97
98 <Note>
99
100 This link is available starting from Medusa `v2.5.0`.
101
102 </Note>
103
104 ### Retrieve with Query
105
106 To retrieve the account holder associated with a customer with [Query](!docs!/learn/fundamentals/query), pass `customer.*` in `fields`:
107
108 <CodeTabs group="relation-query">
109 <CodeTab label="query.graph" value="method">
110
111 ```ts
112 const { data: customers } = await query.graph({
113 entity: "customer",
114 fields: [
115 "account_holder_link.account_holder.*",
116 ],
117 })
118
119 // customers[0].account_holder_link?.[0]?.account_holder
120 ```
121
122 </CodeTab>
123 <CodeTab label="useQueryGraphStep" value="step">
124
125 ```ts
126 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
127
128 // ...
129
130 const { data: customers } = useQueryGraphStep({
131 entity: "customer",
132 fields: [
133 "account_holder_link.account_holder.*",
134 ],
135 })
136
137 // customers[0].account_holder_link?.[0]?.account_holder
138 ```
139
140 </CodeTab>
141 </CodeTabs>
142
143 ### Manage with Link
144
145 To manage the account holders of a customer, use [Link](!docs!/learn/fundamentals/module-links/link):
146
147 <CodeTabs group="relation-link">
148 <CodeTab label="link.create" value="method">
149
150 ```ts
151 import { Modules } from "@medusajs/framework/utils"
152
153 // ...
154
155 await link.create({
156 [Modules.CUSTOMER]: {
157 customer_id: "cus_123",
158 },
159 [Modules.PAYMENT]: {
160 account_holder_id: "acchld_123",
161 },
162 })
163 ```
164
165 </CodeTab>
166 <CodeTab label="createRemoteLinkStep" value="step">
167
168 ```ts
169 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
170
171 // ...
172
173 createRemoteLinkStep({
174 [Modules.CUSTOMER]: {
175 customer_id: "cus_123",
176 },
177 [Modules.PAYMENT]: {
178 account_holder_id: "acchld_123",
179 },
180 })
181 ```
182
183 </CodeTab>
184 </CodeTabs>
185
186 <Note>
187
188 A customer can have many account holders, but each account holder belongs to only one customer. If you try to link an account holder that already belongs to another customer, Medusa throws a `Cannot create multiple links between 'customer' and 'payment'` error. To transfer an account holder to a different customer, remove the existing link first, then create the new one.
189
190 </Note>
191
192 ---
193
194 ## Cart Module
195
196 Medusa defines a read-only link between the [Cart Module](../../cart/page.mdx)'s `Cart` data model and the `Customer` data model. Because the link is read-only from the `Cart`'s side, you can only retrieve the customer of a cart, and not the other way around.
197
198 ### Retrieve with Query
199
200 To retrieve the customer of a cart with [Query](!docs!/learn/fundamentals/query), pass `customer.*` in `fields`:
201
202 <CodeTabs group="relation-query">
203 <CodeTab label="query.graph" value="method">
204
205 ```ts
206 const { data: carts } = await query.graph({
207 entity: "cart",
208 fields: [
209 "customer.*",
210 ],
211 })
212
213 // carts.customer
214 ```
215
216 </CodeTab>
217 <CodeTab label="useQueryGraphStep" value="step">
218
219 ```ts
220 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
221
222 // ...
223
224 const { data: carts } = useQueryGraphStep({
225 entity: "cart",
226 fields: [
227 "customer.*",
228 ],
229 })
230
231 // carts.customer
232 ```
233
234 </CodeTab>
235 </CodeTabs>
236
237 ---
238
239 ## Order Module
240
241 Medusa defines a read-only link between the [Order Module](../../order/page.mdx)'s `Order` data model and the `Customer` data model. Because the link is read-only from the `Order`'s side, you can only retrieve the customer of an order, and not the other way around.
242
243 ### Retrieve with Query
244
245 To retrieve the customer of an order with [Query](!docs!/learn/fundamentals/query), pass `customer.*` in `fields`:
246
247 <CodeTabs group="relation-query">
248 <CodeTab label="query.graph" value="method">
249
250 ```ts
251 const { data: orders } = await query.graph({
252 entity: "order",
253 fields: [
254 "customer.*",
255 ],
256 })
257
258 // orders.customer
259 ```
260
261 </CodeTab>
262 <CodeTab label="useQueryGraphStep" value="step">
263
264 ```ts
265 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
266
267 // ...
268
269 const { data: orders } = useQueryGraphStep({
270 entity: "order",
271 fields: [
272 "customer.*",
273 ],
274 })
275
276 // orders.customer
277 ```
278
279 </CodeTab>
280 </CodeTabs>