Problem with ACF Date Range and Repeater Integration in WooCommerce

I am using ACF to create fields integrated with WooCommerce. I am struggling to manage the tour status for each period in the Week Days mode: The tour has 2 modes: Week Days and Exact Dates. A tour can have multiple periods (using the repeater field tour_period). When selecting Week Days mode, the tour_period contains field groups tour_date_range, which include tour_start_date and tour_end_date. When selecting Exact Dates, the range is not used but replaced with tour_dates (which is a repeater field and can be added as many times as needed). Each period has a field tour_booked (checkbox, false/true). I want to display a message when the tour is marked as booked (using an AJAX call for live checking). The status of tour_booked for one period should not affect other periods (in cases where one tour has multiple periods). The current issue in Week Days mode is that when one period has tour_booked set to true, other periods also display the same message, preventing all periods from being booked. This problem does not occur in Exact Dates mode. I have tried many approaches but still have issues in Week Days mode. Here is my code: Handler if (!class_exists('My_Tour_Fields_Handler')) { class My_Tour_Fields_Handler { const DATE_FORMAT = 'Y-m-d'; public static function get_date_format() { return self::DATE_FORMAT; } public static function get_tour_mode($product_id) { return get_field('tour_mode', $product_id); } public static function get_tour_period($product_id) { return get_field('tour_period', $product_id); } public static function is_tour_booked($product_id, $selected_date) { $tour_period = self::get_tour_period($product_id); $tour_booked = false; if ($tour_period) { $tour_mode = strtolower(trim(self::get_tour_mode($product_id))); foreach ($tour_period as $period) { if ($tour_mode === 'exact dates' && isset($period['exact_dates']) && is_array($period['exact_dates'])) { foreach ($period['exact_dates'] as $date) { if (isset($date['exact_date']) && $date['exact_date'] === $selected_date && isset($period['tour_booked']) && $period['tour_booked'] === true) { $tour_booked = true; break 2; } } } elseif ($tour_mode === 'week days' && isset($period['tour_date_range'])) { $tour_date_range = $period['tour_date_range']; if (isset($tour_date_range['tour_start_date']) && isset($tour_date_range['tour_end_date'])) { $start_date = $tour_date_range['tour_start_date']; $end_date = $tour_date_range['tour_end_date']; // Convert dates to timestamp for comparison $selected_date_ts = strtotime($selected_date); $start_date_ts = strtotime($start_date); $end_date_ts = strtotime($end_date); if ($selected_date_ts >= $start_date_ts && $selected_date_ts <= $end_date_ts && isset($period['tour_booked']) && $period['tour_booked'] === true) { $tour_booked = true; break; } } } } } return $tour_booked; } public static function generate_hidden_product_id_input($product_id) { // Ensure that the product ID is valid if (!is_numeric($product_id)) { return ''; } // Return HTML for hidden input field return '<input type="hidden" id="product_id" name="product_id" value="' . esc_attr($product_id) . '">'; } } } ?> Checker include_once MY_TOUR_PLUGIN_DIR . 'includes/config/config-tour-date.php'; include_once MY_TOUR_PLUGIN_DIR . 'includes/class-tour-fields-handler.php'; // Enqueue JS and insert inline script function my_enqueue_tour_booked_scripts() { wp_enqueue_script('jquery'); wp_add_inline_script('jquery', ' jQuery(document).ready(function ($) { const dateSelector = `#' . TOUR_DATE_SELECTOR_ID . '`; $(dateSelector).on("change", function () { const selectedDate = $(this).val(); const productId = $("input[name=\'product_id\']").val(); if (selectedDate && productId) { $.ajax({ url: "' . admin_url('admin-ajax.php') . '", type: "POST", data: { action: "check_tour_booked", product_id: productId, selected_date: selectedDate }, success: function (response) { if (response.success) { alert(response.data.message); } else { alert("Error: " + response.data.message); } }, error: function () { alert("AJAX error."); } }); } }); }); '); } add_action('wp_enqueue_scripts', 'my_enqueue_tour_booked_scripts'); // Insert hidden input for product_id on single product page function my_add_hidden_product_id() { global $product; if (is_product() && $product) { echo '<input type="hidden" name="product_id" value="' . $product->get_id() . '">'; } } add_action('woocommerce_before_add_to_cart_button', 'my_add_hidden_product_id'); // AJAX handler to check if the tour is fully booked function my_check_tour_booked() { if (isset($_POST['product_id']) && isset($_POST['selected_date'])) { $product_id = sanitize_text_field($_POST['product_id']); $selected_date = sanitize_text_field($_POST['selected_date']); $is_booked = My_Tour_Fields_Handler::is_tour_booked($product_id, $selected_date); if ($is_booked) { wp_send_json_success(array('message' => 'Tour is fully booked.')); } else { wp_send_json_success(array('message' => 'Date available.')); } } else { wp_send_json_error(array('message' => 'Invalid data.')); } } add_action('wp_ajax_check_tour_booked', 'my_check_tour_booked'); add_action('wp_ajax_nopriv_check_tour_booked', 'my_check_tour_booked'); ?>

Comment (0)

You’ll be in good company