从 WooCommerce 3.0 开始:
$booking_ids = WC_Bookings_Controller::get_bookings_in_date_range($submitStartTime, $submitEndTime, $product_id, true);
时间戳是纪元,所以 strtotime 可能非常有用。在我允许其他人添加到购物车之前,检查是否存在预订或购物车内预订确实是在我的情况下进行的额外验证。
add_action( 'woocommerce_add_to_cart_validation', 'woo_add_to_cart_validation, 10, 5 );
function woo_add_to_cart_validation( $passed, $product_id ) {
//Build timestamp from the potential product values
$submitStartTime = strtotime( $_POST['wc_bookings_field_start_date_year'] . "-" . $_POST['wc_bookings_field_start_date_month'] . '-' . $_POST['wc_bookings_field_start_date_day'] . " " . $_POST['wc_bookings_field_start_date_time']);
//Compare with a 30 minute booking.
$submitEndTime = $submitStartTime + (60 * 30);
//Verify that no bookings exist (as orders or in cart)
$booking_ids = WC_Bookings_Controller::get_bookings_in_date_range($submitStartTime, $submitEndTime, $product_id, true);
if( $booking_ids ) {
wc_add_notice( __( 'The date/time you selected has already been booked. Please select another.', 'woocommerce-bookings' ), 'error' );
$passed = false;
}
return $passed;
}