Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ChobPT/fd0dd3c62d375d5814dad8e2ea68cc82 to your computer and use it in GitHub Desktop.

Select an option

Save ChobPT/fd0dd3c62d375d5814dad8e2ea68cc82 to your computer and use it in GitHub Desktop.
WooCommerce Bookings Availability Search Shortcode
<?php
/*
* This code uses the original function and
* adds a shortcode functionality to display the product loop with
* the products that are available adding also another shortcode for
* products that are unavailable
*
* Usage: [bookable_products start="2019-10-10" end="2019-10-15"]
* [unbookable_products start="2019-10-10" end="2019-10-15"]
*
* This was meant to use with Accomodation bookings, which work in day blocks.
* Modification may be needed to search for hour blocks
*
* Add this code to the functions.php file of your theme folder
*
*/
function checkBookableRooms($bookStart,$bookEnd){
/**
* 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'],
);
return $args;
}
function getProducts( $atts ) {
$startDate = date(strtotime($atts["start"]),"Y-m-d")." 00:00:00";
$endDate = date(strtotime($atts["end"]) ,"Y-m-d")." 23:59:59";
$wpArgs=checkBookableRooms($startDate,$endDate);
$latest = new WP_Query($wpArgs);
$post_ids = wp_list_pluck( $latest->posts, 'ID' );
return do_shortcode("[products ids='".implode(",", $post_ids)."']");
}
add_shortcode( 'bookable_products', 'getProducts' );
function checkUnBookableRooms($bookStart,$bookEnd){
/**
* 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__in' => $exclude,
'post_type' => ['product'],
'post_status' => ['published'],
);
return $args;
}
function getExcludedProducts( $atts ) {
$startDate = date(strtotime($atts["start"]),"Y-m-d")." 00:00:00";
$endDate = date(strtotime($atts["end"]) ,"Y-m-d")." 23:59:59";
$wpArgs=checkUnBookableRooms($startDate,$endDate);
$latest = new WP_Query($wpArgs);
$post_ids = wp_list_pluck( $latest->posts, 'ID' );
return do_shortcode("[products ids='".implode(",", $post_ids)."']");
}
add_shortcode( 'unbookable_products', 'getExcludedProducts' );
@apintocr
Copy link
Copy Markdown

apintocr commented Oct 9, 2019

Nice work.
Thanks for sharing.

@Ahigh4life
Copy link
Copy Markdown

How do I use it

@mikelaguiriano
Copy link
Copy Markdown

Hi I am using Easy Booking plugin on my multivendor website and i want to check if the product is available in the selected dates. How can i add this code?

@ChobPT
Copy link
Copy Markdown
Author

ChobPT commented Nov 13, 2019

Hi, just paste this code on the end of the functions.php file on your theme folder and then use the shortcode.

@Zerael-K
Copy link
Copy Markdown

Zerael-K commented Jan 4, 2020

Hi, I tried to use the code and I got error: date() expects parameter 2 to be int. referring to this part:
$startDate = date(strtotime($atts["start"]),"Y-m-d");
$endDate = date(strtotime($atts["end"]) ,"Y-m-d");
Can you help me fix it?

@ChobPT
Copy link
Copy Markdown
Author

ChobPT commented Jan 4, 2020

Oh wow, didn't notice that. I'll try to fix it ASAP.

The fix is simple, puth the "Y-m-d" BEFORE the comma and the strtotime bit after the comma.

Cheers

@alej1286
Copy link
Copy Markdown

alej1286 commented Jan 7, 2020

Wow, i appreciate the gift, it's really really helpful, i am new in world of wordpress, is there a way to put in de custom HTML a date picker for automate the search or the availability of the bookable products?

@somlaweb
Copy link
Copy Markdown

somlaweb commented Apr 1, 2020

Hi,

I thought that this code was to generate a Search Form for bookable products (Woocommerce Booking plugin) but it does not work for me. It shows me a loop of booked/unbooked Products.

Am I in the right place? I need the code to generate the Search Form.
Anyone know where is the code I need?

Thanks in advance.

Copy link
Copy Markdown

ghost commented Apr 12, 2021

Does the latest version still support this code? The shortcode is outputting products but a lot more than expected, for example I've tested it for a range where only one should display and we have 8 showing with this.

@hamadUNB
Copy link
Copy Markdown

hamadUNB commented Jun 1, 2023

Saved me a right nightmare of trying to find the relevant code in their plugin. Only update needed now is changing "WC_Bookings_Controller" to "WC_Booking_Data_Store"

@Sirineidriss
Copy link
Copy Markdown

How to use code in home page?
Thanks,

@vighneshbkl
Copy link
Copy Markdown

can we know the shortcode for this?

@ChobPT
Copy link
Copy Markdown
Author

ChobPT commented Nov 17, 2025

The usage instructions are within the first lines of the code

@vighneshbkl
Copy link
Copy Markdown

The usage instructions are within the first lines of the code

sorry actually i added this code to my theme function but i am getting error critical error notice :) is am doing anything wrong?

@apintocr
Copy link
Copy Markdown

The usage instructions are within the first lines of the code

sorry actually i added this code to my theme function but i am getting error critical error notice :) is am doing anything wrong?

It's been a while since I updated this snippet and right now I'm not using bookings in any project.

Can you let me know the error logs so I may look at it?

Thanks

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