Last active
July 19, 2025 06:02
-
-
Save thisissandip/b9c070e827ac4254e90fc7f1fdee9885 to your computer and use it in GitHub Desktop.
WooCommerce Bookings: Auto Add 1-Hour Buffer After Booking Ends for Resource Availability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Add a 1-hour buffer period to resource availability AFTER a paid booking ends by blocking availability | |
| from the booking's end time to 1 hour later. This prevents back-to-back bookings. | |
| */ | |
| function auto_create_one_hour_buffer_availability_resource($booking_id) { | |
| $booking = new WC_Booking($booking_id); | |
| $product_id = $booking->get_product_id(); | |
| $resource_id = $booking->get_resource_id(); | |
| $start_time = $booking->get_start(); | |
| $end_time = $booking->get_end(); | |
| if ($product_id && $resource_id && $start_time && $end_time) { | |
| $existing_availability = get_post_meta($resource_id, '_wc_booking_availability', true); | |
| if (!is_array($existing_availability)) { | |
| $existing_availability = []; | |
| } | |
| $from_date = date('Y-m-d', $start_time); | |
| $to_date = date('Y-m-d', $end_time); | |
| $from_time = $end_time ? date('H:i', $end_time) : '00:00'; | |
| $to_time = date('H:i', strtotime('+1 hour', $end_time)); | |
| $is_full_day = ($from_time === '00:00' && $to_time === '23:59'); | |
| // Set blocking rule format | |
| $availability_rule = [ | |
| 'bookable' => 'no', | |
| 'priority' => 6, // priority to override other rules | |
| ]; | |
| if ($is_full_day) { | |
| $availability_rule['type'] = 'custom'; // use the date range rule type | |
| $availability_rule['from'] = $from_date; | |
| $availability_rule['to'] = $to_date; | |
| } else { | |
| $availability_rule['type'] = 'custom:daterange'; // use the date-range with time rule type | |
| $availability_rule['from_date'] = $from_date; | |
| $availability_rule['to_date'] = $to_date; | |
| $availability_rule['from'] = $from_time; | |
| $availability_rule['to'] = $to_time; | |
| } | |
| // Add the rule | |
| $existing_availability[] = $availability_rule; | |
| // Update the booking product availability | |
| update_post_meta($resource_id, '_wc_booking_availability', $existing_availability); | |
| // Log the action in WooCommerce Logs | |
| if (function_exists('wc_get_logger')) { | |
| $logger = wc_get_logger(); | |
| $context = ['source' => 'bookings-resource-availability-logger']; | |
| // Log message format | |
| if ($is_full_day) { | |
| $log_message = sprintf( | |
| 'Booking ID %d confirmed: Added buffer to Resource ID %d from %s (Full day) to %s (Full day) to prevent booking.', | |
| $booking_id, | |
| $resource_id, | |
| $from_date, | |
| $to_date | |
| ); | |
| } else { | |
| $log_message = sprintf( | |
| 'Booking ID %d confirmed: Added buffer to Resource ID %d from %s %s to %s %s to prevent booking', | |
| $booking_id, | |
| $resource_id, | |
| $from_date, $from_time, | |
| $to_date, $to_time | |
| ); | |
| } | |
| $logger->info($log_message, $context); | |
| } | |
| } | |
| } | |
| add_action('woocommerce_booking_in-cart_to_paid', 'auto_create_one_hour_buffer_availability_resource'); | |
| add_action('woocommerce_booking_unpaid_to_paid', 'auto_create_one_hour_buffer_availability_resource'); | |
| add_action('woocommerce_booking_confirmed_to_paid', 'auto_create_one_hour_buffer_availability_resource'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment