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

求助修复WooCommerce Bookings中资源字段重置用户已选日期时间的问题

Fix: Resource Selection Resets Date/Time in WooCommerce Bookings

Got it, let's tackle this issue head-on. The core problem here is that WooCommerce Bookings is hardwired to expect users to pick a resource first (to filter available dates). When you moved the resource field after the calendar, changing the resource triggers a full reset of your date/time selection because the plugin assumes you're starting over. Since all your resources are always available, we can tweak the plugin's logic to play nice with your custom flow.

Step 1: Modify the Availability Check Logic

First, let's fix the backend function that's rejecting availability checks when no resource is selected yet. Open includes/wc-booking-functions.php and find the wc_bookings_get_total_available_bookings_for_range function. Look for this line:

if ( $bookable_product->has_resources() && ! is_numeric( $resource_id ) ) {
    return false;
}

This code returns false immediately if there are resources but none is selected—perfect for the default flow, but terrible for your use case. Replace it with logic that calculates combined availability across all resources (since they're all available):

if ( $bookable_product->has_resources() && ! is_numeric( $resource_id ) ) {
    // Since all resources are always available, return combined availability of all resources
    return $bookable_product->get_all_resources_availability( $start_date, $end_date, $qty );
}

Now, when a user selects a date/time first (without picking a resource), the plugin won't reject the check, and switching resources later won't invalidate their existing selection.

Step 2: Fix Time Slot Caching

Next, let's adjust the time slot caching so switching resources doesn't wipe out the cached slots. In the same file, find the wc_bookings_get_time_slots function. The transient name includes the resource ID, which means each resource gets its own cache (even though your resources have identical availability):

$transient_name = 'book_ts_' . md5( http_build_query( array( $bookable_product->get_id(), $resource_id, $from, $to, $include_sold_out ) ) );

Remove $resource_id from the transient key to share the same cache across all resources:

// Modified transient name (resource ID removed since all resources have same availability)
$transient_name = 'book_ts_' . md5( http_build_query( array( $bookable_product->get_id(), $from, $to, $include_sold_out ) ) );

This stops the plugin from reloading empty slots when a resource is selected, keeping the user's date/time picker intact.

Step 3: Override Frontend JS (Critical!)

The PHP fixes handle the backend, but you still need to stop the frontend from resetting the calendar when the resource changes. WooCommerce Bookings uses JS to listen for resource dropdown changes and trigger a full reset.

Add this custom JS to your child theme's functions.php or a custom plugin to override that behavior:

function disable_resource_calendar_reset() {
    if ( is_product() && class_exists( 'WC_Product_Booking' ) ) {
        $product = wc_get_product( get_the_ID() );
        if ( is_a( $product, 'WC_Product_Booking' ) && $product->has_resources() ) {
            ?>
            <script>
                jQuery(document).ready(function($) {
                    // Unbind the default resource change event that resets the calendar
                    $('select.wc_bookings_resource_select').off('change');
                    
                    // Add our custom event: update resource ID and refresh availability without resetting dates
                    $('select.wc_bookings_resource_select').on('change', function() {
                        var $resourceSelect = $(this);
                        var $bookingForm = $resourceSelect.closest('.wc-bookings-booking-form');
                        
                        // Update the hidden resource ID input
                        $bookingForm.find('input[name="resource_id"]').val($resourceSelect.val());
                        
                        // Refresh availability for the already selected date/time
                        if ( typeof wc_bookings_params !== 'undefined' ) {
                            $bookingForm.trigger('wc_bookings_refresh_availability');
                        }
                    });
                });
            </script>
            <?php
        }
    }
}
add_action( 'wp_footer', 'disable_resource_calendar_reset' );

This code unhooks the default reset behavior and instead just updates the selected resource ID and refreshes availability for the user's existing date/time choice.

Important Notes

  • Backup First: Always back up your site and plugin files before making changes—you don't want to break your booking system!
  • Avoid Core Plugin Edits: Modifying core WooCommerce Bookings files directly will lose your changes when the plugin updates. Instead, use a custom plugin or child theme to override these functions. For the PHP functions, you can wrap them in a plugin and use function_exists checks to replace the core versions.
  • Test Everything: Walk through the booking flow multiple times—make sure date/time stays selected when switching resources, and that availability is correctly calculated for all resources.

内容的提问来源于stack exchange,提问作者tff

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 15:08:11