Search.../

reserve( )

Reserves tickets for an event.

Description

The reserve() function returns a Promise that resolves to a ReservationResponse when the tickets have been reserved.

Retrieve ticket IDs to specify which tickets to reserve from the Events/Tickets collection. Each ticket must have a unique ID.

Note: To work with the Wix Events API, you need to publish your site.

Note: You can use the backend createReservation() function to reserve tickets.

Syntax

function reserve(eventId: string, tickets: Array<TicketSelection>): ReservationResponse

reserve Parameters

NAME
TYPE
DESCRIPTION
eventId
string

ID of the event to reserve tickets for.

tickets
Array<TicketSelection>

Tickets to reserve.

Returns

Fulfilled - Information about the reservations that were created.

Return Type:

Promise<ReservationResponse>
NAME
TYPE
DESCRIPTION
id
string

Reservation ID.

reservations
Array<Reservation>

Ticket reservations.

invoice
Invoice

Ticket reservations invoice.

expirationTime
Date

Time the reservations expire.

Related Content:

Was this helpful?

Reserve tickets

Copy Code
1import wixEventsFrontend from 'wix-events-frontend';
2
3const selectedTickets = [
4 {"ticketId": "1234-123456-123456-1234", "quantity": 1},
5 {"ticketId": "2345-234567-234567-2345", "quantity": 2}
6];
7
8wixEventsFrontend.tickets.reserve(eventId, selectedTickets)
9 .then((response) => {
10 let reservationId = response.id;
11 let expirationTime = response.expirationTime;
12
13 let firstReservation = response.reservations[0];
14
15 let invoice = response.invoice;
16 let grandTotal = invoice.grandTotal.amount;
17
18 });
19
20/* response:
21 *
22 * {
23 * "id": "97948a8a-e902-453d-a0a9-5ee6a674fd36",
24 * "reservations": [
25 * {
26 * "quantity": 1,
27 * "ticket": {
28 * "price": {
29 * "amount": "6.00",
30 * "currency": "USD"
31 * },
32 * "free": false,
33 * "name": "VIP ticket",
34 * "description": "Super special ticket",
35 * "limitPerCheckout": 0,
36 * "orderIndex": 0,
37 * "policy": "No returns",
38 * "eventId": "9a513b88-a223-4144-b0dc-938fd7cc5b4a",
39 * "_id": "de8e3fd8-b257-40a7-933b-77c627d884e4"
40 * }
41 * }, {
42 * "quantity": 2,
43 * "ticket": {
44 * "price": {
45 * "amount": "3.50",
46 * "currency": "USD"
47 * },
48 * "free": false,
49 * "name": "Regular ticket",
50 * "description": "Just a regular ticket",
51 * "limitPerCheckout": 0,
52 * "orderIndex": 0,
53 * "policy": "No refunds",
54 * "eventId": "9a513b88-a223-4144-b0dc-938fd7cc5b4a",
55 * "_id": "9207637c-3d49-43af-b91e-32e496620c60"
56 * }
57 * }
58 * ],
59 * "invoice": {
60 * "items": [
61 * {
62 * "id": "de8e3fd8-b257-40a7-933b-77c627d884e4",
63 * "quantity": 2,
64 * "name": "VIP ticket",
65 * "price": {
66 * "amount": "6.00",
67 * "currency": "USD"
68 * },
69 * "total": {
70 * "amount": "12.00",
71 * "currency": "USD"
72 * },
73 * "discount":{
74 * "amount":{
75 * "amount":"2.0",
76 * "currency":"USD"
77 * },
78 * "afterDiscount":{
79 * "amount":"10.00",
80 * "currency":"USD"
81 * },
82 * "code":"CouponCode",
83 * "name":"Coupon Name",
84 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59",
85 * "discounts":[
86 * {
87 * "amount":{
88 * "amount":"2.0",
89 * "currency":"USD"
90 * },
91 * "coupon":{
92 * "name":"Coupon Name",
93 * "code":"CouponCode",
94 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59"
95 * }
96 * }
97 * ]
98 * },
99 * "fees": [
100 * {
101 * "name": "WIX_FEE",
102 * "type": "FEE_ADDED",
103 * "rate": "2.5",
104 * "amount": {
105 * "amount": "0",
106 * "currency": "USD"
107 * }
108 * }
109 * ]
110 * },
111 * {
112 * "id": "9207637c-3d49-43af-b91e-32e496620c60",
113 * "quantity": 1,
114 * "name": "Regular ticket",
115 * "price": {
116 * "amount": "3.50",
117 * "currency": "USD"
118 * },
119 * "total": {
120 * "amount": "3.50",
121 * "currency": "USD"
122 * },
123 * "discount":{
124 * "amount":{
125 * "amount":"1.0",
126 * "currency":"USD"
127 * },
128 * "afterDiscount":{
129 * "amount":"2.50",
130 * "currency":"USD"
131 * },
132 * "code":"CouponCode",
133 * "name":"Coupon Name",
134 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59",
135 * "discounts":[
136 * {
137 * "amount":{
138 * "amount":"1.0",
139 * "currency":"USD"
140 * },
141 * "coupon":{
142 * "name":"Coupon Name",
143 * "code":"CouponCode",
144 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59"
145 * }
146 * }
147 * ]
148 * },
149 * "fees": [
150 * {
151 * "name": "WIX_FEE",
152 * "type": "FEE_ADDED",
153 * "rate": "2.5",
154 * "amount": {
155 * "amount": "0",
156 * "currency": "USD"
157 * }
158 * }
159 * ]
160 * }
161 * ],
162 * "total":{
163 * "amount":"15.50",
164 * "currency":"BIF"
165 * },
166 * "discount":{
167 * "amount":{
168 * "amount":"3.0",
169 * "currency":"BIF"
170 * },
171 * "afterDiscount":{
172 * "amount":"12.50",
173 * "currency":"BIF"
174 * },
175 * "code":"CouponCode",
176 * "name":"Coupon Name",
177 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59",
178 * "discounts":[
179 * {
180 * "amount":{
181 * "amount":"3.0",
182 * "currency":"BIF"
183 * },
184 * "coupon":{
185 * "name":"Coupon Name",
186 * "code":"CouponCode",
187 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59"
188 * }
189 * }
190 * ]
191 * },
192 * "subTotal":{
193 * "amount":"15.50",
194 * "currency":"BIF"
195 * },
196 * "grandTotal":{
197 * "amount":"12.50",
198 * "currency":"BIF"
199 * },
200 * "fees":[],
201 * "revenue":{
202 * "amount":"12.50",
203 * "currency":"BIF"
204 * }
205 * },
206 * "expirationTime": "2020-04-22T12:49:59.233Z"
207 * }
208 */
Reserve, checkout, and update tickets

Copy Code
1import wixEventsFrontend from 'wix-events-frontend';
2import wixData from 'wix-data';
3import wixPayFrontend from 'wix-pay-frontend';
4
5let eventId;
6let reservationId;
7let orderNumber;
8let tickets;
9
10$w.onReady(function () {
11 // Retrieve data and set up page
12 await getData();
13 $w("#ticketRepeater").onItemReady(displayTickets);
14 $w("#ticketRepeater").data = tickets;
15
16 // Define button click actions
17 $w("#reservationButton").onClick(reserveTickets);
18 $w("#checkoutButton").onClick(checkoutTickets);
19 $w("#updateButton").onClick(updateTickets);
20
21 // Verify coupon code when it is entered
22 $w("#couponCode").onCustomValidation(verifyCouponCode);
23});
24
25async function getData() {
26 // Run a query that returns only one event. Add
27 // additional filtering to the query if necessary.
28 const eventResults = await wixData.query("Events/Events")
29 .eq("title", "My Event")
30 .find();
31
32 if (eventResults.items.length > 0) {
33 eventId = eventResults.items[0]._id;
34
35 // Get tickets for the event
36 const ticketResults = await wixData.query("Events/Tickets")
37 .eq("event", eventId)
38 .find();
39
40 if (ticketResults.items.length > 0) {
41 tickets = ticketResults.items;
42 } else {
43 $w("#ticketRepeater").hide();
44 console.log("Could not find tickets");
45 }
46 } else {
47 console.log("Could not find event");
48 }
49}
50
51function displayTickets($item, itemData, index) {
52 $item("#ticketName").text = itemData.name;
53 $item("#ticketPrice").text = itemData.price.toString();
54 $item("#ticketQuantity").value = 0;
55}
56
57function reserveTickets() {
58 const selectedTickets = getSelectedTickets();
59
60 wixEventsFrontend.tickets.reserve(eventId, selectedTickets)
61 .then((reservation) => {
62 reservationId = reservation.id;
63 })
64 .catch((error) => {
65 console.log("Error", error.message)
66 });
67}
68
69function getSelectedTickets() {
70 let selectedTickets = [];
71
72 $w("#ticketRepeater").forEachItem(($item, itemData, index) => {
73 if ($item("#ticketQuantity").value > 0) {
74 selectedTickets.push( {
75 "ticketId": itemData._id,
76 "quantity": $item("#ticketQuantity").value
77 });
78 }
79 });
80
81 return selectedTickets;
82}
83
84function checkoutTickets() {
85 wixEventsFrontend.tickets.checkout(eventId, reservationId, {
86 "formValues": getFormValues(),
87 "coupon": $w("#couponCode").value
88 })
89 .then(({order}) => {
90 orderNumber = order.orderNumber
91 wixPayFrontend.startPayment(order.paymentId);
92 // Note that PDF tickets are available before payment is complete
93 $w("#ticketsPdfLink").value = order.ticketsPdf;
94 })
95 .catch((error) => {
96 console.log("Error", error.message)
97 });
98}
99
100function updateTickets() {
101 wixEventsFrontend.tickets.updateOrder(eventId, orderNumber, {
102 "formValues": getFormValues()
103 })
104 .catch((error) => {
105 console.log("Error", error.message)
106 });
107}
108
109function verifyCouponCode(value, reject) {
110 const coupon = $w("#couponCode").value;
111 wixEventsFrontend.tickets.verifyCoupon(eventId, reservationId, coupon)
112 .then((result) => {
113 if (result.discountErrors) {
114 // handle verification failure
115 $w("#couponCode").updateValidityIndication();
116 $w("#couponErrorMsg").show();
117 $w("#couponSuccessMsg").hide();
118 reject("Coupon is invalid");
119 } else {
120 // handle coupon verified
121 $w("#couponErrorMsg").hide()
122 $w("#couponSuccessMsg").show()
123 }
124 });
125}
126
127function getFormValues() {
128 return [
129 {"name": "firstName", "value": $w("#firstName").value},
130 {"name": "lastName", "value": $w("#lastName").value},
131 {"name": "email", "value": $w("#email").value},
132 {"name": "custom", "value": $w("#foodAllergies").value},
133
134 // When a form contains an address, the way you format the
135 // address information for submission depends on what type
136 // of input elements you use to gather that information.
137
138 // Wix address input element.
139 {"name": "address", "value": $w("#address").value},
140
141 // Single element which is not an address
142 // input element, such as a text input.
143 {"name": "address", "value": [$w("#address").value]},
144
145 // Multiple elements for the
146 // various parts of an address.
147 {
148 "name": "address",
149 "value": [
150 $w("#street").value,
151 $w("#city").value,
152 $w("#state").value,
153 $w("#country").value,
154 $w("#postalCode").value
155 ]
156 },
157
158 // When a form contains an input for adding more guests to an
159 // RSVP, format the guest names for submission in an array
160 // where each element is the full name of a guest.
161 {"name": "additionalGuests", "value": $w("#additionalGuests").value},
162 {
163 "name": "guestNames",
164 "value": [
165 `${$w("#guest1FirstName").value} ${$w("#guest1LastName").value}`,
166 `${$w("#guest2FirstName").value} ${$w("#guest2LastName").value}`,
167 ]
168 }
169 ];
170}