daily pastebin goal
27%
SHARE
TWEET

Bookings requiring another event booking

eventsmanager Mar 11th, 2018 191 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * The function below requires a previous event to be booked, or for that required event to be present in the booking cart.
  4.  * Once installed, add a new custom field called 'Parent Event ID' and then add the event ID of the required event booking to your secondary event.
  5.  * For instructions on how to install this snippet, see here - http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  6. */
  7.  
  8. /**
  9.  * @param boolean $result
  10.  * @param EM_Booking $EM_Booking
  11.  */
  12. function my_em_booking_validate_dependence( $result, $EM_Booking ){
  13.     //check if the event has a designated parent event
  14.     $EM_Event = $EM_Booking->get_event();
  15.     if( !empty($EM_Event->event_attributes['Parent Event ID']) ){
  16.         //get the parent event and make sure it exists
  17.         $EM_Event = em_get_event($EM_Event->event_attributes['Parent Event ID']);
  18.         if( empty($EM_Event->event_id) ) return $result;
  19.         $has_booked_parent_event = false;
  20.         //if user isn't logged in, we fail validation as they must log in so we can verify their booking of the parent event. If MB is enabled, we skip since they may have booked both events at once
  21.         if( !is_user_logged_in() && !get_option('dbem_multiple_bookings') ){
  22.             $EM_Booking->add_error("You must have previously booked '{$EM_Event->event_name}' before this event. Please log in so we can verify your previous bookings.");
  23.             return false;
  24.         }
  25.         //check if user has previously boooked this parent event
  26.         if( is_user_logged_in() ){
  27.             //make sure we search all events 'owner'=>false and only booked or pending offline payment events 'status'=>'1,5'
  28.             $EM_Bookings = EM_Bookings::get( array('person' => get_current_user_id(), 'event' => $EM_Event->event_id, 'owner'=>false, 'status' => '1,5') );
  29.             if( !empty($EM_Bookings->bookings) ){
  30.                 $has_booked_parent_event = true;
  31.             }
  32.         }
  33.         //check if this is booking mod and the event is already in the cart
  34.         if( !$has_booked_parent_event && get_option('dbem_multiple_bookings') ){
  35.             $EM_Multiple_Booking = EM_Multiple_Bookings::get_multiple_booking();
  36.             foreach( $EM_Multiple_Booking->get_bookings() as $em_booking ){ /* @var EM_Booking $em_booking */
  37.                 if( $em_booking->event_id == $EM_Event->event_id ){
  38.                     $has_booked_parent_event = true;
  39.                     break;
  40.                 }
  41.             }
  42.         }
  43.         //if parent booking not found, provide error message and fail validation
  44.         if( !$has_booked_parent_event ){
  45.             $EM_Booking->add_error("You must book '".$EM_Event->output('#_EVENTLINK')."' in order to attend this event.");
  46.             $result = false;
  47.         }
  48.     }
  49.     return $result;
  50. }
  51. add_filter('em_booking_validate', 'my_em_booking_validate_dependence', 10, 2);
  52.  
  53. function my_em_multiple_booking_validate_dependence( $result, $EM_Multiple_Booking ){
  54.     foreach( $EM_Multiple_Booking->get_bookings() as $EM_Booking ){ /* @var EM_Booking $EM_Booking */
  55.         if( !my_em_booking_validate_dependence( true, $EM_Booking ) ){
  56.             $result = false;
  57.         }
  58.     }
  59.     return $result;
  60. }
  61. add_filter('em_multiple_booking_validate','my_em_multiple_booking_validate_dependence', 10, 2);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand