Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Created April 10, 2025 08:28
Show Gist options
  • Save Crocoblock/f098047c348aa83a23f1d37d1619e455 to your computer and use it in GitHub Desktop.
Save Crocoblock/f098047c348aa83a23f1d37d1619e455 to your computer and use it in GitHub Desktop.
JetBooking - Price Filter Based on Selected Date Range
<?php
add_filter( 'jet-smart-filters/query/final-query', function( $query ) {
if ( empty( $query['meta_query'] ) ) {
return $query;
}
foreach ( $query['meta_query'] as $index => $meta_query ) {
if ( isset( $meta_query['key'] ) && '_seasonal_price' === $meta_query['key'] ) { // `_seasonal_price` unique key from Query Variable filter option.
$excluded = jet_abaf_get_excluded_apartments( $meta_query['value'] );
unset( $query['meta_query'][ $index ] );
if ( isset( $query['post__not_in'] ) ) {
$query['post__not_in'] = array_merge( $query['post__not_in'], $excluded );
} else {
$query['post__not_in'] = $excluded;
}
}
}
return $query;
}, 1, 20 );
function jet_abaf_get_excluded_apartments( $range_value ) {
if ( empty( $range_value ) ) {
return [];
}
$store_type = jet_abaf()->settings->get( 'filters_store_type' );
$searched_dates = jet_abaf()->stores->get_store( $store_type )->get( 'searched_dates' );
if ( ! trim( $searched_dates ) ) {
$dates = [
strtotime( 'today' ) + 1,
strtotime( 'today' ) + 12 * HOUR_IN_SECONDS,
];
} else {
$dates = explode( ' - ', $searched_dates );
}
$posts = jet_abaf()->tools->get_booking_posts();
if ( empty( $posts ) ) {
return [];
}
$excluded_posts = [];
foreach ( $posts as $post ) {
$period = jet_abaf()->tools->get_booking_period( $dates[0], $dates[1], $post->ID );
$interval = jet_abaf()->tools->get_booking_period_interval( $dates[0], $dates[1], $post->ID );
$price = new \JET_ABAF\Price( $post->ID );
$seasonal_prices = $price->seasonal_price->get_price();
$period_prices = [];
foreach ( $period as $day ) {
$pricing = [
'price' => $price->get_default_price(),
'price_rates' => $price->rates_price->get_rates(),
'weekend_price' => $price->weekend_price->get_price(),
];
if ( ! empty( $seasonal_prices ) ) {
foreach ( $seasonal_prices as $seasonal_price ) {
if ( $day->getTimestamp() >= $seasonal_price['start'] && $day->getTimestamp() <= $seasonal_price['end'] ) {
$pricing = $seasonal_price;
}
}
}
$weekend_price = $pricing['weekend_price'][ $day->format( 'w' ) ] ?? [];
$day_price = ! empty( $weekend_price ) ? $weekend_price : $pricing['price'];
if ( ! empty( $pricing['price_rates'] ) ) {
foreach ( $pricing['price_rates'] as $price_rate ) {
if ( $interval->days >= intval( $price_rate['duration'] ) ) {
$day_price = $price_rate['value'];
}
}
}
$period_prices[] = floatval( $day_price );
}
if ( min( $period_prices ) >= $range_value[0] && min( $period_prices ) <= $range_value[1] ) {
continue;
}
$excluded_posts[] = $post->ID;
}
return $excluded_posts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment