ID)) { return; } } woocommerce_register_additional_checkout_field([ 'id' => ACS_POINTS_PLUGIN_ID . '/point_id', 'label' => 'ACS Point', 'location' => 'order', 'type' => 'text', 'required' => false, 'hidden' => false, ]); }); add_action('woocommerce_blocks_validate_location_order_fields', function (\WP_Error $errors, $fields, $group) { $chosen = WC()->session ? WC()->session->get('chosen_shipping_methods') : []; $is_acs = is_array($chosen) && in_array(ACS_POINTS_PLUGIN_ID, $chosen); if (!$is_acs) { try { $packages = WC()->shipping()->get_packages(); foreach ($packages as $package) { foreach ($package['rates'] as $rate) { } } } 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); 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); add_action('wp_enqueue_scripts', function () { if (!function_exists('is_checkout') || !is_checkout()) { return; } $settings = AcsPointsPlugin::getSettings(); if ($settings['enabled'] !== 'yes') { return; } global $post; if (!is_a($post, 'WP_Post') || !has_block('woocommerce/checkout', $post->ID)) { return; } $mapProvider = $settings['mapProvider'] ?? 'google'; wp_enqueue_style( ACS_POINTS_PLUGIN_ID . '-css-styles', plugins_url('css/styles.css', ACS_POINTS_PLUGIN_DIR_FILE), [], ACS_POINTS_PLUGIN_VERSION, 'all' ); if ($mapProvider === 'osm') { wp_enqueue_style(ACS_POINTS_PLUGIN_ID . '-leaflet-css', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css', [], '1.9.4', 'all'); wp_enqueue_style(ACS_POINTS_PLUGIN_ID . '-leaflet-cluster-css', 'https://unpkg.com/leaflet.markercluster@1.5.3/dist/MarkerCluster.css', [], '1.5.3', 'all'); wp_enqueue_style(ACS_POINTS_PLUGIN_ID . '-leaflet-cluster-default-css', 'https://unpkg.com/leaflet.markercluster@1.5.3/dist/MarkerCluster.Default.css', [], '1.5.3', 'all'); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-leaflet', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', [], '1.9.4', false); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-leaflet-cluster', 'https://unpkg.com/leaflet.markercluster@1.5.3/dist/leaflet.markercluster.js', [ACS_POINTS_PLUGIN_ID . '-js-leaflet'], '1.5.3', false); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-script', plugins_url('js/script.js', ACS_POINTS_PLUGIN_DIR_FILE), ['jquery', ACS_POINTS_PLUGIN_ID . '-js-leaflet', ACS_POINTS_PLUGIN_ID . '-js-leaflet-cluster'], ACS_POINTS_PLUGIN_VERSION, true); } else { $googleMapsKey = $settings['googleMapsKey'] ?? ''; wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-googleapis', 'https://maps.googleapis.com/maps/api/js?key=' . $googleMapsKey . '&libraries=geometry&callback=initMap', [], 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-google.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_localize_script( ACS_POINTS_PLUGIN_ID . '-js-script', 'acsPointsConfig', array( 'mapProvider' => $mapProvider, 'pluginUrl' => plugins_url('', ACS_POINTS_PLUGIN_DIR_FILE), ) ); 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; } }); });