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…
<?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);
}
}
}
clev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.