Raw
1 ---
2 products:
3 - product
4 - cart
5 - order
6 ---
7
8 import { AcademicCapSolid, PuzzleSolid } from "@medusajs/icons";
9
10 export const metadata = {
11 title: `Ticket Booking System Recipe`,
12 };
13
14 # {metadata.title}
15
16 This recipe provides the general steps to implement a ticket booking system in your Medusa application.
17
18 <Note>
19
20 Follow the step-by-step [Ticket Booking System Example](./example/page.mdx) to learn how to implement a ticket booking system in your Medusa application.
21
22 </Note>
23
24 ## Overview
25
26 A ticket booking system allows customers to book tickets for events, such as shows or sports games. By using a ticket booking system, you can manage shows, venues, and seat availability, among other features.
27
28 The Medusa [Framework](!docs!/learn/fundamentals/framework) facilitates building a ticket booking system on top of the existing commerce functionalities, such as products, pricing, inventory, carts, and orders. All of these are provided by Medusa's [Commerce Modules](../../commerce-modules/page.mdx).
29
30 To support ticket-booking features, you can customize the Medusa application by creating a Ticket Booking Module, linking its data models to Medusa's existing models, and building flows around the module.
31
32 ---
33
34 ## Create Ticket Booking Module
35
36 Your custom features and functionalities are implemented inside modules. The module is integrated into the Medusa application without any implications on existing functionalities.
37
38 The module will hold your custom data models and the service implementing ticket-booking-related features.
39
40 <Card
41 href="!docs!/learn/fundamentals/modules"
42 title="How to Create a Module"
43 text="Learn how to create a module."
44 icon={AcademicCapSolid}
45 />
46
47 ### Create Custom Data Models
48
49 A data model represents a table in the database. You can define in your module data models to store data related to your custom features, such as shows, venues, and seats.
50
51 For example, you can define:
52
53 - A `Venue` model for the venue where the event takes place.
54 - A `TicketProduct` model for a show, and a `TicketProductVariant` model for the ticket variants (for example, different seating sections).
55 - A `TicketPurchase` model for a ticket purchase.
56
57 Then, you can link your custom data model to data models from other modules. For example, you can link:
58
59 - The `TicketProduct` model to the Product Module's `Product` data model, benefiting from existing features related to products and collections.
60 - The `TicketProductVariant` model to the Product Module's `ProductVariant` data model, benefiting from existing features related to inventory and pricing.
61 - The `TicketPurchase` model to the Order Module's `Order` data model, benefiting from existing features related to orders and payments.
62
63 <CardList itemsPerRow={2} items={[
64 {
65 href: "!docs!/learn/fundamentals/modules#1-create-data-model",
66 title: "How to Create a Data Model",
67 text: "Learn how to create a data model.",
68 icon: AcademicCapSolid,
69 },
70 {
71 href: "!docs!/learn/fundamentals/module-links",
72 title: "Define Module Links",
73 text: "Define links between data models.",
74 icon: AcademicCapSolid,
75 },
76 ]} />
77
78 ### Implement Data Management Features
79
80 Your module’s main service holds data-management and other related features. Then, in other resources, such as an API route, you can resolve the service from the Medusa container and use its functionalities.
81
82 Medusa facilitates implementing data-management features using the service factory. Your module's main service can extend this service factory, and it generates data-management methods for your data models.
83
84 <Card
85 href="!docs!/learn/fundamentals/modules/service-factory"
86 title="Service Factory"
87 text="Learn about the service factory and how to use it."
88 icon={AcademicCapSolid}
89 />
90
91 ---
92
93 ## Implement Ticket Booking Business Flows
94
95 You can implement ticket-booking-related business logic in workflows. A workflow is a series of queries and actions, called steps, that complete a task.
96
97 By using workflows, you benefit from features like rollback mechanisms, error handling, and retrying failed steps. You can then execute workflows from other resources, such as an API route.
98
99 You can implement workflows that create venues and ticket products, complete carts with ticket products, and more. In the workflow's steps, you can resolve the Ticket Booking Module's service and use its data-management methods to manage ticket-booking-related data.
100
101 You can then utilize the API routes that execute these workflows in your client applications, such as your Medusa Admin customizations or your storefront.
102
103 <CardList itemsPerRow={2} items={[
104 {
105 href: "!docs!/learn/fundamentals/workflows",
106 title: "Workflows",
107 text: "Learn how to create a workflow.",
108 icon: AcademicCapSolid,
109 },
110 {
111 href: "!docs!/learn/fundamentals/api-routes",
112 title: "API Routes",
113 text: "Learn how to create API routes.",
114 icon: AcademicCapSolid,
115 },
116 ]} />
117
118 ---
119
120 ## Handle Ticket Products Inventory
121
122 By linking the `TicketProductVariant` model to the Product Module's `ProductVariant` model, you can leverage Medusa's inventory management features, implemented in the Inventory Module, to manage ticket product variants' inventory.
123
124 You can set up inventory items for ticket product variants based on show dates and seating sections. You can set the available quantity for each inventory item to reflect the number of seats available in that section for that show date.
125
126 This setup ensures that customers can only book tickets for available seats, preventing overbooking.
127
128 <CardList itemsPerRow={2} items={[
129 {
130 href: "/commerce-modules/inventory",
131 title: "Inventory Module",
132 text: "Learn about the Inventory Module and its features.",
133 icon: AcademicCapSolid,
134 },
135 {
136 href: "/commerce-modules/product/variant-inventory",
137 title: "Product Variant Inventory",
138 text: "Learn how to manage product variant inventory.",
139 icon: AcademicCapSolid,
140 },
141 ]} />
142
143 ---
144
145 ## Disable Shipping on Ticket Product Variants
146
147 By default, Medusa product variants require shipping, which prompts the customer to provide a shipping address and choose a shipping method during checkout.
148
149 You can disable shipping on ticket product variants by setting the `requires_shipping` property of the product variant's inventory item to `false`.
150
151 Then, you can remove shipping-related steps from the checkout flow in the storefront.
152
153 <Card
154 href="/commerce-modules/product/selling-products#configure-shipping-requirements"
155 title="Configure Shipping Requirements"
156 text="Learn how to configure shipping requirements for product variants."
157 icon={AcademicCapSolid}
158 />
159
160 ---
161
162 ## Customize Admin Dashboard
163
164 You can extend the Medusa Admin to provide merchants with an interface to manage ticket-booking-related data, such as shows and venues. You can inject widgets into existing pages or create new pages.
165
166 In your customizations, you send requests to the API routes you created that execute the workflows implementing ticket-booking-related features.
167
168 <CardList items={[
169 {
170 href: "!docs!/learn/fundamentals/admin/widgets",
171 title: "Create a Widget",
172 text: "Learn how to create a widget in the Medusa Admin.",
173 icon: AcademicCapSolid,
174 },
175 {
176 href: "!docs!/learn/fundamentals/admin/ui-routes",
177 title: "Create UI Route",
178 text: "Learn how to create a UI route in the Medusa Admin.",
179 icon: AcademicCapSolid,
180 },
181 ]} />
182
183 ---
184
185 ## Add Custom Validation on Cart Operations
186
187 Medusa's workflows provide hooks that allow you to inject custom logic at specific points in the workflow execution.
188
189 Specifically, workflows like `addToCartWorkflow` and `completeCartWorkflow` provide a `validate` hook that you can consume to add custom validation logic.
190
191 For example, you can consume the `validate` hook in the `addToCartWorkflow` to check if the selected seat is available before adding a ticket product variant to the cart.
192
193 <Card
194 href="!docs!/learn/fundamentals/workflows/workflow-hooks"
195 title="Workflow Hooks"
196 text="Learn how to use workflow hooks."
197 icon={AcademicCapSolid}
198 />
199
200 ---
201
202 ## Send Order Confirmation Email with QR Code Ticket
203
204 When an order is placed, Medusa emits an `order.placed` event. You can listen to this event in a subscriber, which is an asynchronous function executed in the background when its associated event is emitted.
205
206 In the subscriber, you can send an email using the Notification Module. You can register a Notification Module Provider, such as [SendGrid](../../infrastructure-modules/notification/sendgrid/page.mdx) or [Resend](../../integrations/guides/resend/page.mdx), that sends the email.
207
208 You can include a QR code representing the ticket in the email, which you can generate using a library like `qrcode`.
209
210 You can also implement an API route that verifies the QR code at the event entrance.
211
212 <CardList itemsPerRow={2} items={[
213 {
214 href: "!docs!/learn/fundamentals/events-and-subscribers",
215 title: "Events and Subscribers",
216 text: "Learn how to create a subscriber.",
217 icon: AcademicCapSolid,
218 },
219 {
220 href: "/infrastructure-modules/notification",
221 title: "Notification Module",
222 text: "Learn about available Notification Module Providers.",
223 icon: AcademicCapSolid,
224 },
225 ]} />
226
227 ---
228
229 ## Customize or Build Storefront
230
231 Customers can book tickets through your storefront. They need features like listing shows, selecting show dates and seats, and placing orders through a checkout flow that supports ticket products.
232
233 Medusa provides a Next.js Starter Storefront with standard commerce features including listing products, placing orders, and managing accounts. You can customize the storefront and tailor its functionalities to support ticket booking features.
234
235 Alternatively, you can build the storefront with your preferred tech stack.
236
237 <CardList items={[
238 {
239 href: "/nextjs-starter",
240 title: "Next.js Starter Storefront",
241 text: "Learn how to install and use the Next.js Starter Storefront.",
242 icon: AcademicCapSolid,
243 },
244 {
245 href: "/storefront-development",
246 title: "Storefront Guides",
247 text: "Learn how to build a storefront for your Medusa application.",
248 icon: AcademicCapSolid,
249 },
250 ]} />