Hi guys, this plugin is not compatible with Booking Check In restriction days or "WooCommerce Accommodation Bookings" plugin, so I hired a developer to make this tool compatible with those.
I share with you the code:
// Search filter fix for Check In restriction days & WooCommerce Accommodation Bookings
add_action( 'elementor/query/alquil_custom_bookings_filter', function( $query ) {
if( isset( $_GET['start_date'] ) && isset( $_GET['end_date']) ) {
if ($_GET['start_date'] != '' && $_GET['end_date'] != '') {
//$start_date = str_replace('/', '-',$_GET['start_date']);
// $end_date = str_replace('/', '-',$_GET['end_date']);
if ( 'd/m/Y' == get_option( 'date_format', true ) ) {
$start_date = gmdate( 'Y-m-d', strtotime( str_replace( '/', '-', $_GET['start_date'] ) ) );
$end_date = gmdate( 'Y-m-d', strtotime( str_replace( '/', '-', $_GET['end_date'] ) ) );
}
else{
$start_date = gmdate( 'Y-m-d', strtotime($_GET['start_date'] ) );
$end_date = gmdate( 'Y-m-d', strtotime( $_GET['end_date'] ) );
}
$day_of_the_week = date('w', strtotime($start_date));
$num_days = (strtotime($end_date) - strtotime($start_date)) / (60 * 60 * 24);
$meta_query[] = $query->get( 'meta_query');
$meta_query[] = [
'key' => '_wc_booking_min_duration',
'value' => $num_days,
'compare' => '<=',
'type' => 'NUMERIC'
];
$meta_query[] = [
'key' => '_wc_booking_max_duration',
'value' => $num_days,
'compare' => '>=',
'type' => 'NUMERIC'
];
$meta_query[] = array (
'relation' => 'OR',
array (
'key' => '_wc_booking_restricted_days',
'value' => '"'.$day_of_the_week.'"',
'compare' => 'LIKE'
),
array (
'key' => '_wc_booking_has_restricted_days',
'value' => '1',
'compare' => '!='
),
);
$query->set( 'meta_query', $meta_query );
}
}
} );
Also, I share a function to include the total cost of the Booking the user is searching for:
// Add the total cost of the search in the results
function alq_search_item_totals($post) {
global $post;
$booking_total_cost_html = '';
if( isset( $_GET['start_date'] ) && isset( $_GET['end_date']) ) {
if ( 'd/m/Y' == get_option( 'date_format', true ) ) {
$start_date = gmdate( 'Y-m-d', strtotime( str_replace( '/', '-', $_GET['start_date'] ) ) );
$end_date = gmdate( 'Y-m-d', strtotime( str_replace( '/', '-', $_GET['end_date'] ) ) );
}
else{
$start_date = gmdate( 'Y-m-d', strtotime($_GET['start_date'] ) );
$end_date = gmdate( 'Y-m-d', strtotime( $_GET['end_date'] ) );
}
$product = wc_get_product($post->ID);
$booking_data = array();
$booking_data['_persons'] = 1;
$booking_data['_start_date'] = strtotime($start_date.' 12:00');
$booking_data['_end_date'] = strtotime($end_date);
$booking_data['date'] = strtotime($start_date);
//$booking_data['_resource_id'] ='';
//$booking_data['_qty'] = 1;
$booking_data['_duration'] = (strtotime($end_date) - strtotime($start_date)) / (60 * 60 * 24);
if (class_exists( 'WC_Bookings_Cost_Calculation')){
$cost = WC_Bookings_Cost_Calculation::calculate_booking_cost( $booking_data, $product );
if ($cost && ! is_wp_error( $cost )) {
$booking_cost = $cost;
$booking_cost = apply_filters( 'formatted_woocommerce_price', number_format( $booking_cost, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator()), $booking_cost, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator());
$formatted_price = sprintf( get_woocommerce_price_format(), '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol('') . '</span>', $booking_cost );
$booking_total_cost_html .= '<div class="results-Totals"><span>Total: </span>'.$formatted_price.'</div>';
}
}
}
return $booking_total_cost_html;
}
add_shortcode('search_item_totals', 'alq_search_item_totals');
Cheers.