ID)) { return; } } woocommerce_register_additional_checkout_field([ 'id' => ACS_POINTS_PLUGIN_ID . '/point_id', 'label' => 'ACS Point', 'location' => 'order', 'type' => 'text', 'required' => false, 'hidden' => true, ]); }); // ── 2. Validate: require a point only when ACS Points shipping is selected ─── add_action('woocommerce_blocks_validate_location_order_fields', function (\WP_Error $errors, $fields, $group) { // Only validate if ACS Points shipping is chosen $chosen = WC()->session ? WC()->session->get('chosen_shipping_methods') : []; $is_acs = is_array($chosen) && in_array(ACS_POINTS_PLUGIN_ID, $chosen); // For Blocks, check via the order's shipping items instead if (!$is_acs) { // Try to detect from cart try { $packages = WC()->shipping()->get_packages(); foreach ($packages as $package) { foreach ($package['rates'] as $rate) { // Can't easily detect selected rate here — skip session-less validation } } } catch (\Throwable $e) {} return; } $point_id = $fields[ACS_POINTS_PLUGIN_ID . '/point_id'] ?? ''; if (empty($point_id)) { $errors->add( 'acs_no_point', __(AcsPointsPlugin::$labels['checkout_validation_error'], ACS_POINTS_PLUGIN_ID) ); } }, 10, 3); // ── 3. Save the full point data to order meta when order is placed ──────────── add_action('woocommerce_set_additional_field_value', function ($key, $value, $group, $object) { if ($key !== ACS_POINTS_PLUGIN_ID . '/point_id') { return; } if (!($object instanceof WC_Order)) { return; } $point_id = sanitize_text_field($value); if (empty($point_id)) { return; } $points_file = plugin_dir_path(ACS_POINTS_PLUGIN_DIR_FILE) . 'data.json'; if (!file_exists($points_file)) { return; } $points_data = json_decode(file_get_contents($points_file), true); $selected_point = [(int)$point_id]; foreach ($points_data['points'] ?? [] as $point) { if ($point['id'] == $point_id) { $selected_point = $point; break; } } $object->update_meta_data( AcsPointsPlugin::$configuration['post_meta_field_name'], wp_json_encode($selected_point, JSON_UNESCAPED_UNICODE) ); $object->update_meta_data( AcsPointsPlugin::$configuration['post_meta_field_name'] . '_slug', esc_attr($selected_point['Acs_Station_Destination'] ?? '') . esc_attr($selected_point['Acs_Station_Branch_Destination'] ?? '') ); $object->save(); }, 10, 4); // ── 4. Enqueue scripts and map HTML on the checkout page ───────────────────── add_action('wp_enqueue_scripts', function () { if (!function_exists('is_checkout') || !is_checkout()) { return; } $settings = AcsPointsPlugin::getSettings(); if ($settings['enabled'] !== 'yes') { return; } // Only add for Blocks checkout (not classic — classic has its own enqueue via add_map_in_checkout) global $post; if (!is_a($post, 'WP_Post') || !has_block('woocommerce/checkout', $post->ID)) { return; } $googleMapsKey = $settings['googleMapsKey'] ?? ''; wp_enqueue_script( ACS_POINTS_PLUGIN_ID . '-js-googleapis', 'https://maps.googleapis.com/maps/api/js?key=' . $googleMapsKey . '&libraries=geometry', [], ACS_POINTS_PLUGIN_VERSION, true ); wp_enqueue_script( ACS_POINTS_PLUGIN_ID . '-js-markerclusterer', plugins_url('js/markerclusterer.js', ACS_POINTS_PLUGIN_DIR_FILE), [], ACS_POINTS_PLUGIN_VERSION, true ); wp_enqueue_script( ACS_POINTS_PLUGIN_ID . '-js-script', plugins_url('js/script.js', ACS_POINTS_PLUGIN_DIR_FILE), ['jquery'], ACS_POINTS_PLUGIN_VERSION, true ); wp_enqueue_script( ACS_POINTS_PLUGIN_ID . '-js-blocks', plugins_url('js/acs-points-blocks.js', ACS_POINTS_PLUGIN_DIR_FILE), ['jquery', ACS_POINTS_PLUGIN_ID . '-js-script'], ACS_POINTS_PLUGIN_VERSION, true ); wp_enqueue_style( ACS_POINTS_PLUGIN_ID . '-css-styles', plugins_url('css/styles.css', ACS_POINTS_PLUGIN_DIR_FILE), [], ACS_POINTS_PLUGIN_VERSION, 'all' ); // Inject map HTML into footer add_action('wp_footer', function () { $filesPath = '/wp-content/plugins/acs-points/'; $mapFile = plugin_dir_path(ACS_POINTS_PLUGIN_DIR_FILE) . 'acs-points-map.php'; if (file_exists($mapFile)) { include $mapFile; } }); });