1

我试图弄清楚如何才能获得特定产品的所有预订日期 - 但这似乎比人们想象的要难(或者可能是因为我不知道我在做什么)。

到目前为止,我用了几个小时,但我无法到达任何地方,到目前为止我已经尝试过:

$WCBooking = new WC_Product_Booking('booking');
$WCBooking -> get_bookings_in_date_range($dToday, $dOneYear)
$WCBooking -> get_all_resources_availability($dToday, $dOneYear, $iProductID)

只返回一个空数组,

还尝试查询所有预订帖子,但我找不到他们的开始和结束日期信息:(

    $aBookings = new WP_Query( 
    array( 
        'post_type' => 'wc_booking', 
        'posts_per_page' => -1 )
    );

接下来我应该尝试什么?我认为这不应该那么难,但我不确定我哪里出错了

4

2 回答 2

2

从 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;
}
于 2017-05-20T03:14:14.890 回答
2
$aBookings = new WP_Query( 
array( 
    'post_type' => 'wc_booking', 
    'posts_per_page' => -1 )
);   

$oCustomPostMeta = get_post_custom($oPost->ID);
$oCustomPostMeta['_booking_start']
$oCustomPostMeta['_booking_end']

成功了,问题是他们将帖子元保存为 YYYYmmddhhmmss,所以只需要先将其拆分:(

于 2017-02-17T11:59:17.690 回答