• Is it possible to limit users to 1 event booked at a time? This should apply across all available events in the system, not just a singular event with multiple occurrences.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You could use the following code snippet to restrict users to 1 event booking at a time (it won’t work for guest signups).

    add_filter('em_booking_validate', 'restrict_user_to_one_booking_total', 10, 2);

    function restrict_user_to_one_booking_total($result, $EM_Booking) {
    // Only apply if the user is logged in
    if (!is_user_logged_in()) {
    return $result;
    }

    // Skip check for admins
    if (current_user_can('manage_options')) {
    return $result;
    }

    global $wpdb;
    $table_bookings = $wpdb->prefix . 'em_bookings';
    $current_user_id = get_current_user_id();

    // Check for existing bookings that are pending (0, 5) or confirmed (1)
    // We ignore rejected (2) and cancelled (3)
    $active_booking_count = $wpdb->get_var($wpdb->prepare(
    "SELECT COUNT(*) FROM $table_bookings
    WHERE person_id = %d
    AND booking_status IN (0, 1, 5)",
    $current_user_id
    ));

    if ($active_booking_count > 0) {
    $EM_Booking->add_error('You already have an existing event booking. You are limited to one active booking at a time.');
    return false;
    }

    return $result;
    }

    You can use the Code Snippets plugin to add this code snippet.

    Here’s an updated version of the code snippet that only checks for bookings on future events (that were not canceled).

    add_filter('em_booking_validate', 'restrict_user_to_one_future_booking', 10, 2);

    function restrict_user_to_one_future_booking($result, $EM_Booking) {
    // Only apply if the user is logged in
    if (!is_user_logged_in()) {
    return $result;
    }

    // Skip check for admins/editors
    if (current_user_can('manage_options')) {
    return $result;
    }

    global $wpdb;
    $table_bookings = $wpdb->prefix . 'em_bookings';
    $table_events = $wpdb->prefix . 'em_events';
    $current_user_id = get_current_user_id();
    $today = date('Y-m-d');

    // Counts bookings only if:
    // 1. The booking status is pending (0, 5) or confirmed (1)
    // 2. The event date is today or in the future
    // 3. The event itself is marked as active (status 1)
    $active_booking_count = $wpdb->get_var($wpdb->prepare(
    "SELECT COUNT(*)
    FROM $table_bookings b
    INNER JOIN $table_events e ON b.event_id = e.event_id
    WHERE b.person_id = %d
    AND b.booking_status IN (0, 1, 5)
    AND e.event_start_date >= %s
    AND e.event_status = 1",
    $current_user_id,
    $today
    ));

    if ($active_booking_count > 0) {
    $EM_Booking->add_error('You already have an existing booking for a future event. You are limited to one active future booking at a time.');
    return false;
    }

    return $result;
    }
Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.