Bookings that run the same time

This topic contains 4 replies, has 2 voices, and was last updated by  Jody Brown 9 years, 9 months ago.

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

    Hi Stephen.

    Is it possible to not allow bookings that fall into the same time spot ?

    for example: Concert – 8-9pm (free) Concert 2 8-9pm (free)

    At this stage if I use the example above my users can book for both events even though they overlap. Doesn’t really help the attendance figures (we have limited spaces) if a user gets greedy and just books them all

    My users have to be logged in to book.

    Thanks Stephen

    Jody

    Jody Brown
    #11926

    Hi Jody,

    This is possible. On the booking form validation hook (eventorganiser_validate_booking_form_submission) you can do one of two things:

    • Look up events this user is attending and check the dates don’t overlap
    • Look up all concurrent events and check that the user has not booked for any of them.

    The only difference will a slight performance gain depending on whether you have a lot concurrent events or potentially users booking a lot of events. The latter is probably more likely, so below that’s what I’ve gone with. It requires Pro 1.7+ because of the newer hook used (there is another deprecated one used in old versions).

    function my_check_bookings_dont_clash( $form ){
        $event_id = (int) $form->get( 'event_id' );
        //I'm assuming you're booking by date too
        $occurrence_id = (int) $form->get( 'occurrence_id' );
        $event_starts = eo_get_the_start( DATETIMEOBJ, $event_id, null, $occurrence_id );
        $event_ends = eo_get_the_end( DATETIMEOBJ, $event_id, null, $occurrence_id );
    
         //Find all concurrent events
         $events = eo_get_events( array(
              'event_start_before' => $event_ends,
              'event_end_after' => $event_starts,
         ));
    
         if( $events ){
              //Get all statuses except 'cancelled'. You may want to change this if you want to ignore 'pending' bookings as well.
              $statuses = eo_get_booking_statuses( array( 'name' => 'cancelled' ), 'names', 'not' );
              $user_id = get_current_user_id();
    
              foreach( $events as $event ){
                  if( eo_user_has_bookings( $user_id,  $event->ID, $event->occurrence_id, $statuses ){
                         $form->add_error( 
                              'booking-clash', 
                              sprintf( 'Sorry you\'re already attending "%s" at that time.', get_the_title( $event->ID ) )
                         );
                  }
              }
         }
    }
    add_action( 'eventorganiser_validate_booking_form_submission', 'my_check_bookings_dont_clash', 15 );

    This hasn’t been untested, so there maybe a few typos.

    Remarks

    • You can change the statuses which are checked. In the above we check
      if the user has any bookings for any concurrent event which is not cancelled.
    • It doesn’t ignore the current event being booked. If you want the user to be able to make an additional booking for the same
      event, you’ll want to skip where the concurrent event is the same as
      the current event being booked
    Stephen Harris
    #11937

    Thanks Stephen

    Where should this code go ?

    If one event finishes at 9pm and the next event starts at 9pm will this be an issue ?

    Jody Brown
    #11939

    Where should this code go ?

    Ideally, its own dedicated plug-in or a site utility function. It will work in your theme’s functions.php but this adds hassle when switching theme.

    If one event finishes at 9pm and the next event starts at 9pm will this be an issue ?

    Yes. You can work around this by doing

    $event_starts = clone eo_get_the_start( DATETIMEOBJ, $event_id, null, $occurrence_id );
    $event_ends   = clone eo_get_the_end( DATETIMEOBJ, $event_id, null, $occurrence_id );
    $event_starts->modify( '+1 second' );
    $event_ends->modify( '-1 second' );

    The clones are important.

    Stephen Harris
    #11940

    Hi Stephen

    sent email regards freelance to implement this.

    Jody Brown
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.