-
-
Save ChobPT/fd0dd3c62d375d5814dad8e2ea68cc82 to your computer and use it in GitHub Desktop.
<?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' ); |
How do I use it
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?
Hi, just paste this code on the end of the functions.php file on your theme folder and then use the shortcode.
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?
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
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?
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.
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.
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"
How to use code in home page?
Thanks,
Nice work.
Thanks for sharing.