Woocommece Bookings: How To add a buffer period to a Booking product only if there are existing bookings

I’m using Woocommerce Bookings and the admin UI for this adds the buffer even if there are no bookings for that product on that day. That restricts the hours someone can choose which isn’t good. What I’m try to do is to add a buffer to that product when (and only when) there’s a booking for it.

Say a product has a booking for 8am to 12pm on June 2nd. I want someone to be able to reserve it but I want it to have a 2 hour buffer after the existing booking ends. So, if someone tried to reserve for June 2nd, they’d see availability starting at 2pm.

The code below works fine to get the product ID of the product someone is looking at and I get the bookings fine ($bookings returns valid info), but the filter is where it fails silently.It’s not altering the buffer, so the first available start time in my example is 12pm, i.e without any buffer at all. “wc_bookings_get_time_slots” doesn’t seem to be the thing I need but I can’t seem to find what is…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
add_action('woocommerce_before_single_product', 'custom_set_buffer_period_for_viewed_product');
function custom_set_buffer_period_for_viewed_product() {
// Ensure WooCommerce functions are available
if (function_exists('is_product') && is_product()) {
global $product;
// Get the current product ID
$product_id = $product->get_id();
error_log("Product ID $product_id");
// Set the product ID for which you want to apply the buffer period
$specific_product_id = 924; // Replace 123 with your product ID
error_log("Sepcific Product ID $specific_product_id");
// Initialize the buffer period in minutes
$buffer_before = 30;
$buffer_after = 3000;
// Check if the product matches the specific product ID
if ($product_id) {
// Check if there are existing bookings for the product
$bookings = WC_Bookings_Controller::get_bookings_for_objects(array($product_id));
error_log(print_r($bookings,true));
add_filter('wc_bookings_get_time_slots', function($booking, $booking_id) use ($buffer_before, $buffer_after) {
// Apply the buffer period if there are existing bookings
$booking->set_buffer_period($buffer_before, $buffer_after);
return $booking;
}, 10, 2);
}
}
}
</code>
<code><?php add_action('woocommerce_before_single_product', 'custom_set_buffer_period_for_viewed_product'); function custom_set_buffer_period_for_viewed_product() { // Ensure WooCommerce functions are available if (function_exists('is_product') && is_product()) { global $product; // Get the current product ID $product_id = $product->get_id(); error_log("Product ID $product_id"); // Set the product ID for which you want to apply the buffer period $specific_product_id = 924; // Replace 123 with your product ID error_log("Sepcific Product ID $specific_product_id"); // Initialize the buffer period in minutes $buffer_before = 30; $buffer_after = 3000; // Check if the product matches the specific product ID if ($product_id) { // Check if there are existing bookings for the product $bookings = WC_Bookings_Controller::get_bookings_for_objects(array($product_id)); error_log(print_r($bookings,true)); add_filter('wc_bookings_get_time_slots', function($booking, $booking_id) use ($buffer_before, $buffer_after) { // Apply the buffer period if there are existing bookings $booking->set_buffer_period($buffer_before, $buffer_after); return $booking; }, 10, 2); } } } </code>
<?php
add_action('woocommerce_before_single_product', 'custom_set_buffer_period_for_viewed_product');
function custom_set_buffer_period_for_viewed_product() {
// Ensure WooCommerce functions are available
if (function_exists('is_product') && is_product()) {
 global $product;
 
 // Get the current product ID
 $product_id = $product->get_id();
 error_log("Product ID $product_id");
 
 // Set the product ID for which you want to apply the buffer period
 $specific_product_id = 924; // Replace 123 with your product ID
 error_log("Sepcific Product ID $specific_product_id");

 // Initialize the buffer period in minutes
 $buffer_before = 30;
 $buffer_after = 3000;

 // Check if the product matches the specific product ID
 if ($product_id) {
     // Check if there are existing bookings for the product
     $bookings = WC_Bookings_Controller::get_bookings_for_objects(array($product_id));
     error_log(print_r($bookings,true));
             add_filter('wc_bookings_get_time_slots', function($booking, $booking_id) use ($buffer_before, $buffer_after) {
             // Apply the buffer period if there are existing bookings
             $booking->set_buffer_period($buffer_before, $buffer_after);
             return $booking;
             }, 10, 2);
 }
}
}

New contributor

clev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật