Reputation: 334
I'm trying to figure out how can I get all booking dates for a specific product - but it seems to be harder than one should think (or maybe it's because I don't know what I'm doing).
I used a couple of hours on it so far, and I'm unable to get anywhere, so far I have tried:
$WCBooking = new WC_Product_Booking('booking');
$WCBooking -> get_bookings_in_date_range($dToday, $dOneYear)
$WCBooking -> get_all_resources_availability($dToday, $dOneYear, $iProductID)
just returns an empty array,
also tried to query all booking posts, but I can't find their start and end date information on them :(
$aBookings = new WP_Query(
array(
'post_type' => 'wc_booking',
'posts_per_page' => -1 )
);
What should I try next? I figured it shouldn't be that hard, but I'm not sure where I am going wrong
Upvotes: 1
Views: 3187
Reputation: 21
As of WooCommerce 3.0:
$booking_ids = WC_Bookings_Controller::get_bookings_in_date_range($submitStartTime, $submitEndTime, $product_id, true);
The timestamps are epoch so strtotime may be very helpful. It certainly was in my case of extra validation to check that no bookings or in-cart bookings exist before allowing someone else to add to cart.
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;
}
Upvotes: 2
Reputation: 334
$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']
did the trick, the thing is they saved the post meta as YYYYmmddhhmmss, so just had to split it up first :(
Upvotes: 2