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.

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

Syntax

function reserve(eventId: string, tickets: Array<TicketSelection>): Promise<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>

Related Content:

Was this helpful?

Reserve tickets

Code Example

Copy Code
1import wixEvents from 'wix-events';
2
3const selectedTickets = [
4 {"ticketId": "1234-123456-123456-1234", "quantity": 1},
5 {"ticketId": "2345-234567-234567-2345", "quantity": 2}
6];
7
8wixEvents.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

Code Example

Copy Code
1import wixEvents from 'wix-events';
2import wixData from 'wix-data';
3import wixPay from 'wix-pay';
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 will return 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 }
43 else {
44 $w("#ticketRepeater").hide();
45 console.log("Could not find tickets");
46 }
47 }
48 else {
49 console.log("Could not find event");
50 }
51}
52
53function displayTickets($item, itemData, index) {
54 $item("#ticketName").text = itemData.name;
55 $item("#ticketPrice").text = itemData.price.toString();
56 $item("#ticketQuantity").value = 0;
57}
58
59function reserveTickets() {
60 const selectedTickets = getSelectedTickets();
61
62 wixEvents.tickets.reserve(eventId, selectedTickets)
63 .then( (reservation) => {
64 reservationId = reservation.id;
65 } )
66 .catch( (error) => {
67 console.log("Error", error.message)
68 } );
69}
70
71function getSelectedTickets() {
72 let selectedTickets = [];
73
74 $w("#ticketRepeater").forEachItem( ($item, itemData, index) => {
75 if($item("#ticketQuantity").value > 0) {
76 selectedTickets.push( {
77 "ticketId": itemData._id,
78 "quantity": $item("#ticketQuantity").value
79 } );
80 }
81 } );
82
83 return selectedTickets;
84}
85
86function checkoutTickets() {
87 wixEvents.tickets.checkout(eventId, reservationId, {
88 "formValues": getFormValues(),
89 "coupon": $w("#couponCode").value
90 })
91 .then( ({order}) => {
92 orderNumber = order.orderNumber
93 wixPay.startPayment(order.paymentId);
94 // Note that PDF tickets are available before payment is complete
95 $w("ticketsPdfLink").value = order.ticketsPdf;
96 })
97 .catch( (error) => {
98 console.log("Error", error.message)
99 } );
100}
101
102function updateTickets() {
103 wixEvents.tickets.updateOrder(eventId, orderNumber, {
104 "formValues": getFormValues()
105 })
106 .catch( (error) => {
107 console.log("Error", error.message)
108 } );
109}
110
111function verifyCouponCode(value, reject) {
112 const coupon = $w("#couponCode").value;
113 wixEvents.tickets.verifyCoupon(eventId, reservationId, coupon)
114 .then( (result) => {
115 if (result.discountErrors) {
116 // handle verification failure
117 $w("#couponCode").updateValidityIndication();
118 $w("#couponErrorMsg").show();
119 $w("#couponSuccessMsg").hide();
120 reject("Coupon is invalid");
121 }
122 else {
123 // handle coupon verified
124 $w("#couponErrorMsg").hide()
125 $w("#couponSuccessMsg").show()
126 }
127 } );
128}
129
130function getFormValues() {
131 return [
132 {"name": "firstName", "value": $w("#firstName").value},
133 {"name": "lastName", "value": $w("#lastName").value},
134 {"name": "email", "value": $w("#email").value},
135 {"name": "custom", "value": $w("#foodAllergies").value},
136
137 // When a form contains an address, the way you format the
138 // address information for submission depends on what type
139 // of input elements you use to gather that information.
140
141 // Wix address input element.
142 {"name": "address", "value": $w("#address").value},
143
144 // Single element which is not an address
145 // input element, such as a text input.
146 {"name": "address", "value": [$w("#address").value]},
147
148 // Multiple elements for the
149 // various parts of an address.
150 {
151 "name": "address",
152 "value": [
153 $w("#street").value,
154 $w("#city").value,
155 $w("#state").value,
156 $w("#country").value,
157 $w("#postalCode").value
158 ]
159 },
160
161 // When a form contains an input for adding more guests to an
162 // RSVP, format the guest names for submission in an array
163 // where each element is the full name of a guest.
164 {"name": "additionalGuests", "value": $w("#additionalGuests").value},
165 {
166 "name": "guestNames",
167 "value": [
168 `${$w("#guest1FirstName").value} ${$w("#guest1LastName").value}`,
169 `${$w("#guest2FirstName").value} ${$w("#guest2LastName").value}`,
170 ]
171 }
172 ];
173}