Restrict users to one booking per time slot in WooCommerce Bookings [closed]

Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 2 hours ago. Improve this question I'm making a booking website for my yoga studio using woocommerce bookings on wordpress. I would like to restrict each user to only one booking per timeslot. Example: Yoga class availability: 20 places per class. Monday: 9am & Tuesday 10 am. If the user books a class on Mondat at 9am, if they try to reserve another yoga class, only the Tuesday 10am slot should be available on the calendar. I tried using the wc_bookings_get_user_bookings() function but it doesnt work. The user is still able to book as many times (in this case 20) as they want for the same timeslot. Here is my function: add_filter('woocommerce_bookings_get_time_slots', 'restrict_user_to_one_booking_per_slot', 10, 4); function restrict_user_to_one_booking_per_slot($available_time_slots, $from, $to, $bookable_product) { // Get the current user ID $user_id = get_current_user_id(); // If the user is not logged in, return the available slots as is if (!$user_id) { return $available_time_slots; } // Get all bookings for the current user $user_bookings = wc_bookings_get_user_bookings($user_id); // Loop through each of the user's bookings foreach ($user_bookings as $user_booking) { // Check if the booking is for the same product as the one being viewed if ($user_booking->get_product_id() == $bookable_product->get_id()) { // Get the start and end time of the existing booking $booking_start_time = $user_booking->get_start_date(); $booking_end_time = $user_booking->get_end_date(); // Loop through the available time slots and unset the ones that overlap foreach ($available_time_slots as $key => $slot) { // Check if the slot is within the range of the existing booking if ($slot['from'] >= $booking_start_time && $slot['from'] <= $booking_end_time) { unset($available_time_slots[$key]); } } } } return $available_time_slots; } EDIT: * @param int $user_id User ID. * @return array Array of bookings. function get_user_bookings($user_id) { if (!class_exists('WC_Bookings')) { return []; // WooCommerce Bookings plugin is not active. } $args = [ 'post_type' => 'wc_booking', 'post_status' => ['confirmed', 'pending', 'cancelled', 'completed'], // Adjust statuses as needed. 'posts_per_page' => -1, // Get all bookings. 'meta_query' => [ [ 'key' => '_booking_customer_id', 'value' => $user_id, 'compare' => '=' ] ] ]; $bookings = new WP_Query($args); $results = []; if ($bookings->have_posts()) { while ($bookings->have_posts()) { $bookings->the_post(); $results[] = [ 'id' => get_the_ID(), 'title' => get_the_title(), 'start_date' => (int) get_post_meta(get_the_ID(), '_booking_start', true), 'end_date' => (int) get_post_meta(get_the_ID(), '_booking_end', true), 'status' => get_post_meta(get_the_ID(), '_booking_status', true), 'customer_id' => get_post_meta(get_the_ID(), '_booking_customer_id', true), ]; } wp_reset_postdata(); } return $results; } add_filter('woocommerce_bookings_get_time_slots', 'restrict_user_to_one_booking_per_slot', 10, 4); function restrict_user_to_one_booking_per_slot($available_time_slots, $from, $to, $bookable_product) { $user_id = get_current_user_id(); if (!$user_id) { return $available_time_slots; } $user_bookings = get_user_bookings($user_id); foreach ($user_bookings as $user_booking) { $booking_start_time = $user_booking['start_date']; $booking_end_time = $user_booking['end_date']; foreach ($available_time_slots as $key => $slot) { $slot_start = (int) $slot['from']; $slot_end = (int) $slot['to']; if ($slot_start < $booking_end_time && $slot_end > $booking_start_time) { unset($available_time_slots[$key]); // Remove the overlapping slot } } } return array_values($available_time_slots); }

Comment (0)

You’ll be in good company