From 64c5c8c2b12cf7dfb34ed73dc0d786491525f4f1 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:01:38 +0530 Subject: [PATCH 01/13] update the `get_check_times()` to accept ProductID --- ...class-wc-product-accommodation-booking.php | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index ccd2da43..3fbcb52c 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -328,20 +328,34 @@ public function get_blocks_in_range( $start_date, $end_date, $intervals = array( /** * Get checkin and checkout times. * - * @param string $type + * @param string $type The type, check_in or check_out. + * @param int $product_id The product ID. * - * @return string Time, either from options or default + * @return string The time, either from options or default or from the filtered value. */ - public static function get_check_times( $type ) { - $option = get_option( 'woocommerce_accommodation_bookings_times_settings' ); + public static function get_check_times( $type, $product_id = 0 ) { + $option = get_option( 'woocommerce_accommodation_bookings_times_settings' ); + $check_time = ''; + switch ( $type ) { case 'in': - return isset( $option['check_in'] ) ? $option['check_in'] : '14:00'; + $check_time = isset( $option['check_in'] ) ? $option['check_in'] : '14:00'; case 'out': - return isset( $option['check_out'] ) ? $option['check_out'] : '14:00'; + $check_time = isset( $option['check_out'] ) ? $option['check_out'] : '14:00'; } - return ''; + /** + * Filter the check-in/out times for a specific product. + * + * @hook woocommerce_accommodation_booking_get_check_times + * + * @param {string} $check_time The check-in/out time stored in the database. + * @param {string} $type The type, check_in or check_out. + * @param {int} $product_id The product ID. + * + * @return {string} The filtered/original time. + */ + return apply_filters( 'woocommerce_accommodation_booking_get_check_times', $check_time, $type, $product_id ); } /** From 2c9c55d0cda49a2baa17f1092a6b5d9cb7079cb2 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:16:17 +0530 Subject: [PATCH 02/13] update the `get_item_data()` with new param --- includes/class-wc-accommodation-booking-cart-manager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-accommodation-booking-cart-manager.php b/includes/class-wc-accommodation-booking-cart-manager.php index 13ec63c8..951418b8 100644 --- a/includes/class-wc-accommodation-booking-cart-manager.php +++ b/includes/class-wc-accommodation-booking-cart-manager.php @@ -33,8 +33,8 @@ function add_to_cart() { */ public function get_item_data( $other_data, $cart_item ) { if ( 'accommodation-booking' === $cart_item['data']->get_type() && ! empty( $other_data ) ) { - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $cart_item['product_id'] ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $cart_item['product_id'] ); $end_date = date_i18n( get_option( 'date_format'), $cart_item['booking']['_end_date'] ); if ( ! empty( $check_in ) ) { From 07452fa1844ce1fdfbb70e38d06c661536159a32 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:16:48 +0530 Subject: [PATCH 03/13] update the date picker file with new param --- includes/class-wc-accommodation-booking-date-picker.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-accommodation-booking-date-picker.php b/includes/class-wc-accommodation-booking-date-picker.php index cd4e9567..fe237dfd 100644 --- a/includes/class-wc-accommodation-booking-date-picker.php +++ b/includes/class-wc-accommodation-booking-date-picker.php @@ -29,8 +29,8 @@ public function __construct() { * @return mixed */ public function add_accommodation_posted_data( $data, $product, $total_duration ) { - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $product->get_id() ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $product->get_id() ); if ( 'night' === $product->get_duration_unit() ) { $data['_start_date'] = strtotime( "{$data['_year']}-{$data['_month']}-{$data['_day']} $check_in" ); @@ -106,7 +106,7 @@ public function add_partially_booked_dates( $booked_data_array, $product ) { continue; } - $check_in_time = $product->get_check_times( 'in' ); + $check_in_time = $product->get_check_times( 'in', $product->get_id() ); if ( 'in' === $which ) { $check_time = strtotime( '-1 day ' . $check_in_time , $time ); } else { From d99e7f3364cf084af7735c54621b94928a45ec97 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:21:37 +0530 Subject: [PATCH 04/13] update the order manager with a new param --- includes/class-wc-accommodation-booking-order-manager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-accommodation-booking-order-manager.php b/includes/class-wc-accommodation-booking-order-manager.php index 2885f48d..77c9825e 100644 --- a/includes/class-wc-accommodation-booking-order-manager.php +++ b/includes/class-wc-accommodation-booking-order-manager.php @@ -33,8 +33,8 @@ public function add_checkinout_info_to_order_email( $item_id, $item, $order ) { return; } - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $item['product_id'] ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $item['product_id'] ); ?>

From 5ae93752b2d4ab094d334ea93fe17243a4330697 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:22:14 +0530 Subject: [PATCH 05/13] update the tabs class with a new param --- ...ass-wc-accommodation-booking-product-tabs.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/includes/class-wc-accommodation-booking-product-tabs.php b/includes/class-wc-accommodation-booking-product-tabs.php index 61223704..4b9d9996 100644 --- a/includes/class-wc-accommodation-booking-product-tabs.php +++ b/includes/class-wc-accommodation-booking-product-tabs.php @@ -10,6 +10,11 @@ */ class WC_Accommodation_Booking_Product_Tabs { + /** + * Product ID. + */ + public $product_id = 0; + /** * Hook into WooCommerce.. */ @@ -22,8 +27,8 @@ public function __construct() { * @return boolean */ public function are_time_fields_filled_out() { - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $this->product_id ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $this->product_id ); if ( empty( $check_in ) ) { return false; @@ -56,6 +61,9 @@ public function add_time_tab( $tabs = array() ) { return $tabs; } + // Set the product ID. + $this->product_id = $post->ID; + if ( ! $this->are_time_fields_filled_out() ) { return $tabs; } @@ -77,8 +85,8 @@ public function add_time_tab_content() { if ( ! $this->are_time_fields_filled_out() ) { return; } - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $this->product_id ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $this->product_id ); ?>

    From 1e3b180005f0621fa305078391fa47710773ca9f Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:22:49 +0530 Subject: [PATCH 06/13] update `WC_Accommodation_Booking` with new param --- includes/class-wc-accommodation-booking.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/class-wc-accommodation-booking.php b/includes/class-wc-accommodation-booking.php index 2b9e585d..6abf3999 100644 --- a/includes/class-wc-accommodation-booking.php +++ b/includes/class-wc-accommodation-booking.php @@ -109,7 +109,7 @@ public function add_checkin_time_to_booking_start_time( $date, $booking ) { return $date; } - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $product->get_id() ); $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() ); $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() ); @@ -123,7 +123,7 @@ public function add_checkout_time_to_booking_end_time( $date, $booking ) { return $date; } - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $product->get_id() ); $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() ); $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() ); @@ -181,8 +181,8 @@ public function update_start_end_time( $booking_id ) { return; } - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $product_id ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $product_id ); $start = get_post_meta( $booking_id, '_booking_start', true ); $end = get_post_meta( $booking_id, '_booking_end', true ); From 39f7bd9ef38fdabcdd488caa15e3555490d309fa Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 18:24:07 +0530 Subject: [PATCH 07/13] update the `get_time_slots` with new param --- includes/class-wc-product-accommodation-booking.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index 3fbcb52c..74196899 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -195,15 +195,16 @@ public function get_price_html( $price = '' ) { public function get_time_slots( $blocks, $resource_id = 0, $from = 0, $to = 0, $include_sold_out = false ) { $bookable_product = $this; - $transient_name = 'book_ts_' . md5( http_build_query( array( $bookable_product->get_id(), $resource_id, $from, $to ) ) ); + $product_id = $bookable_product->get_id(); + $transient_name = 'book_ts_' . md5( http_build_query( array( $product_id, $resource_id, $from, $to ) ) ); $available_slots = get_transient( $transient_name ); $booking_slots_transient_keys = array_filter( (array) get_transient( 'booking_slots_transient_keys' ) ); - if ( ! isset( $booking_slots_transient_keys[ $bookable_product->get_id() ] ) ) { - $booking_slots_transient_keys[ $bookable_product->get_id() ] = array(); + if ( ! isset( $booking_slots_transient_keys[ $product_id ] ) ) { + $booking_slots_transient_keys[ $product_id ] = array(); } - $booking_slots_transient_keys[ $bookable_product->get_id() ][] = $transient_name; + $booking_slots_transient_keys[ $product_id ][] = $transient_name; // Give array of keys a long ttl because if it expires we won't be able to flush the keys when needed. // We can't use 0 to never expire because then WordPress will autoload the option on every page. @@ -229,8 +230,8 @@ public function get_time_slots( $blocks, $resource_id = 0, $from = 0, $to = 0, $ $has_resources = $bookable_product->has_resources(); foreach ( $blocks as $block ) { - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in' ); - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out' ); + $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $product_id ); + $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $product_id ); // Blocks for accommodation products are initially calculated as days but the actuall time blocks are shifted by check in and checkout times. $block_start_time = strtotime( "{$check_in}", $block ); $block_end_time = strtotime( "{$check_out}", strtotime( "+1 days", $block ) ); From 12a3d353731c39d98cf6c82e46a919389f83136a Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Fri, 3 Feb 2023 20:32:14 +0530 Subject: [PATCH 08/13] break the flow to avoid value overwrite --- includes/class-wc-product-accommodation-booking.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index 74196899..8ac07167 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -341,8 +341,10 @@ public static function get_check_times( $type, $product_id = 0 ) { switch ( $type ) { case 'in': $check_time = isset( $option['check_in'] ) ? $option['check_in'] : '14:00'; + break; case 'out': $check_time = isset( $option['check_out'] ) ? $option['check_out'] : '14:00'; + break; } /** From 46d0a120abd18904a87e0aefc95b4ab1f63558e6 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Thu, 2 Mar 2023 16:49:31 +0530 Subject: [PATCH 09/13] Fix - remove use of filter on existing bookings --- includes/class-wc-accommodation-booking.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-accommodation-booking.php b/includes/class-wc-accommodation-booking.php index 6abf3999..2a2e03d8 100644 --- a/includes/class-wc-accommodation-booking.php +++ b/includes/class-wc-accommodation-booking.php @@ -109,12 +109,10 @@ public function add_checkin_time_to_booking_start_time( $date, $booking ) { return $date; } - $check_in = WC_Product_Accommodation_Booking::get_check_times( 'in', $product->get_id() ); - $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() ); $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() ); - return date_i18n( $date_format, $booking->start ) . date_i18n( $time_format, strtotime( "Today " . $check_in ) ); + return date_i18n( $date_format, $booking->start ) . date_i18n( $time_format, $booking->start ); } public function add_checkout_time_to_booking_end_time( $date, $booking ) { @@ -123,11 +121,10 @@ public function add_checkout_time_to_booking_end_time( $date, $booking ) { return $date; } - $check_out = WC_Product_Accommodation_Booking::get_check_times( 'out', $product->get_id() ); $date_format = apply_filters( 'woocommerce_bookings_date_format', wc_date_format() ); $time_format = apply_filters( 'woocommerce_bookings_time_format', ', ' . wc_time_format() ); - return date_i18n( $date_format, $booking->end ) . date_i18n( $time_format, strtotime( "Today " . $check_out ) ); + return date_i18n( $date_format, $booking->end ) . date_i18n( $time_format, $booking->end ); } /** From cd63055a46233e5877a7f758ecef680a1be2dc02 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Wed, 5 Apr 2023 17:02:36 +0530 Subject: [PATCH 10/13] force return integer ID --- includes/class-wc-product-accommodation-booking.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index 8ac07167..b7420055 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -358,7 +358,7 @@ public static function get_check_times( $type, $product_id = 0 ) { * * @return {string} The filtered/original time. */ - return apply_filters( 'woocommerce_accommodation_booking_get_check_times', $check_time, $type, $product_id ); + return apply_filters( 'woocommerce_accommodation_booking_get_check_times', $check_time, $type, (int) $product_id ); } /** From e3593f477c2aec59a1ad26c7140063f76656bc92 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Thu, 6 Apr 2023 12:19:36 +0530 Subject: [PATCH 11/13] unwrap types from {} --- includes/class-wc-product-accommodation-booking.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index b7420055..557c08c3 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -352,9 +352,9 @@ public static function get_check_times( $type, $product_id = 0 ) { * * @hook woocommerce_accommodation_booking_get_check_times * - * @param {string} $check_time The check-in/out time stored in the database. - * @param {string} $type The type, check_in or check_out. - * @param {int} $product_id The product ID. + * @param string $check_time The check-in/out time stored in the database. + * @param string $type The type, check_in or check_out. + * @param int $product_id The product ID. * * @return {string} The filtered/original time. */ From 1352f6b3c64149ad1f9d9a926ce25a7782d95584 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Thu, 6 Apr 2023 12:20:40 +0530 Subject: [PATCH 12/13] shorten things up a little --- includes/class-wc-product-accommodation-booking.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index 557c08c3..5523e30d 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -340,10 +340,10 @@ public static function get_check_times( $type, $product_id = 0 ) { switch ( $type ) { case 'in': - $check_time = isset( $option['check_in'] ) ? $option['check_in'] : '14:00'; + $check_time = $option['check_in'] ?? '14:00'; break; case 'out': - $check_time = isset( $option['check_out'] ) ? $option['check_out'] : '14:00'; + $check_time = $option['check_out'] ?? '14:00'; break; } From f10f347f0f1d18514286895f036b21b66b749279 Mon Sep 17 00:00:00 2001 From: faisal-alvi Date: Thu, 6 Apr 2023 12:35:13 +0530 Subject: [PATCH 13/13] update the hook doc --- includes/class-wc-product-accommodation-booking.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/includes/class-wc-product-accommodation-booking.php b/includes/class-wc-product-accommodation-booking.php index 5523e30d..a6ded682 100644 --- a/includes/class-wc-product-accommodation-booking.php +++ b/includes/class-wc-product-accommodation-booking.php @@ -350,13 +350,11 @@ public static function get_check_times( $type, $product_id = 0 ) { /** * Filter the check-in/out times for a specific product. * - * @hook woocommerce_accommodation_booking_get_check_times - * * @param string $check_time The check-in/out time stored in the database. * @param string $type The type, check_in or check_out. * @param int $product_id The product ID. * - * @return {string} The filtered/original time. + * @return string The filtered/original time. */ return apply_filters( 'woocommerce_accommodation_booking_get_check_times', $check_time, $type, (int) $product_id ); }