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

如何配置Woocommerce Bookings资源分组:预订7人制场地锁定2个5人制场地

How to Set Up Resource Dependency Rules for WooCommerce Bookings (Futsal Venues)

Great question! WooCommerce Bookings doesn't have a built-in visual setting to link resource availability like this, but we can achieve exactly what you need with a bit of custom code. Let's break this down step by step:

1. Grab Your Resource IDs First

First, you'll need the unique IDs for your three venues:

  • Go to Products > Edit your "Futsal" booking product > Resources tab
  • Hover over each resource name (7-a-side, 5-a-side 1, 5-a-side 2) — your browser's status bar will show a URL with post=XXX; that number is the resource ID. Jot them down:
    • 7-a-side venue ID (e.g., 100)
    • 5-a-side venue 1 ID (e.g., 101)
    • 5-a-side venue 2 ID (e.g., 102)

2. Add Custom Code to Your Site

You can add this code to your child theme's functions.php file, or use the Code Snippets plugin (safer, since it won't get overwritten when you update your theme):

// Lock 5-a-side venues when 7-a-side is booked
function lock_five_a_side_when_seven_booked( $availability, $resource_id, $args ) {
    // Replace these IDs with your actual resource IDs
    $seven_a_side_id = 100;
    $five_a_side_ids = array(101, 102);

    // Check if we're checking availability for a 5-a-side venue
    if ( in_array( $resource_id, $five_a_side_ids ) ) {
        // Get the requested booking time window
        $start_date = $args['start'];
        $end_date = $args['end'];

        // Look for existing confirmed/paid/pending bookings for the 7-a-side venue in this window
        $existing_bookings = get_posts( array(
            'post_type'      => 'wc_booking',
            'post_status'    => array( 'paid', 'confirmed', 'pending' ),
            'meta_query'     => array(
                array(
                    'key'     => '_booking_resource_id',
                    'value'   => $seven_a_side_id,
                    'compare' => '=',
                ),
                array(
                    'key'     => '_booking_start',
                    'value'   => $end_date->getTimestamp(),
                    'compare' => '<',
                ),
                array(
                    'key'     => '_booking_end',
                    'value'   => $start_date->getTimestamp(),
                    'compare' => '>',
                ),
            ),
        ) );

        // If there's an active 7-a-side booking, mark the 5-a-side venue as unavailable
        if ( ! empty( $existing_bookings ) ) {
            $availability['available'] = false;
            $availability['message'] = __( 'This 5-a-side venue is unavailable because the 7-a-side venue has been booked for this time slot.', 'woocommerce-bookings' );
        }
    }

    return $availability;
}
add_filter( 'woocommerce_booking_get_availability', 'lock_five_a_side_when_seven_booked', 10, 3 );

3. How This Code Works

  • We use the woocommerce_booking_get_availability filter to intercept availability checks for your 5-a-side venues.
  • For each check, we look for any active bookings (paid, confirmed, or pending) for the 7-a-side venue in the same time window.
  • If a conflicting booking exists, we mark the 5-a-side venue as unavailable and show a clear message to users.

4. Optional: Reverse Lock (Lock 7-a-Side When 5-a-Side is Booked)

If you also want to prevent booking the 7-a-side venue when either 5-a-side venue is booked, add this additional code:

// Lock 7-a-side venue when any 5-a-side venue is booked (optional)
function lock_seven_a_side_when_five_booked( $availability, $resource_id, $args ) {
    $seven_a_side_id = 100;
    $five_a_side_ids = array(101, 102);

    if ( $resource_id == $seven_a_side_id ) {
        $start_date = $args['start'];
        $end_date = $args['end'];

        $existing_bookings = get_posts( array(
            'post_type'      => 'wc_booking',
            'post_status'    => array( 'paid', 'confirmed', 'pending' ),
            'meta_query'     => array(
                array(
                    'key'     => '_booking_resource_id',
                    'value'   => $five_a_side_ids,
                    'compare' => 'IN',
                ),
                array(
                    'key'     => '_booking_start',
                    'value'   => $end_date->getTimestamp(),
                    'compare' => '<',
                ),
                array(
                    'key'     => '_booking_end',
                    'value'   => $start_date->getTimestamp(),
                    'compare' => '>',
                ),
            ),
        ) );

        if ( ! empty( $existing_bookings ) ) {
            $availability['available'] = false;
            $availability['message'] = __( 'The 7-a-side venue is unavailable because a 5-a-side venue has been booked for this time slot.', 'woocommerce-bookings' );
        }
    }

    return $availability;
}
add_filter( 'woocommerce_booking_get_availability', 'lock_seven_a_side_when_five_booked', 10, 3 );

5. Test Thoroughly

After adding the code, make sure to test the flow:

  1. Book the 7-a-side venue for a specific time slot.
  2. Try booking either 5-a-side venue for the same slot — it should show as unavailable with your custom message.
  3. Cancel the 7-a-side booking, and the 5-a-side venues should become available again.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:39:06