我的目标是创建状态为“in-cart”的预订。下面的代码工作正常,创建了正确数据的预订。但是,当我检查该时间范围内的预订时,创建的预订不存在。我认为这与兑现“get_bookings_in_date_range()”有关。如果是这样,我如何清理现金?
//This works fine, it returns all the bookingids
$booking_ids = WC_Bookings_Controller::get_bookings_in_date_range($start_date, $end_date, $product_id, true);
//I use insert_post, because create_wc_booking doesnt accept the "in-cart" status
//It creates a booking with the right data
$cart_booking = array(
'post_type' => 'wc_booking',
'post_status' => 'in-cart',
'post_author' => 69,
);
$booking_id = wp_insert_post($cart_booking);
//Updating some data - works
update_post_meta($booking_id , "_booking_product_id", $product_id);
update_post_meta($booking_id , "_booking_start", date("Y-m-d H:i", strtotime($date . $availability[0][0]['from'])));
update_post_meta($booking_id , "_booking_end", date("Y-m-d H:i", strtotime($date . $availability[0][0]['to'])));
update_post_meta($booking_id , "_booking_persons", $personcount);
//Make booking expire after 60 Minutes - works
custom_schedule_cart_removal($booking_id)
//NOW, this booking exists in the backend but doesnt get recognized by the code below, even though it has the right meta-data
WC_Bookings_Controller::get_bookings_in_date_range($start_date, $end_date, $product_id, true);
答案 0 :(得分:0)
“ _ booking_end”和“ _booking_start”属性的存储值应为Unix时间戳。分配这些值时,您使用的是date()函数,它的作用相反:它将时间戳转换为该日期的人类可读字符串。
因此,当预期值应为时间戳记(例如“ 1531097445”)时,您可能会存储类似“ 2018-07-09 00:52”的字符串。因此使方法WC_Bookings_Controller :: get_bookings_in_date_range()变得难以理解。
假设“ $ date。$ availability [0] [0] ['from']”和“ $ date。$ availability [0] [0] ['to']”是有效值,因为没有引用它们在发布的代码上,尝试通过以下方式更新“ _booking_end”和“ _booking_start”:
update_post_meta($booking_id , "_booking_start", strtotime($date . $availability[0][0]['from']));
update_post_meta($booking_id , "_booking_end", strtotime($date . $availability[0][0]['to']));
WC_Booking类还具有以下方法来设置这些属性:
/**
* Set start_time.
*
* @param string $timestamp
* @throws WC_Data_Exception
*/
public function set_start( $timestamp ) {
$this->set_prop( 'start', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
}
/**
* Set end_time.
*
* @param string $timestamp
* @throws WC_Data_Exception
*/
public function set_end( $timestamp ) {
$this->set_prop( 'end', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
}
要获取WC_Booking的实例,只需使用 get_wc_booking($ booking_id),如果没有现有提供的ID的预订,它将检索“ false”。