Created
March 18, 2025 11:03
-
-
Save thisissandip/75ba757fe81c959becfffc0175219be1 to your computer and use it in GitHub Desktop.
WooCommerce Bookings: Automatically Block Other Resources When One is Booked
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
/* | |
This snippet ensures that when a booking is paid for a booking product with multiple resources, | |
all other linked resources are automatically blocked for the same time slot. It dynamically adds | |
an availability rule within the booking product blocking the slot, preventing other resources bookings for that slot. | |
*/ | |
function block_booking_slot_to_prevent_other_resource_bookings_on_payment($booking_id) { | |
$booking = new WC_Booking($booking_id); | |
$product_id = $booking->get_product_id(); | |
$start_time = $booking->get_start(); | |
$end_time = $booking->get_end(); | |
if ($product_id && $start_time && $end_time) { | |
$existing_availability = get_post_meta($product_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 = $start_time ? date('H:i', $start_time) : '00:00'; | |
$to_time = $end_time ? date('H:i', $end_time) : '00:00'; | |
$is_full_day = ($from_time === '00:00' && $to_time === '23:59'); | |
// Set blocking rule format | |
$availability_rule = [ | |
'bookable' => 'no', | |
'priority' => 1, // Lowest priority to override every other rule | |
]; | |
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($product_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-availability-logger']; | |
// Log message format | |
if ($is_full_day) { | |
$log_message = sprintf( | |
'Booking ID %d confirmed: Blocked booking slot for Product ID %d from %s (Full day) to %s (Full day) to prevent booking of other resources in the product.', | |
$booking_id, | |
$product_id, | |
$from_date, | |
$to_date | |
); | |
} else { | |
$log_message = sprintf( | |
'Booking ID %d confirmed: Blocked booking slot for Product ID %d from %s %s to %s %s to prevent booking of other resources in the product.', | |
$booking_id, | |
$product_id, | |
$from_date, $from_time, | |
$to_date, $to_time | |
); | |
} | |
$logger->info($log_message, $context); | |
} | |
} | |
} | |
add_action('woocommerce_booking_in-cart_to_paid', 'block_booking_slot_to_prevent_other_resource_bookings_on_payment'); | |
add_action('woocommerce_booking_unpaid_to_paid', 'block_booking_slot_to_prevent_other_resource_bookings_on_payment'); | |
add_action('woocommerce_booking_confirmed_to_paid', 'block_booking_slot_to_prevent_other_resource_bookings_on_payment'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment