You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为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:

方法1:使用WooCommerce Bookings过滤器(推荐)

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 * 3600 condition 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.
方法2:Custom Template Override

If you prefer working with template files, you can override the WooCommerce Bookings slot template:

  1. Locate the original template file at wp-content/plugins/woocommerce-bookings/templates/booking-form/slot.php
  2. Copy this file to your theme directory under woocommerce/booking-form/ (create the folders if they don't exist)
  3. Open the copied slot.php file, find the line that displays the start time (usually echo 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 06:52:43