Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Add a buffer period to a Woocommece Bookable products only if there are existing bookings

$
0
0

I'm using Woocommerce Bookings and if I add a buffer to a product using the admin UI it 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 since I've set products up so that customers can choose a six hour block starting at the top of an hour. Using the admin UI, it creates a buffer so that instead of being able to book a product at 7am, 8am, 9am, etc you can only book products (if the buffer is set for 2 hours, say) at 7am, 9am, 11am, etc.

What I'm try to do is to add a buffer to a 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. However, I don't want the buffer to affect available start times if there are no bookings for that product on that day

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 (or at least if it is, that is not reflected in the Start time dropdown in the UI), 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...

<?phpadd_action('woocommerce_before_single_product', 'custom_set_buffer_period_for_viewed_product');function custom_set_buffer_period_for_viewed_product() {// Ensure WooCommerce functions are availableif (function_exists('is_product') && is_product()) { global $product; $product_id = $product->get_id(); error_log("Product ID $product_id"); $buffer_before = 0; $buffer_after = 120; 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); }}}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>