Last active
August 7, 2023 15:05
-
-
Save iamsathyaseelan/e1eae9e5b749cddc7fdf3fb259bb423d to your computer and use it in GitHub Desktop.
solving issue with woocommerce bookings and retainful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Retainful | |
*/ | |
add_action( 'wc_ajax_wc_bookings_find_booked_day_blocks', 'rnoc_find_booked_day_blocks',9); | |
function rnoc_find_booked_day_blocks() | |
{ | |
$product_id = absint($_GET['product_id']); | |
if (empty($product_id)) { | |
wp_send_json_error('Missing product ID'); | |
exit; | |
} | |
try { | |
$args = array(); | |
$product = get_wc_product_booking($product_id); | |
$args['availability_rules'] = array(); | |
$args['availability_rules'][0] = $product->get_availability_rules(); | |
$args['min_date'] = isset($_GET['min_date']) ? strtotime($_GET['min_date']) : $product->get_min_date(); | |
$args['max_date'] = isset($_GET['max_date']) ? strtotime($_GET['max_date']) : $product->get_max_date(); | |
$min_date = (!isset($_GET['min_date'])) ? strtotime("+{$args['min_date']['value']} {$args['min_date']['unit']}", current_time('timestamp')) : $args['min_date']; | |
$max_date = (!isset($_GET['max_date'])) ? strtotime("+{$args['max_date']['value']} {$args['max_date']['unit']}", current_time('timestamp')) : $args['max_date']; | |
$timezone_offset = isset($_GET['timezone_offset']) ? $_GET['timezone_offset'] : 0; | |
if ($product->has_resources()) { | |
foreach ($product->get_resources() as $resource) { | |
$args['availability_rules'][$resource->ID] = $product->get_availability_rules($resource->ID); | |
} | |
} | |
$booked = WC_Bookings_Controller::find_booked_day_blocks($product_id, $min_date, $max_date, 'Y-n-j', $timezone_offset); | |
$args['partially_booked_days'] = $booked['partially_booked_days']; | |
$args['fully_booked_days'] = $booked['fully_booked_days']; | |
$args['unavailable_days'] = $booked['unavailable_days']; | |
$args['restricted_days'] = $product->has_restricted_days() ? $product->get_restricted_days() : false; | |
$buffer_days = array(); | |
if (!in_array($product->get_duration_unit(), array('minute', 'hour'))) { | |
$buffer_days = WC_Bookings_Controller::get_buffer_day_blocks_for_booked_days($product, $args['fully_booked_days']); | |
} | |
$args['buffer_days'] = $buffer_days; | |
wp_send_json($args); | |
} catch (Exception $e) { | |
wp_die(); | |
} | |
} | |
add_action( 'wp_ajax_wc_bookings_get_end_time_html', 'rnoc_get_end_time_html',9); | |
add_action( 'wp_ajax_nopriv_wc_bookings_get_end_time_html', 'rnoc_get_end_time_html',9); | |
function rnoc_get_end_time_html() { | |
$start_date_time = wc_clean( $_POST['start_date_time'] ); | |
$product_id = intval( $_POST['product_id'] ); | |
$blocks = wc_clean( $_POST['blocks'] ); | |
$bookable_product = wc_get_product( $product_id ); | |
$booking_form = new WC_Booking_Form( $bookable_product ); | |
$html = $booking_form->get_end_time_html( $blocks, $start_date_time ); | |
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment