Limiting number of tickets in a booking

WordPress Event Management, Calendars & Registration Forums General Question Limiting number of tickets in a booking

This topic contains 4 replies, has 3 voices, and was last updated by  Stephen Harris 2 years, 4 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #38499

    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
    #38500

    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
    #38502

    Thanks so much for the supper speedy reply.
    That has worked perfectly.

    Many thanks
    Madeleine

    Madeleine Parkyn
    #40498

    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:

    1. does not prevent more than 2 tickets being purchased in 1st transaction
    2. 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 2 years, 4 months ago by  Joe Fairlamb.
    Joe Fairlamb
    #40566

    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
Viewing 5 posts - 1 through 5 (of 5 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.