如何为WordPress+WooCommerce Bookings房间租赁站点的固定时长预订时段添加结束时间显示?
Hey there! I've run into this exact scenario with WooCommerce Bookings before, so I can help you add the end time to those fixed-duration slots. Here are two reliable methods to make this happen:
WooCommerce Bookings provides the woocommerce_booking_form_slot_html filter, which lets you customize how booking slots are displayed. You can add this code to your theme's functions.php file or a custom functionality plugin:
add_filter( 'woocommerce_booking_form_slot_html', 'add_end_time_to_booking_slots', 10, 4 ); function add_end_time_to_booking_slots( $html, $slot, $product, $duration ) { // Target only rooms with fixed 4-hour bookings if ( $duration == 4 * 3600 ) { // Convert 4 hours to seconds // Parse the start time into a DateTime object $start_time = DateTime::createFromFormat( 'H:i', $slot->get_start_time() ); if ( $start_time ) { // Calculate end time by adding 4 hours $end_time = clone $start_time; $end_time->add( new DateInterval( 'PT4H' ) ); // Format end time to match the start time's H:i format $end_time_formatted = $end_time->format( 'H:i' ); // Update the slot text to include both start and end time $original_text = strip_tags( $html ); $new_text = $original_text . ' - ' . $end_time_formatted; $html = str_replace( $original_text, $new_text, $html ); } } return $html; }
Notes:
- This code checks if the slot duration is exactly 4 hours (14400 seconds) and adds the end time only for those slots. Adjust the
$duration == 4 * 3600condition if you have other fixed durations to support. - Double-check your WordPress timezone settings (under Settings > General) to ensure time calculations are accurate for your users.
If you prefer working with template files, you can override the WooCommerce Bookings slot template:
- Locate the original template file at
wp-content/plugins/woocommerce-bookings/templates/booking-form/slot.php - Copy this file to your theme directory under
woocommerce/booking-form/(create the folders if they don't exist) - Open the copied
slot.phpfile, find the line that displays the start time (usuallyecho esc_html( $slot->get_start_time() );), and replace it with this code:
<?php $start_time = $slot->get_start_time(); $duration = $product->get_duration(); // Calculate end time for 4-hour slots $start_dt = DateTime::createFromFormat( 'H:i', $start_time ); if ( $start_dt && $duration == 4 * 3600 ) { $end_dt = clone $start_dt; $end_dt->add( new DateInterval( 'PT' . ( $duration / 3600 ) . 'H' ) ); $end_time = $end_dt->format( 'H:i' ); echo esc_html( $start_time . ' - ' . $end_time ); } else { echo esc_html( $start_time ); } ?>
Tips:
- Always back up template files before modifying them—this prevents losing changes when the WooCommerce Bookings plugin updates.
- This method is better if you want to further customize the slot's HTML structure or styling.
Before deploying to your live site, test either method in a staging environment to make sure it plays nicely with your theme and other plugins.
内容的提问来源于stack exchange,提问作者Jakob Mayerhofer

