Hello Stephen
I’s like to limit the number of places reserved (tickets) in a single booking to 4.
I’ve tried following this tutorial, by adding it to my snippets plugin “my-custom-code” folder in plugins. But the booking are being accepted when over the limit.
https://wp-event-organiser.com/blog/tutorial/limit-bookings-to-one-ticket/
Is this tutorial still valid or should I be adding it in a different way?
Many thanks for your help.
Madeleine
Madeleine Parkyn
Hi Madeleine,
It should still work, but there is an easier way to do this now:
add_action( 'eventorganiser_validate_booking_submission', function( $input, $form, $errors ){
$ticketpicker = $form->get_element( 'ticketpicker' );
$tickets = count($ticketpicker->get_value());
if($tickets > 1) {
$ticketpicker->add_error( 'too_many_tickets', 'You may only book 1 ticket for this event.' );
}
return $input;
}, 20, 3);
This only prevents multiple tickets in a single booking, it doesn’t limit a customer to only one booking, though.
Stephen Harris
Thanks so much for the supper speedy reply.
That has worked perfectly.
Many thanks
Madeleine
Madeleine Parkyn
Hi Stephen, Im trying to implement this code so that I can both limit purchasers to 2 tickets max. I have added it to my functions.php as follows but currently have the following issues:
- does not prevent more than 2 tickets being purchased in 1st transaction
- does prevent second transaction but does not show error message
any help would be much appreciated, cheers!
function xmas2021_limit_tickets_in_booking( $input, $form, $errors ){
$input = $input + array( 'event_id' => 0, 'occurrence_id' => 0, 'gateway' => false, 'tickets'=>array() );
$tickets = $input['tickets'];
$post_id = $input['event_id'];
$occurrence_id = $input['occurrence_id'];
/* $tickets is an array ( ticket type ID => quantity purchased ).
Remove ticket types that have not been selected (0 quantity) */
$tickets = array_filter( $tickets );
/* Throw error if user has previously made a booking for this event */
if( eo_user_has_bookings( get_current_user_id(), $post_id, $occurrence_id ) ){
//This error will prevent the booking from being processed, and display the given error message
$errors->add( 'previous_booking', 'You have already bought tickets for this event.' );
}
/* Count how many tickets are in this booking */
$total_qty =0;
if ( $tickets ) {
$total_qty = array_sum( $tickets );
}
/* Throw error if booking contains multiple tickets */
if( $total_qty > 2 ){
$errors->add( 'too_many_tickets', 'You can only purchase two tickets maximum.' );
}
return $input;
}
add_action( 'eventorganiser_validate_booking_submission', 'xmas2021_limit_tickets_in_booking', 20, 3 );
-
This reply was modified 3 years, 1 month ago by Joe Fairlamb.
Joe Fairlamb
Hi Joe,
For errors to appear you’ll need to add them to a form element (e.g. the ticketpicker element):
<?php
add_action( 'eventorganiser_validate_booking_submission', function( $input, $form, $errors ){
// Prevent booking more than 2 tickets
$ticketpicker = $form->get_element( 'ticketpicker' );
$tickets = count($ticketpicker->get_value());
if($tickets > 2) {
$ticketpicker->add_error( 'too_many_tickets', 'You may only book 1 ticket for this event.' );
return $input;
}
// Prevent multiple confirmed bookings
$event_id = $input['event_id'];
$occurrence_id = $input['occurrence_id'];
$confirmed = eventorganiser_get_bookings( array(
'status' => eo_get_confirmed_booking_statuses(),
'fields'=>'ids',
'occurrence_id'=> $occurrence_id,
'event_id'=> $post->ID
));
$ticketpicker = $form->get_element( 'ticketpicker' );
if(count($confirmed) > 0) {
$ticketpicker->add_error( 'too_many_bookings', 'This event is no longer accepting bookings.' );
}
return $input;
}, 20, 3);
<
p>“`</p>
<p>.</p>
Stephen Harris