1 ---
2 sidebar_label: "Extend Cart"
3 tags:
4 - cart
5 - tutorial
6 - extend module
7 - server
8 ---
9
10 import { Prerequisites } from "docs-ui"
11
12 export const metadata = {
13 title: `Extend Cart Data Model`,
14 }
15
16 # {metadata.title}
17
18 In this documentation, you'll learn how to extend a data model of the Cart Module to add a custom property.
19
20 You'll create a `Custom` data model in a module. This data model will have a `custom_name` property, which is the property you want to add to the [Cart data model](/references/cart/models/Cart) defined in the Cart Module.
21
22 You'll then learn how to:
23
24 - Link the `Custom` data model to the `Cart` data model.
25 - Set the `custom_name` property when a cart is created or updated using Medusa's API routes.
26 - Retrieve the `custom_name` property with the cart's details, in custom or existing API routes.
27
28 ## Step 1: Define Custom Data Model
29
30 Consider you have a Hello Module defined in the `/src/modules/hello` directory.
31
32 <Note title="Tip">
33
34 If you don't have a module, follow [this guide](!docs!/learn/fundamentals/modules) to create one.
35
36 </Note>
37
38 To add the `custom_name` property to the `Cart` data model, you'll create in the Hello Module a data model that has the `custom_name` property.
39
40 Create the file `src/modules/hello/models/custom.ts` with the following content:
41
42 ```ts title="src/modules/hello/models/custom.ts"
43 import { model } from "@medusajs/framework/utils"
44
45 export const Custom = model.define("custom", {
46 id: model.id().primaryKey(),
47 custom_name: model.text(),
48 })
49 ```
50
51 This creates a `Custom` data model that has the `id` and `custom_name` properties.
52
53 <Note title="Tip">
54
55 Learn more about data models in [this guide](!docs!/learn/fundamentals/modules#1-create-data-model).
56
57 </Note>
58
59 ---
60
61 ## Step 2: Define Link to Cart Data Model
62
63 Next, you'll define a module link between the `Custom` and `Cart` data model. A module link allows you to form a relation between two data models of separate modules while maintaining module isolation.
64
65 <Note title="Tip">
66
67 Learn more about module links in [this guide](!docs!/learn/fundamentals/module-links).
68
69 </Note>
70
71 Create the file `src/links/cart-custom.ts` with the following content:
72
73 ```ts title="src/links/cart-custom.ts"
74 import { defineLink } from "@medusajs/framework/utils"
75 import HelloModule from "../modules/hello"
76 import CartModule from "@medusajs/medusa/cart"
77
78 export default defineLink(
79 CartModule.linkable.cart,
80 HelloModule.linkable.custom
81 )
82 ```
83
84 This defines a link between the `Cart` and `Custom` data models. Using this link, you'll later query data across the modules, and link records of each data model.
85
86 ---
87
88 ## Step 3: Generate and Run Migrations
89
90 <Prerequisites
91 items={[
92 {
93 text: "Module must be registered in medusa-config.ts",
94 link: "!docs!/learn/fundamentals/modules#4-add-module-to-configurations"
95 }
96 ]}
97 />
98
99 To reflect the `Custom` data model in the database, generate a migration that defines the table to be created for it.
100
101 Run the following command in your Medusa project's root:
102
103 ```bash npx2yarn
104 npx medusa db:generate helloModuleService
105 ```
106
107 Where `helloModuleService` is your module's name.
108
109 Then, run the `db:migrate` command to run the migrations and create a table in the database for the link between the `Cart` and `Custom` data models:
110
111 ```bash npx2yarn
112 npx medusa db:migrate
113 ```
114
115 A table for the link is now created in the database. You can now retrieve and manage the link between records of the data models.
116
117 ---
118
119 ## Step 4: Consume cartCreated Workflow Hook
120
121 When a cart is created, you also want to create a `Custom` record and set the `custom_name` property, then create a link between the `Cart` and `Custom` records.
122
123 To do that, you'll consume the [cartCreated](/references/medusa-workflows/createCartWorkflow#cartcreated) hook of the [createCartWorkflow](/references/medusa-workflows/createCartWorkflow). This workflow is executed in the [Create Cart API Route](!api!/store/carts/create-cart).
124
125 <Note title="Tip">
126
127 Learn more about workflow hooks in [this guide](!docs!/learn/fundamentals/workflows/workflow-hooks).
128
129 </Note>
130
131 The API route accepts in its request body an `additional_data` parameter. You can pass in it custom data, which is passed to the workflow hook handler.
132
133 ### Add custom_name to Additional Data Validation
134
135 To pass the `custom_name` in the `additional_data` parameter, you must add a validation rule that tells the Medusa application about this custom property.
136
137 Create the file `src/api/middlewares.ts` with the following content:
138
139 <Note>
140
141 As of [Medusa v2.13.0](https://github.com/medusajs/medusa/releases/tag/v2.13.0), Zod should be imported from `@medusajs/framework/zod`.
142
143 </Note>
144
145 ```ts title="src/api/middlewares.ts"
146 import { defineMiddlewares } from "@medusajs/framework/http"
147 import { z } from "@medusajs/framework/zod"
148
149 export default defineMiddlewares({
150 routes: [
151 {
152 method: "POST",
153 matcher: "/store/carts",
154 additionalDataValidator: {
155 custom_name: z.string().optional(),
156 },
157 },
158 ],
159 })
160 ```
161
162 The `additional_data` parameter validation is customized using `defineMiddlewares`. In the routes middleware configuration object, the `additionalDataValidator` property accepts [Zod](https://zod.dev/) validation rules.
163
164 In the snippet above, you add a validation rule indicating that `custom_name` is a string that can be passed in the `additional_data` object.
165
166 <Note title="Tip">
167
168 Learn more about additional data validation in [this guide](!docs!/learn/fundamentals/api-routes/additional-data).
169
170 </Note>
171
172 ### Create Workflow to Create Custom Record
173
174 You'll now create a workflow that will be used in the hook handler.
175
176 This workflow will create a `Custom` record, then link it to the cart.
177
178 Start by creating the step that creates the `Custom` record. Create the file `src/workflows/create-custom-from-cart/steps/create-custom.ts` with the following content:
179
180 ```ts title="src/workflows/create-custom-from-cart/steps/create-custom.ts"
181 import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
182 import HelloModuleService from "../../../modules/hello/service"
183 import { HELLO_MODULE } from "../../../modules/hello"
184
185 type CreateCustomStepInput = {
186 custom_name?: string
187 }
188
189 export const createCustomStep = createStep(
190 "create-custom",
191 async (data: CreateCustomStepInput, { container }) => {
192 if (!data.custom_name) {
193 return
194 }
195
196 const helloModuleService: HelloModuleService = container.resolve(
197 HELLO_MODULE
198 )
199
200 const custom = await helloModuleService.createCustoms(data)
201
202 return new StepResponse(custom, custom)
203 },
204 async (custom, { container }) => {
205 const helloModuleService: HelloModuleService = container.resolve(
206 HELLO_MODULE
207 )
208
209 await helloModuleService.deleteCustoms(custom.id)
210 }
211 )
212 ```
213
214 In the step, you resolve the Hello Module's main service and create a `Custom` record.
215
216 In the compensation function that undoes the step's actions in case of an error, you delete the created record.
217
218 <Note title="Tip">
219
220 Learn more about compensation functions in [this guide](!docs!/learn/fundamentals/workflows/compensation-function).
221
222 </Note>
223
224 Then, create the workflow at `src/workflows/create-custom-from-cart/index.ts` with the following content:
225
226 ```ts title="src/workflows/create-custom-from-cart/index.ts" collapsibleLines="1-7" expandButtonLabel="Show Imports"
227 import { createWorkflow, transform, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
228 import { CartDTO } from "@medusajs/framework/types"
229 import { createCustomStep } from "./steps/create-custom"
230 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
231 import { Modules } from "@medusajs/framework/utils"
232 import { HELLO_MODULE } from "../../modules/hello"
233
234 export type CreateCustomFromCartWorkflowInput = {
235 cart: CartDTO
236 additional_data?: {
237 custom_name?: string
238 }
239 }
240
241 export const createCustomFromCartWorkflow = createWorkflow(
242 "create-custom-from-cart",
243 (input: CreateCustomFromCartWorkflowInput) => {
244 const customName = transform(
245 {
246 input,
247 },
248 (data) => data.input.additional_data?.custom_name || ""
249 )
250
251 const custom = createCustomStep({
252 custom_name: customName,
253 })
254
255 when(({ custom }), ({ custom }) => custom !== undefined)
256 .then(() => {
257 createRemoteLinkStep([{
258 [Modules.CART]: {
259 cart_id: input.cart.id,
260 },
261 [HELLO_MODULE]: {
262 custom_id: custom.id,
263 },
264 }])
265 })
266
267 return new WorkflowResponse({
268 custom,
269 })
270 }
271 )
272 ```
273
274 The workflow accepts as an input the created cart and the `additional_data` parameter passed in the request. This is the same input that the `cartCreated` hook accepts.
275
276 In the workflow, you:
277
278 1. Use `transform` to get the value of `custom_name` based on whether it's set in `additional_data`. Learn more about why you can't use conditional operators in a workflow without using `transform` in [this guide](!docs!/learn/fundamentals/workflows/conditions#why-if-conditions-arent-allowed-in-workflows).
279 2. Create the `Custom` record using the `createCustomStep`.
280 3. Use `when-then` to link the cart to the `Custom` record if it was created. Learn more about why you can't use if-then conditions in a workflow without using `when-then` in [this guide](!docs!/learn/fundamentals/workflows/conditions#why-if-conditions-arent-allowed-in-workflows).
281
282 You'll next call the workflow in the hook handler.
283
284 ### Consume Workflow Hook
285
286 You can now consume the `cartCreated` hook, which is executed in the `createCartWorkflow` after the cart is created.
287
288 To consume the hook, create the file `src/workflow/hooks/cart-created.ts` with the following content:
289
290 ```ts title="src/workflow/hooks/cart-created.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports"
291 import { createCartWorkflow } from "@medusajs/medusa/core-flows"
292 import {
293 createCustomFromCartWorkflow,
294 CreateCustomFromCartWorkflowInput,
295 } from "../create-custom-from-cart"
296
297 createCartWorkflow.hooks.cartCreated(
298 async (hookData, { container }) => {
299 await createCustomFromCartWorkflow(container)
300 .run({
301 input: hookData as CreateCustomFromCartWorkflowInput,
302 })
303 }
304 )
305 ```
306
307 The hook handler executes the `createCustomFromCartWorkflow`, passing it its input.
308
309 ### Test it Out
310
311 To test it out, send a `POST` request to `/store/carts` to create a cart, passing `custom_name` in `additional_data`:
312
313 ```bash
314 curl -X POST 'localhost:9000/store/carts' \
315 -H 'x-publishable-api-key: {publishable_api_key}' \
316 -H 'Content-Type: application/json' \
317 --data '{
318 "region_id": "reg_01J9NNNWVV0T71PT44EAMTJCMP",
319 "additional_data": {
320 "custom_name": "test"
321 }
322 }'
323 ```
324
325 Make sure to replace `{publishable_api_key}` with your publishable API key, which you can retrieve from the Medusa Admin. Also, replace the value of `region_id` with an ID of a region in your application.
326
327 The request will return the cart's details. You'll learn how to retrieve the `custom_name` property with the cart's details in the next section.
328
329 ---
330
331 ## Step 5: Retrieve custom_name with Cart Details
332
333 When you extend an existing data model through links, you also want to retrieve the custom properties with the data model.
334
335 ### Allow Retrieving Custom Fields in API Routes
336
337 The Store API routes only return the fields that they allow in the `fields` query parameter. Since the `custom` link isn't part of the Store Cart routes' allowed fields, Medusa removes it from the response without an error.
338
339 So, before you can retrieve the linked `Custom` record through the Store Cart API routes, you must add `custom` to the allowed fields of those routes.
340
341 To do that, add a new route middleware configuration object in `src/api/middlewares.ts`:
342
343 ```ts title="src/api/middlewares.ts"
344 import { allowFields } from "@medusajs/framework/http"
345
346 export default defineMiddlewares({
347 routes: [
348 // ...
349 {
350 matcher: "/store/carts",
351 middlewares: [allowFields("custom")],
352 },
353 ],
354 })
355 ```
356
357 <Note>
358
359 `allowFields` is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). In earlier versions, write the middleware yourself, as explained in the [Allowed Fields](!docs!/learn/fundamentals/api-routes/allowed-fields#how-to-override-allowed-fields-of-medusas-api-routes) documentation.
360
361 </Note>
362
363 This middleware pushes `custom` to the allowed fields of the Store Cart API routes, so Medusa keeps the field when a request asks for it.
364
365 <Note title="Important">
366
367 The middleware must be global, so don't set a `method` property in the route object, even though some of the Store Cart API routes accept `POST` requests. Medusa runs a middleware scoped to a method after it validates the `fields` query parameter, so it has no effect.
368
369 </Note>
370
371 Learn more in the [Allowed Fields](!docs!/learn/fundamentals/api-routes/allowed-fields) documentation.
372
373 ### Retrieve in API Routes
374
375 After you allow the `custom` field, you can retrieve the `custom_name` property when you're retrieving the cart through API routes, such as the [Get Cart API Route](!api!/store/carts/get-a-cart). To do that, pass in the `fields` query parameter `+custom.*`, which retrieves the linked `Custom` record's details.
376
377 <Note title="Tip">
378
379 The `+` prefix in `+custom.*` indicates that the relation should be retrieved with the default cart fields. Learn more about selecting fields and relations in the [API reference](!api!/store/select-fields-and-relations).
380
381 </Note>
382
383 For example:
384
385 ```bash
386 curl -X POST 'localhost:9000/store/carts/{cart_id}?fields=+custom.*' \
387 -H 'x-publishable-api-key: {publishable_api_key}'
388 ```
389
390 Make sure to replace `{cart_id}` with the cart's ID, and `{publishable_api_key}` with your publishable API key, which you can retrieve from the Medusa Admin.
391
392 Among the returned `cart` object, you'll find a `custom` property which holds the details of the linked `Custom` record:
393
394 ```json
395 {
396 "cart": {
397 // ...
398 "custom": {
399 "id": "01J9NP7ANXDZ0EAYF0956ZE1ZA",
400 "custom_name": "test",
401 "created_at": "2024-10-08T09:09:06.877Z",
402 "updated_at": "2024-10-08T09:09:06.877Z",
403 "deleted_at": null
404 }
405 }
406 }
407 ```
408
409 ### Retrieve using Query
410
411 You can also retrieve the `Custom` record linked to a cart in your code using [Query](!docs!/learn/fundamentals/query).
412
413 For example:
414
415 ```ts
416 const { data: [cart] } = await query.graph({
417 entity: "cart",
418 fields: ["*", "custom.*"],
419 filters: {
420 id: cart_id,
421 },
422 })
423 ```
424
425 Learn more about how to use Query in [this guide](!docs!/learn/fundamentals/query).
426
427 ---
428
429 ## Step 6: Consume cartUpdated Workflow Hook
430
431 Similar to the `cartCreated` hook, you'll consume the [cartUpdated](/references/medusa-workflows/updateCartWorkflow#cartupdated) hook of the [updateCartWorkflow](/references/medusa-workflows/updateCartWorkflow) to update `custom_name` when the cart is updated.
432
433 The `updateCartWorkflow` is executed by the [Update Cart API route](!api!/store/carts/update-a-cart), which accepts the `additional_data` parameter to pass custom data to the hook.
434
435 ### Add custom_name to Additional Data Validation
436
437 To allow passing `custom_name` in the `additional_data` parameter of the update cart route, add in `src/api/middlewares.ts` a new route middleware configuration object:
438
439 <Note>
440
441 As of [Medusa v2.13.0](https://github.com/medusajs/medusa/releases/tag/v2.13.0), Zod should be imported from `@medusajs/framework/zod`.
442
443 </Note>
444
445 ```ts title="src/api/middlewares.ts"
446 import { defineMiddlewares } from "@medusajs/framework/http"
447 import { z } from "@medusajs/framework/zod"
448
449 export default defineMiddlewares({
450 routes: [
451 // ...
452 {
453 method: "POST",
454 matcher: "/store/carts/:id",
455 additionalDataValidator: {
456 custom_name: z.string().nullish(),
457 },
458 },
459 ],
460 })
461 ```
462
463 The validation schema is similar to that of the Create Cart API route, except you can pass a `null` value for `custom_name` to remove or unset the `custom_name`'s value.
464
465 ### Create Workflow to Update Custom Record
466
467 Next, you'll create a workflow that creates, updates, or deletes `Custom` records based on the provided `additional_data` parameter:
468
469 1. If `additional_data.custom_name` is set and it's `null`, the `Custom` record linked to the cart is deleted.
470 2. If `additional_data.custom_name` is set and the cart doesn't have a linked `Custom` record, a new record is created and linked to the cart.
471 3. If `additional_data.custom_name` is set and the cart has a linked `Custom` record, the `custom_name` property of the `Custom` record is updated.
472
473 Start by creating the step that updates a `Custom` record. Create the file `src/workflows/update-custom-from-cart/steps/update-custom.ts` with the following content:
474
475 ```ts title="src/workflows/update-custom-from-cart/steps/update-custom.ts"
476 import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
477 import { HELLO_MODULE } from "../../../modules/hello"
478 import HelloModuleService from "../../../modules/hello/service"
479
480 type UpdateCustomStepInput = {
481 id: string
482 custom_name: string
483 }
484
485 export const updateCustomStep = createStep(
486 "update-custom",
487 async ({ id, custom_name }: UpdateCustomStepInput, { container }) => {
488 const helloModuleService: HelloModuleService = container.resolve(
489 HELLO_MODULE
490 )
491
492 const prevData = await helloModuleService.retrieveCustom(id)
493
494 const custom = await helloModuleService.updateCustoms({
495 id,
496 custom_name,
497 })
498
499 return new StepResponse(custom, prevData)
500 },
501 async (prevData, { container }) => {
502 const helloModuleService: HelloModuleService = container.resolve(
503 HELLO_MODULE
504 )
505
506 await helloModuleService.updateCustoms(prevData)
507 }
508 )
509 ```
510
511 In this step, you update a `Custom` record. In the compensation function, you revert the update.
512
513 Next, you'll create the step that deletes a `Custom` record. Create the file `src/workflows/update-custom-from-cart/steps/delete-custom.ts` with the following content:
514
515 ```ts title="src/workflows/update-custom-from-cart/steps/delete-custom.ts" collapsibleLines="1-6" expandButtonLabel="Show Imports"
516 import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
517 import { Custom } from "../../../modules/hello/models/custom"
518 import { InferTypeOf } from "@medusajs/framework/types"
519 import HelloModuleService from "../../../modules/hello/service"
520 import { HELLO_MODULE } from "../../../modules/hello"
521
522 type DeleteCustomStepInput = {
523 custom: InferTypeOf<typeof Custom>
524 }
525
526 export const deleteCustomStep = createStep(
527 "delete-custom",
528 async ({ custom }: DeleteCustomStepInput, { container }) => {
529 const helloModuleService: HelloModuleService = container.resolve(
530 HELLO_MODULE
531 )
532
533 await helloModuleService.deleteCustoms(custom.id)
534
535 return new StepResponse(custom, custom)
536 },
537 async (custom, { container }) => {
538 const helloModuleService: HelloModuleService = container.resolve(
539 HELLO_MODULE
540 )
541
542 await helloModuleService.createCustoms(custom)
543 }
544 )
545 ```
546
547 In this step, you delete a `Custom` record. In the compensation function, you create it again.
548
549 Finally, you'll create the workflow. Create the file `src/workflows/update-custom-from-cart/index.ts` with the following content:
550
551 ```ts title="src/workflows/update-custom-from-cart/index.ts" collapsibleLines="1-9" expandButtonLabel="Show Imports"
552 import { CartDTO } from "@medusajs/framework/types"
553 import { createWorkflow, when, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
554 import { createRemoteLinkStep, dismissRemoteLinkStep, useQueryGraphStep } from "@medusajs/medusa/core-flows"
555 import { createCustomStep } from "../create-custom-from-cart/steps/create-custom"
556 import { Modules } from "@medusajs/framework/utils"
557 import { HELLO_MODULE } from "../../modules/hello"
558 import { deleteCustomStep } from "./steps/delete-custom"
559 import { updateCustomStep } from "./steps/update-custom"
560
561 export type UpdateCustomFromCartStepInput = {
562 cart: CartDTO
563 additional_data?: {
564 custom_name?: string | null
565 }
566 }
567
568 export const updateCustomFromCartWorkflow = createWorkflow(
569 "update-custom-from-cart",
570 (input: UpdateCustomFromCartStepInput) => {
571 const { data: carts } = useQueryGraphStep({
572 entity: "cart",
573 fields: ["custom.*"],
574 filters: {
575 id: input.cart.id,
576 },
577 })
578
579 // TODO create, update, or delete Custom record
580 }
581 )
582 ```
583
584 The workflow accepts the same input as the `cartUpdated` workflow hook handler would.
585
586 In the workflow, you retrieve the cart's linked `Custom` record using Query.
587
588 Next, replace the `TODO` with the following:
589
590 ```ts title="src/workflows/update-custom-from-cart/index.ts"
591 const created = when(
592 "create-cart-custom-link",
593 {
594 input,
595 carts,
596 },
597 (data) =>
598 !data.carts[0].custom &&
599 data.input.additional_data?.custom_name?.length > 0
600 )
601 .then(() => {
602 const custom = createCustomStep({
603 custom_name: input.additional_data.custom_name,
604 })
605
606 createRemoteLinkStep([{
607 [Modules.CART]: {
608 cart_id: input.cart.id,
609 },
610 [HELLO_MODULE]: {
611 custom_id: custom.id,
612 },
613 }])
614
615 return custom
616 })
617
618 // TODO update, or delete Custom record
619 ```
620
621 Using `when-then`, you check if the cart doesn't have a linked `Custom` record and the `custom_name` property is set. If so, you create a `Custom` record and link it to the cart.
622
623 To create the `Custom` record, you use the `createCustomStep` you created in an earlier section.
624
625 Next, replace the new `TODO` with the following:
626
627 ```ts title="src/workflows/update-custom-from-cart/index.ts"
628 const deleted = when(
629 "delete-cart-custom-link",
630 {
631 input,
632 carts,
633 }, (data) =>
634 data.carts[0].custom && (
635 data.input.additional_data?.custom_name === null ||
636 data.input.additional_data?.custom_name.length === 0
637 )
638 )
639 .then(() => {
640 deleteCustomStep({
641 custom: carts[0].custom,
642 })
643
644 dismissRemoteLinkStep({
645 [HELLO_MODULE]: {
646 custom_id: carts[0].custom.id,
647 },
648 })
649
650 return carts[0].custom.id
651 })
652
653 // TODO delete Custom record
654 ```
655
656 Using `when-then`, you check if the cart has a linked `Custom` record and `custom_name` is `null` or an empty string. If so, you delete the linked `Custom` record and dismiss its links.
657
658 Finally, replace the new `TODO` with the following:
659
660 ```ts title="src/workflows/update-custom-from-cart/index.ts"
661 const updated = when({
662 input,
663 carts,
664 }, (data) => data.carts[0].custom && data.input.additional_data?.custom_name?.length > 0)
665 .then(() => {
666 return updateCustomStep({
667 id: carts[0].custom.id,
668 custom_name: input.additional_data.custom_name,
669 })
670 })
671
672 return new WorkflowResponse({
673 created,
674 updated,
675 deleted,
676 })
677 ```
678
679 Using `when-then`, you check if the cart has a linked `Custom` record and `custom_name` is passed in the `additional_data`. If so, you update the linked `Custom` record.
680
681 You return in the workflow response the created, updated, and deleted `Custom` record.
682
683 ### Consume cartUpdated Workflow Hook
684
685 You can now consume the `cartUpdated` and execute the workflow you created.
686
687 Create the file `src/workflows/hooks/cart-updated.ts` with the following content:
688
689 ```ts title="src/workflows/hooks/cart-updated.ts"
690 import { updateCartWorkflow } from "@medusajs/medusa/core-flows"
691 import {
692 UpdateCustomFromCartStepInput,
693 updateCustomFromCartWorkflow,
694 } from "../update-custom-from-cart"
695
696 updateCartWorkflow.hooks.cartUpdated(
697 async (hookData, { container }) => {
698 await updateCustomFromCartWorkflow(container)
699 .run({
700 input: hookData as UpdateCustomFromCartStepInput,
701 })
702 }
703 )
704 ```
705
706 In the workflow hook handler, you execute the workflow, passing it the hook's input.
707
708 ### Test it Out
709
710 To test it out, send a `POST` request to `/store/carts/:id` to update a cart, passing `custom_name` in `additional_data`. The request also passes `+custom.*` in the `fields` query parameter, which the [middleware you added earlier](#allow-retrieving-custom-fields-in-api-routes) allows:
711
712 ```bash
713 curl -X POST 'localhost:9000/store/carts/{cart_id}?fields=+custom.*' \
714 -H 'x-publishable-api-key: {publishable_api_key}' \
715 -H 'Content-Type: application/json' \
716 --data '{
717 "additional_data": {
718 "custom_name": "test 2"
719 }
720 }'
721 ```
722
723 Make sure to replace `{cart_id}` with the cart's ID, and `{publishable_api_key}` with your publishable API key, which you can retrieve from the Medusa Admin.
724
725 The request will return the cart's details with the updated `custom` linked record.