Я создаю веб-сайт бронирования для своей студии йоги, используя WooCommerce-бронирование в Wordpress.
Я хотел бы ограничить каждого пользователя только одним бронированием в каждом временном интервале.Пример:
Наличие занятий йогой:
20 мест на занятие.
Понедельник: 9:00 и вторник: 10:00.
Если пользователь заказывает занятие в Mondat на 9 утра и пытается зарезервировать еще одно занятие йогой, в календаре должно быть доступно только время вторника на 10 утра.
Я попробовал используя функцию wc_bookings_get_user_bookings(), но она не работает. Пользователь по-прежнему может бронировать столько раз (в данном случае 20), сколько пожелает, на один и тот же временной интервал.
Вот моя функция:
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'] '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);
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... e-bookings
Ограничить пользователей одним бронированием за временной интервал в WooCommerce Bookings. ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Ограничить пользователей одним бронированием за временной интервал в WooCommerce Bookings.
Anonymous » » в форуме Php - 0 Ответы
- 8 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Ограничить пользователей одним бронированием за временной интервал в WooCommerce Bookings.
Anonymous » » в форуме Php - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-