关于woocommerce_booking_form_get_posted_data钩子修改预订时长的技术咨询
Great question! Let’s break down everything you need to know about the woocommerce_booking_form_get_posted_data hook for WooCommerce Bookings, step by step:
woocommerce_booking_form_get_posted_data Hook How It Works
This hook is a core filter in the WooCommerce Bookings plugin that lets you intercept and modify the raw POST data submitted when a user fills out a booking form (either adding a new booking to the cart or editing an existing one in the cart). It acts as a "middleware" between the user’s input and the plugin’s internal booking processing logic—any changes you make here will be passed directly to the system that calculates booking details like duration, dates, and pricing.
Trigger Timing
It fires at two key moments:
- When a user first adds a booking product to their cart (right after they submit the booking form, before the system validates or saves any booking data).
- When a user edits an existing booking in their cart (e.g., modifying the date or duration of a booking already in the cart—this edit action resubmits the booking form, triggering the hook again).
Absolutely—you can re-calculate and override the booking duration directly in this hook. Here’s a practical example that handles both new cart additions and edits to existing bookings:
add_filter( 'woocommerce_booking_form_get_posted_data', 'custom_adjust_booking_duration', 10, 2 ); function custom_adjust_booking_duration( $posted_data, $product ) { // Only target booking-type products if ( ! $product->is_type( 'booking' ) ) { return $posted_data; } // Check if we're editing an existing cart item $cart_item_key = isset( $_POST['cart_item_key'] ) ? $_POST['cart_item_key'] : ''; if ( ! empty( $cart_item_key ) ) { // Locate the existing booking in the cart foreach ( WC()->cart->get_cart() as $key => $cart_item ) { if ( $key === $cart_item_key ) { // Example: Add 2 hours to the existing duration $existing_duration = $cart_item['booking']['_duration']; $posted_data['duration'] = $existing_duration + 2; break; } } } else { // For new bookings: Force a minimum duration of 3 hours $posted_data['duration'] = max( $posted_data['duration'], 3 ); } // Bonus: Double duration for weekend bookings $start_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $posted_data['start_date'] ); if ( $start_date && in_array( $start_date->format( 'N' ), [6, 7] ) ) { $posted_data['duration'] *= 2; } return $posted_data; }
Key Details in This Example:
- We first check if the product is a booking type to avoid affecting regular products.
- For existing cart edits, we use the
cart_item_keyfrom the POST data to target the specific booking in the cart, then adjust its duration. - You can build any custom logic here: based on user role, cart contents, date ranges, or even external data.
Yes! Since this hook runs before the plugin processes booking pricing, your adjusted duration will automatically trigger a full price recalculation:
- If your booking product is priced by duration (e.g., $50/hour), the new duration will multiply correctly to update the line item total.
- The updated duration and price will reflect in the cart immediately, and will carry over to the final order details once the user checks out.
- Always test with
var_dump($posted_data)in a staging environment to confirm the exact keys used for duration (they can vary slightly depending on your booking settings: hourly vs. daily, resource-based bookings, etc.). - If your duration changes affect resource availability, pair this hook with
woocommerce_booking_get_availabilityto ensure the modified booking window is valid for your resources.
内容的提问来源于stack exchange,提问作者James

