首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce预订-创建预订并使":get_bookings_in_date_range“识别它

Woocommerce预订-创建预订并使":get_bookings_in_date_range“识别它
EN

Stack Overflow用户
提问于 2018-01-24 18:29:57
回答 1查看 816关注 0票数 0

我的目标是创建一个具有“车内”状态的预订。下面的代码运行良好,创建了具有正确数据的预订。但是当我在那个时间范围内检查预订时,创建的预订并不存在.我认为这与"get_bookings_in_date_range()“的兑现有关。如果是的话,我该如何结清现金?

代码语言:javascript
复制
//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); 
EN

回答 1

Stack Overflow用户

发布于 2018-07-09 01:08:23

"_booking_end“和"_booking_start”属性的存储值应该是Unix时间戳。在分配这些值时,您使用的是函数日期(),它反过来:它将时间戳转换为该日期的可读的字符串。

正因为如此,当期望值应该是"1531097445“这样的时间戳时,您可能会存储一个类似于"2018-07-09 00:52”的字符串。从而使WC_Bookings_Controller::get_bookings_in_date_range().方法难以理解。

假设"$date .$availability‘’from“和"$date .$availability‘to”是有效值,因为在发布的代码中没有引用它们,请尝试以这种方式更新"_booking_end“和"_booking_start”:

代码语言:javascript
复制
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类还具有以下方法来设置这些属性:

代码语言:javascript
复制
/**
 * 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 ),它将检索"false“,如果没有提供的ID的现有预订。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48429326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档