1

I'm trying to use the function check_availability_rules_against_time on WooCommerce Bookings to determine if a range of time is available for accomodation. As I understand, it should return true if the time slot checked against the rules is available. The problem I have is that it returns true if the start time is available, even if the slot goes beyond the availavility range.

For example, I have this ranges on a product:

These are the rules on a product to be tested

So I test it like this:

<?php

// Query Filter
$args = array(
  'post_type' => 'product',
  'posts_per_page' => 20,
  'tax_query' => array(
      array(
        'taxonomy'      => 'product_cat',
        'field'         => 'term_id',
        'terms'         => $hotel['cat_habitaciones'],
        'operator'      => 'IN'
      ))
   );

$loop = new WP_Query( $args );

if ( $loop->have_posts() ) {
  while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product;
    
    if( ! $product->has_resources() || count($product->get_resources()) == 0 ){
      continue;
    }

    foreach($product->get_resources() as $resource){
      echo (WC_Product_Booking_Rule_Manager::check_availability_rules_against_time('2020-04-01','2020-07-01',$resource->id,$product,true)) ? 'Available' : 'Not Available';
}
?>
...html...
<?php
  endwhile;
} else {
  echo __( 'No se encontraron habitaciones disponibles.' );
}
wp_reset_postdata();

So, this code in the above product should return false, because acording to the rules it stops being bookable in may, but it still returns true. If I move the start day to may, it will correctly return false.

Can you help me get what I'm doing wrong?

oldslow
  • 11
  • 3
  • Where are you using that code? Is it in a function? Can you give the complete code as `continue;` need to be used in a loop (so if it's not the case, it's making trouble). – LoicTheAztec Mar 12 '21 at 23:10
  • 1
    It's in the body of a page. Rudimentary, I know, but I'll move it later! I've completed the code further in the edit. – oldslow Mar 12 '21 at 23:25
  • 1
    Thanks, but there's no difference. Still getting the same result. `global $product;` seems to work fine though, the variable is correctly asigned. – oldslow Mar 13 '21 at 00:05
  • So this is not working: `echo (WC_Product_Booking_Rule_Manager::check_availability_rules_against_time('2020-04-01','2020-07-01',$resource->id,$product,true)) ? 'Available' : 'Not Available';`, right? I will take a look tomorrow as is getting late tonight. – LoicTheAztec Mar 13 '21 at 00:20

0 Answers0