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