0

My goal is to create a booking with the status "in-cart". The code below works fine, a booking with the right data gets created. But when i check for bookings in that time-range, the created booking doesnt exist. I think it has to do with cashing of "get_bookings_in_date_range()". If so, how do i clear the cash?

//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); 
Jan
  • 81
  • 2
  • 5

1 Answers1

0

The stored values of "_booking_end" and "_booking_start" properties are expected to be Unix timestamps. When assigning these values, you're using the function date() , which goes the other way round: it converts a timestamp into a human-readable string of that date.

Because of that, you might be storing a string like "2018-07-09 00:52" when the expected value should be a timestamp such as "1531097445". Thus making it incomprehensible for the method WC_Bookings_Controller::get_bookings_in_date_range().

Assuming "$date . $availability[0][0]['from']" and "$date . $availability[0][0]['to']" are valid values because there's no reference to them on the posted code, try updating "_booking_end" and "_booking_start" this way:

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 class also has these methods to set those properties:

/**
 * 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 ) );
}

To get an instance of WC_Booking, simply use get_wc_booking( $booking_id ) which will retrieve "false" if there isn't any existing booking with the provided ID.