Detecting when a booking changes status

You may wish to detect when a booking’s status changes, so that you can perform some action (for instance, e-mail the bookee that their booking has been cancelled).

To detect when a booking changes status you can whose one of three hooks:

  1. {$old_status}_to_{$new_status} – to detect when a booking goes from one specific status to another
  2. {$new_status}_eo_booking – to detect when a bookings changes to a specific status
  3. transition_post_status – to detect when a booking change status

(where {$old_status} and {$new_status} should be replaced by the appropriate status).

Note: These are WordPress hooks, not ones provided by the plug-in which is why they do not follow the naming convention of eventorganiser_. Additionally these hooks are triggered for all post types (except number 2), so you should check whether the post changing status is in fact a booking.

When a booking goes from status X to status Y

In this example we e-mail the bookee whenever the booking goes from ‘confirmed’ to ‘cancelled’:

add_action( 'confirmed_to_cancelled', 'my_booking_status_transition' );
function my_booking_status_transition( $post ) {

    if( get_post_type( $post ) != 'eo_booking' ){
        return;
    }

    $booking_id = (int) $post->ID;

    //If you need it:
    $event_id = (int) eo_get_booking_meta( $booking_id, 'event_id' );
    $occurrence_id = (int) eo_get_booking_meta( $booking_id, 'occurrence_id' );
    $booking_amount = (int) eo_get_booking_meta( $booking_id, 'booking_amount' );
    $user_id = (int) eo_get_booking_meta( $booking_id, 'bookee' );

    /* Set the recipients email */
    $bookee_email = eo_get_booking_meta( $booking_id, 'bookee_email' );

    /* Set email template - optional */
    $template = eventorganiser_pro_get_option( 'email_template' );

    /* Set headers - optional */
    $from_name = get_bloginfo( 'name' );
    $from_email = get_option( 'admin_email' );
    $headers = "From: " . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . "  <$from_email>\r\n";
    $headers .= "Reply-To: ". $from_email . "\r\n";

    /* Set message */
    $message = "Dear %display_name%. <p>This is to inform you that your booking (reference#: %booking_reference%) ";
    $message .= "for %event_title%, %event_date% has been cancelled</p>";
    $message = eventorganiser_email_template_tags( $message , $booking_id, $template );//Parses the email tags

    /* Set subject */
    $subject = 'Your booking has been cancelled';

    /* Send message */
    eventorganiser_mail( $bookee_email, $subject, $message, $headers, array(), $template );

}

Whenever a booking goes to status X

The following does the same thing, but this time it does not matter what status the booking originally had.

 add_action( 'cancelled_eo_booking', 'my_booking_status_transition', 10, 2 );
 function my_booking_status_transition( $booking_id, $post ) {
    //as above
 }

Whenever a booking status changes

Finally to do something whenever the booking status changes (regardless of what it’s changing from or to)

 add_action( 'transition_post_status', 'my_booking_status_transition', 10, 3 );
 function my_booking_status_transition( $new_status, $old_status, $post ) {
    if( $new_status == $old_status ){
       //no change
       return;
    }

    if( get_post_type( $post ) != 'eo_booking' ){
        return;
    }

    //as above
 }