Crocoblock / suggestions

The suggestions for CrocoBlock project
191 stars 78 forks source link

checking number of reservations before check-in #7566

Open absitwebb opened 2 weeks ago

absitwebb commented 2 weeks ago

Good morning, I want to use a jet engine selection form and field to determine if the person can still reserve seats or in the desired slot in jet appointment.

Example: the person indicates on the form with the selection field that they wish to reserve for 5 people. It then selects the date and time. On the slot where she wishes to reserve there are only 2 places left. She receives a message telling her that she cannot reserve for 5 people, there are not enough places left. This is what I would like to do.

I would like to use a Hook to achieve this result. Here is the function, but it doesn't work. thank you for being

`function check_appointment_availability($request) { global $wpdb; $table_name = jet_apb()->db->appointments->table();

`function check_appointment_availability($request) { global $wpdb; $table_name = jet_apb()->db->appointments->table();

// Récupération des données de date et de créneau horaire depuis le tableau
$appointment_data = $_REQUEST['appointment_date'];

// Vérifiez que les clés existent dans le tableau pour éviter les erreurs
if (isset($appointment_data['date']) && isset($appointment_data['slot'])) {
    $appointment_date = $appointment_data['date'];
    $time_slot = $appointment_data['slot'];
} else {
    // Gérer le cas où les données sont manquantes
    return new WP_Error('invalid_request', 'Les informations de date ou de créneau horaire sont manquantes.');
    }

$max_seats = 2;  // Remplacer par le nombre maximum de places pour ce créneau horaire

// Requête pour compter le nombre de réservations existantes pour cette date et ce créneau horaire

$query = $wpdb->prepare( "SELECT SUM(nbr_pers) FROM $table_name WHERE date = %s AND slot = %s", $appointment_date, $time_slot );

$existing_appointments = $wpdb->get_var($query);

if ($existing_appointments >= $max_seats) {
    // Refuser la réservation et retourner une réponse d'erreur
    return new WP_Error('max_seats_reached', 'Le nombre maximum de places pour ce créneau horaire a été atteint.');
}

// Continuer avec la réservation si le nombre maximum n'est pas atteint
return $request;

}

// Hook pour vérifier la disponibilité avant de créer une nouvelle réservation add_filter('jet-appointments-booking/insert-appointment/request', 'check_appointment_availability', 10, 1); `