Action uncanny-automator-pro

mec_booking_completed

Fires after a booking is successfully completed for an event within the Modern Events Calendar.

add_action( 'mec_booking_completed', $callback, 10, 1 );

Description

Fires after a booking is successfully completed within Modern Events Calendar. Developers can use this hook to trigger custom actions, send notifications, or update related data when a new booking is confirmed. The `$booking_id` parameter provides access to the specific booking that was completed.


Usage

add_action( 'mec_booking_completed', 'your_function_name', 10, 1 );

Parameters

$booking_id (mixed)
This parameter contains the ID of the booking that has just been completed.

Examples

<?php
/**
 * Example function to hook into the mec_booking_completed action.
 * This function will be executed after a booking is successfully completed in Modern Events Calendar.
 *
 * @param int $booking_id The ID of the completed booking.
 */
function my_custom_mec_booking_completed_handler( $booking_id ) {
    // Ensure we have a valid booking ID.
    if ( ! $booking_id || ! is_numeric( $booking_id ) ) {
        // Log an error or handle invalid input appropriately.
        error_log( 'my_custom_mec_booking_completed_handler received an invalid booking ID: ' . print_r( $booking_id, true ) );
        return;
    }

    // Fetch booking details using the MEC API or helper functions.
    // This is a placeholder, actual MEC functions might vary.
    $booking = MEC_gateway::get_booking( $booking_id );

    if ( ! $booking ) {
        // Booking not found, perhaps it was deleted or an error occurred.
        error_log( 'my_custom_mec_booking_completed_handler: Could not retrieve booking details for ID: ' . $booking_id );
        return;
    }

    // Example: Send a custom email to the attendee or administrator.
    $event_name = isset( $booking['event']['title'] ) ? $booking['event']['title'] : __( 'Unnamed Event', 'your-text-domain' );
    $attendee_email = isset( $booking['email'] ) ? $booking['email'] : false;
    $attendee_name = isset( $booking['name'] ) ? $booking['name'] : __( 'Valued Customer', 'your-text-domain' );

    if ( $attendee_email ) {
        $subject = sprintf( __( 'Your Booking for %s is Confirmed!', 'your-text-domain' ), $event_name );
        $message = sprintf(
            __( 'Hello %s,nnYour booking for the event "%s" has been successfully completed. We look forward to seeing you there!nnBooking ID: %dnnThank you.', 'your-text-domain' ),
            $attendee_name,
            $event_name,
            $booking_id
        );

        // Use WordPress's wp_mail() function for sending emails.
        $mail_sent = wp_mail( $attendee_email, $subject, $message );

        if ( ! $mail_sent ) {
            error_log( 'my_custom_mec_booking_completed_handler: Failed to send confirmation email to ' . $attendee_email . ' for booking ID ' . $booking_id );
        } else {
            // Optional: Log successful email sending.
            // error_log( 'my_custom_mec_booking_completed_handler: Confirmation email sent to ' . $attendee_email . ' for booking ID ' . $booking_id );
        }
    }

    // Example: Update a custom user meta field if the booking is for a specific event.
    // Replace 'your_specific_event_slug' with the actual slug of an event you're targeting.
    $target_event_slug = 'your_specific_event_slug';
    if ( isset( $booking['event']['slug'] ) && $booking['event']['slug'] === $target_event_slug ) {
        $user_id = get_current_user_id(); // Assuming the booking is associated with a logged-in user.
        if ( $user_id ) {
            update_user_meta( $user_id, 'attended_' . $target_event_slug, true );
            // error_log( 'my_custom_mec_booking_completed_handler: Marked user ' . $user_id . ' as attended for event ' . $target_event_slug );
        }
    }

    // Example: Add the booking ID to a custom post type entry (e.g., a "Bookings Log").
    // This requires a custom post type to be registered.
    $post_data = array(
        'post_title'    => 'Booking Completed: #' . $booking_id . ' for ' . $event_name,
        'post_status'   => 'publish',
        'post_type'     => 'your_custom_bookings_log_cpt', // Replace with your CPT slug.
        'meta_input'    => array(
            'mec_booking_id' => $booking_id,
            'event_title'    => $event_name,
            'attendee_email' => $attendee_email,
        ),
    );
    // wp_insert_post( $post_data ); // Uncomment to enable this functionality.
}

// Add the action hook.
// The second parameter '3' specifies the number of arguments the hooked function accepts.
// The third parameter '10' is the default priority.
add_action( 'mec_booking_completed', 'my_custom_mec_booking_completed_handler', 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/modern-events-calendar/actions/mec-book-user.php:230

do_action( 'mec_booking_pended', $booking_id );

		// Send notification if its a new booking.
		try {

			if ( $this->is_new_booking( $booking_id ) ) {

				do_action( 'mec_booking_completed', $booking_id );

			}
		} catch ( Exception $e ) {

			automator_log( $e->getMessage(), 'MEC Error', true, 'mec' );

		}


Scroll to Top