2017-02-17 50 views
1

我想弄清楚如何獲得特定產品的所有預訂日期 - 但它似乎比人們想象的要難(或者是因爲我不知道自己在做什麼)。如何使用PHP獲取特定產品的所有預訂日期?

我用了幾個上就是幾個小時,到目前爲止,我無法取得任何進展,到目前爲止,我曾嘗試:

$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) 
    ); 

我應該嘗試下?我想它不應該是很難的,但我不知道我在哪裏出錯

+0

您能否提供您想要獲得的預訂的鏈接? –

回答

1
$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,所以纔不得不先將其分裂:(

1

隨着WooCommerce 3.0:

$booking_ids = WC_Bookings_Controller::get_bookings_in_date_range($submitStartTime, $submitEndTime, $product_id, true); 

時間戳是時代,所以時間可能會非常有幫助。在我允許其他人添加到購物車之前,確保沒有任何預訂或推車預訂存在的情況下,我確實需要額外驗證。

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; 
}