Advanced notification for WooCommerce Bookings Cancellation

Looking to create an advanced notification to email specific addresses if a WooCommerce Booking gets cancelled? Here’s our complete functions.php guide.

Specific WooCommerce booking cancellation email address

Similar to WooCommerce’s Advanced Notifications plugin, this code snippet allows similar functionality for when a WooCommerce booking has been cancelled.

You’ll need to create a custom field (using ACF) named custom_email_address and assign this to the WooCommerce product’s post type.

Once you’ve created the field, simply add this code to your WordPress theme’s functions.php file:

function custom_booking_cancelled_handler( $booking_id ) {
     $booking = new WC_Booking( $booking_id );

     // ##### ADVANCED CUSTOM FIELDS IS REQUIRED FOR THE get_field FUNCTION #####
     $custom_email = get_field( 'custom_email_address', $booking->product_id );

     $customer_email = get_userdata( $booking->customer_id )->user_email;
     $message = 'Your booking order number #' . $booking_id . ' has been cancelled.';
     ob_start();
     wc_get_template( 'emails/email-header.php', array( 'email_heading' => 'Order Cancelled!' ) );
     echo $message;
     wc_get_template( 'emails/email-footer.php' );
     $message = ob_get_clean();
     $subject = get_bloginfo( 'name' ) . ' Order Cancelled';
     $mailer = WC()->mailer();
     $mailer->send( $custom_email, $subject, $message);
     $mailer->send( $customer_email, $subject, $message);
}
add_action( 'woocommerce_booking_cancelled', 'custom_booking_cancelled_handler', 10, 1 );