|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Product Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Product Module and how to use it in your application. |
|
14
|
|
|
15
|
<Note title="Looking for no-code docs?"> |
|
16
|
|
|
17
|
Refer to the [Medusa Admin User Guide](!user-guide!/products) to learn how to manage products using the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has product related features available out-of-the-box through the Product Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Product Module. |
|
22
|
|
|
23
|
<Note> |
|
24
|
|
|
25
|
Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation). |
|
26
|
|
|
27
|
</Note> |
|
28
|
|
|
29
|
## Product Features |
|
30
|
|
|
31
|
- [Products Management](/references/product/models/Product): Store and manage products. Products have custom options, such as color or size, and each variant in the product sets the value for these options. |
|
32
|
- [Product Organization](/references/product/models): The Product Module provides different data models used to organize products, including categories, collections, tags, and more. |
|
33
|
- [Bundled and Multi-Part Products](../inventory/inventory-kit/page.mdx): Create and manage inventory kits for a single product, allowing you to implement use cases like bundled or multi-part products. |
|
34
|
- [Tiered Pricing and Price Rules](../pricing/price-rules/page.mdx): Set prices for product variants with tiers and rules, allowing you to create complex pricing strategies. |
|
35
|
|
|
36
|
--- |
|
37
|
|
|
38
|
## How to Use the Product Module |
|
39
|
|
|
40
|
In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism. |
|
41
|
|
|
42
|
You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package. |
|
43
|
|
|
44
|
For example: |
|
45
|
|
|
46
|
export const highlights = [ |
|
47
|
["12", "Modules.PRODUCT", "Resolve the module in a step."] |
|
48
|
] |
|
49
|
|
|
50
|
```ts title="src/workflows/create-product.ts" highlights={highlights} |
|
51
|
import { |
|
52
|
createWorkflow, |
|
53
|
WorkflowResponse, |
|
54
|
createStep, |
|
55
|
StepResponse, |
|
56
|
} from "@medusajs/framework/workflows-sdk" |
|
57
|
import { Modules } from "@medusajs/framework/utils" |
|
58
|
|
|
59
|
const createProductStep = createStep( |
|
60
|
"create-product", |
|
61
|
async ({}, { container }) => { |
|
62
|
const productService = container.resolve(Modules.PRODUCT) |
|
63
|
|
|
64
|
const product = await productService.createProducts({ |
|
65
|
title: "Medusa Shirt", |
|
66
|
options: [ |
|
67
|
{ |
|
68
|
title: "Color", |
|
69
|
values: ["Black", "White"], |
|
70
|
}, |
|
71
|
], |
|
72
|
variants: [ |
|
73
|
{ |
|
74
|
title: "Black Shirt", |
|
75
|
options: { |
|
76
|
Color: "Black", |
|
77
|
}, |
|
78
|
}, |
|
79
|
], |
|
80
|
}) |
|
81
|
|
|
82
|
return new StepResponse({ product }, product.id) |
|
83
|
}, |
|
84
|
async (productId, { container }) => { |
|
85
|
if (!productId) { |
|
86
|
return |
|
87
|
} |
|
88
|
const productService = container.resolve(Modules.PRODUCT) |
|
89
|
|
|
90
|
await productService.deleteProducts([productId]) |
|
91
|
} |
|
92
|
) |
|
93
|
|
|
94
|
export const createProductWorkflow = createWorkflow( |
|
95
|
"create-product", |
|
96
|
() => { |
|
97
|
const { product } = createProductStep() |
|
98
|
|
|
99
|
return new WorkflowResponse({ |
|
100
|
product, |
|
101
|
}) |
|
102
|
} |
|
103
|
) |
|
104
|
``` |
|
105
|
|
|
106
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
107
|
|
|
108
|
<CodeTabs group="resource-types"> |
|
109
|
<CodeTab label="API Route" value="api-route"> |
|
110
|
|
|
111
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
112
|
import type { |
|
113
|
MedusaRequest, |
|
114
|
MedusaResponse, |
|
115
|
} from "@medusajs/framework/http" |
|
116
|
import { createProductWorkflow } from "../../workflows/create-product" |
|
117
|
|
|
118
|
export async function GET( |
|
119
|
req: MedusaRequest, |
|
120
|
res: MedusaResponse |
|
121
|
) { |
|
122
|
const { result } = await createProductWorkflow(req.scope) |
|
123
|
.run() |
|
124
|
|
|
125
|
res.send(result) |
|
126
|
} |
|
127
|
``` |
|
128
|
|
|
129
|
</CodeTab> |
|
130
|
<CodeTab label="Subscriber" value="subscriber"> |
|
131
|
|
|
132
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
133
|
import { |
|
134
|
type SubscriberConfig, |
|
135
|
type SubscriberArgs, |
|
136
|
} from "@medusajs/framework" |
|
137
|
import { createProductWorkflow } from "../workflows/create-product" |
|
138
|
|
|
139
|
export default async function handleUserCreated({ |
|
140
|
event: { data }, |
|
141
|
container, |
|
142
|
}: SubscriberArgs<{ id: string }>) { |
|
143
|
const { result } = await createProductWorkflow(container) |
|
144
|
.run() |
|
145
|
|
|
146
|
console.log(result) |
|
147
|
} |
|
148
|
|
|
149
|
export const config: SubscriberConfig = { |
|
150
|
event: "user.created", |
|
151
|
} |
|
152
|
``` |
|
153
|
|
|
154
|
</CodeTab> |
|
155
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
156
|
|
|
157
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
158
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
159
|
import { createProductWorkflow } from "../workflows/create-product" |
|
160
|
|
|
161
|
export default async function myCustomJob( |
|
162
|
container: MedusaContainer |
|
163
|
) { |
|
164
|
const { result } = await createProductWorkflow(container) |
|
165
|
.run() |
|
166
|
|
|
167
|
console.log(result) |
|
168
|
} |
|
169
|
|
|
170
|
export const config = { |
|
171
|
name: "run-once-a-day", |
|
172
|
schedule: `0 0 * * *`, |
|
173
|
} |
|
174
|
``` |
|
175
|
|
|
176
|
</CodeTab> |
|
177
|
</CodeTabs> |
|
178
|
|
|
179
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
180
|
|
|
181
|
--- |
|
182
|
|
|
183
|
<CommerceModuleSections /> |