Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Bookings Availability Search
<?php
/**
* WooCommerce Bookings Availability Search
*
* This is almost pseudo code, it only serves to explain the "how to do it" and does not attempt to be "The Way" to do it.
* NOTE: This NEEDS to be refined in order to work as expected.
*
* @author António Pinto <apinto@vanguardly.com>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
* @var string $bookStart (example: '2016-06-14 16:23:00')
* @var string $bookEnd (example: '2016-06-14 16:23:00')
*/
// Creating DateTime() objects from the input data.
$dateTimeStart = new DateTime($bookStart);
$dateTimeEnd = new DateTime($bookEnd);
// Get all Bookings in Range
$bookings = WC_Bookings_Controller::get_bookings_in_date_range(
$dateTimeStart->getTimestamp(),
$dateTimeEnd->getTimestamp(),
'',
false
);
// Build Array of all the Booked Products for the given Date-Time interval.
$exclude[] = 0;
foreach ($bookings as $booking) {
$exclude[] = $booking->product_id;
}
// Do a regular WP_Query using 'post__not_in' with the previous array.
$args = array (
'post__not_in' => $exclude,
'post_type' => ['product'],
'post_status' => ['published'],
);
@matthewduhig
Copy link

matthewduhig commented Oct 4, 2016

Thanks for publishing this... it just really helped on a project of mine :)

@luongkho
Copy link

luongkho commented Jul 11, 2017

Thanks for your sharing :))

@walidnafeti
Copy link

walidnafeti commented Jan 26, 2018

Thank you for this interesting plugin, could you please help how to improve it with adding more options like type of room, number of persons, type of accomodation, ... thank you in advance for help

@jeremylangerpersonal
Copy link

jeremylangerpersonal commented Jun 11, 2018

Thank you for this! Works great.

@hamid2628
Copy link

hamid2628 commented Jun 27, 2018

sorry , where should we paste this code ?

@cliffordp
Copy link

cliffordp commented Nov 11, 2018

This snippet was last revised Jun 14, 2016, so maybe it was correct at that time, but I'd guess this didn't really work back then either, mostly because of the simplistic WP_Query args, as they don't account for the primary objective of an availability search: get the booking products that have availability from start to end

@apintocr
Copy link
Author

apintocr commented Nov 21, 2018

@clifford
Thanks for your feedback.
Please understand that this gist was always intended to serve as a example, like I stated on the comments it is almost pseudo-code.

It did worked fine, it still works, however it was a specific project where this was enough.

You are correct as this does not really do what it should, sadly it is not as simple as it looks to accommodate for all cases (if it where WCBookings would have a function for it ;) ).

Do you have any tips for improvement?
I'd really like to improve it.

@tossiddu
Copy link

tossiddu commented Nov 26, 2018

Hi,
i really apreciate your work.
could you just explain how it should be used?
thanks

@ChobPT
Copy link

ChobPT commented Oct 9, 2019

For your convenience, I've converted the above into an easy to use shortcode.

https://gist.github.com/ChobPT/fd0dd3c62d375d5814dad8e2ea68cc82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment