Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ChobPT/fd0dd3c62d375d5814dad8e2ea68cc82 to your computer and use it in GitHub Desktop.
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 <[email protected]>
* @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 <[email protected]>
* @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' );
@hamadUNB
Copy link

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

How to use code in home page?
Thanks,

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