'Παραλαβή από ACS Points',//'Collection from an ACS Point', 'checkout_button_label' => 'Επιλέξτε ένα ACS Point',//'Select an ACS Point', 'checkout_validation_error' => 'Please select a pickup point', 'checkout_validation_error_weight' => 'We are sorry. %s is not possible for orders of %d kg and higher.', 'checkout_selected_point_title' => "You've selected to pick up your order from", 'email_selected_point_title' => 'Selected ACS SmartPoint', ]; static $configuration = [ 'checkout_input_name' => 'acs_pp_point_id', 'post_meta_field_name' => 'acs_pp_point', ]; public function __construct() { $this->settings = self::getSettings(); if ($this->settings['enabled'] === 'yes') { $this->initWoo(); } } function initWoo() { add_action('woocommerce_review_order_before_cart_contents', array($this, 'pre_checkout_validation'), 10); add_action('woocommerce_after_checkout_validation', array($this, 'checkout_validation'), 10, 2); add_action('woocommerce_before_checkout_form', array($this, 'add_map_in_checkout'), 10, 1); add_action('woocommerce_after_shipping_rate', array($this, 'add_map_trigger_to_shipping_option'), 10, 2); add_action('woocommerce_after_order_notes', array($this, 'add_checkout_point_input')); add_action('woocommerce_checkout_update_order_meta', array($this, 'save_checkout_point_input')); add_action('woocommerce_order_details_after_customer_details', array($this, 'show_point_details_in_customer'), 10); add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'show_point_details_in_admin'), 10, 1); add_action(ACS_POINTS_PLUGIN_ID . 'cron_hook', array($this, 'fetch_points_json')); } public static function getSettings() { $settings = get_option('woocommerce_acs-points-plugin_settings'); if (empty($settings)) { $settings = array('enabled' => 'no'); } if (isset($settings['baseCost']) && !isset($settings['pricing']['overland']['baseCost'])) { $settings['pricing']['overland']['baseCost'] = $settings['baseCost']; } if (isset($settings['baseCostKgLimit']) && !isset($settings['pricing']['overland']['baseCostKgLimit'])) { $settings['pricing']['overland']['baseCostKgLimit'] = $settings['baseCostKgLimit']; } if (isset($settings['costPerKg']) && !isset($settings['pricing']['overland']['costPerKg'])) { $settings['pricing']['overland']['costPerKg'] = $settings['costPerKg']; } return $settings; } public function get_order_weight() { $chosen_methods = WC()->session->get('chosen_shipping_methods'); $packages = WC()->shipping->get_packages(); $weight = 0; foreach ($packages as $i => $package) { if ($chosen_methods[$i] != ACS_POINTS_PLUGIN_ID) { continue; } foreach ($package['contents'] as $item_id => $values) { $_product = $values['data']; if (is_numeric($_product->get_weight())) { $weight += $_product->get_weight() * $values['quantity']; } } } return (float)wc_get_weight($weight, 'kg'); } public function pre_checkout_validation() { $chosen_methods = WC()->session->get('chosen_shipping_methods'); if (!is_array($chosen_methods)) { return; } if (!in_array(ACS_POINTS_PLUGIN_ID, $chosen_methods)) { return; } $weightLimit = (int)$this->settings['weightUpperLimit']; if ($weightLimit == 0) { return; } $weight = $this->get_order_weight(); if ($weight > $weightLimit) { $message = sprintf( __(self::$labels['checkout_validation_error_weight'], ACS_POINTS_PLUGIN_ID), __(self::$labels['checkout_option_title'], ACS_POINTS_PLUGIN_ID), $weightLimit ); $messageType = "error"; if (!wc_has_notice($message, $messageType)) { wc_add_notice($message, $messageType); } } } public function checkout_validation($data, $errors) { if ( ACS_POINTS_PLUGIN_ID == $data['shipping_method'][0] && empty($_POST[self::$configuration['checkout_input_name']]) ) { $errors->add('validation', __(self::$labels['checkout_validation_error'], ACS_POINTS_PLUGIN_ID)); } $weightLimit = (int)$this->settings['weightUpperLimit']; if ($weightLimit != 0 && $this->get_order_weight() > $weightLimit) { $errors->add('validation', sprintf( __(self::$labels['checkout_validation_error_weight'], ACS_POINTS_PLUGIN_ID), __(self::$labels['checkout_option_title'], ACS_POINTS_PLUGIN_ID), $weightLimit )); } } public function add_map_in_checkout() { $filesPath = '/wp-content/plugins/acs-points/'; $mapProvider = $this->settings['mapProvider'] ?? 'google'; wp_enqueue_style(ACS_POINTS_PLUGIN_ID . '-css-styles', plugins_url('css/styles.css', __FILE__), array(), ACS_POINTS_PLUGIN_VERSION, 'all'); if ($mapProvider === 'osm') { // OpenStreetMap / Leaflet wp_enqueue_style(ACS_POINTS_PLUGIN_ID . '-leaflet-css', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css', array(), '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', array(), '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', array(), '1.5.3', 'all'); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-leaflet', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', array(), '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', array(ACS_POINTS_PLUGIN_ID . '-js-leaflet'), '1.5.3', false); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-woo-script', plugins_url('js/woo-script.js', __FILE__), array(), ACS_POINTS_PLUGIN_VERSION, 'all'); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-script', plugins_url('js/script.js', __FILE__), array(ACS_POINTS_PLUGIN_ID . '-js-leaflet', ACS_POINTS_PLUGIN_ID . '-js-leaflet-cluster'), ACS_POINTS_PLUGIN_VERSION, 'all'); } else { // Google Maps $googleMapsKey = $this->settings['googleMapsKey'] ?? ''; wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-googleapis', 'https://maps.googleapis.com/maps/api/js?key=' . $googleMapsKey . '&libraries=geometry&callback=initMap', array(), ACS_POINTS_PLUGIN_VERSION, 'all'); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-markerclusterer', plugins_url('js/markerclusterer.js', __FILE__), array(), ACS_POINTS_PLUGIN_VERSION, 'all'); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-woo-script', plugins_url('js/woo-script.js', __FILE__), array(), ACS_POINTS_PLUGIN_VERSION, 'all'); wp_enqueue_script(ACS_POINTS_PLUGIN_ID . '-js-script-google', plugins_url('js/script-google.js', __FILE__), array(), ACS_POINTS_PLUGIN_VERSION, 'all'); } // Pass config to JS wp_localize_script( $mapProvider === 'osm' ? ACS_POINTS_PLUGIN_ID . '-js-script' : ACS_POINTS_PLUGIN_ID . '-js-script-google', 'acsPointsConfig', array( 'mapProvider' => $mapProvider, 'pluginUrl' => plugins_url('', __FILE__), ) ); require 'acs-points-map.php'; } public function add_map_trigger_to_shipping_option($method) { if ($method->id == ACS_POINTS_PLUGIN_ID) { $buttonLabel = __(self::$labels['checkout_button_label'], ACS_POINTS_PLUGIN_ID); echo '
'; } } public function add_checkout_point_input($checkout) { $field = self::$configuration['checkout_input_name']; echo ''; } public function save_checkout_point_input($order_id) { $value = (int)$_POST[self::$configuration['checkout_input_name']]; if (!empty($value) && $_POST['shipping_method'][0] == ACS_POINTS_PLUGIN_ID) { $pointsFile = file_get_contents(__DIR__ . '/data.json'); $points = json_decode($pointsFile, true); $selectedPoint = [$value]; foreach ($points['points'] as $point) { if ($point['id'] == $value) { $selectedPoint = $point; } } try { $order = wc_get_order($order_id); if ($order) { $order->update_meta_data( self::$configuration['post_meta_field_name'], wp_json_encode($selectedPoint, JSON_UNESCAPED_UNICODE) ); $order->update_meta_data( self::$configuration['post_meta_field_name'] . '_slug', esc_attr($selectedPoint['Acs_Station_Destination'] ?? '') . esc_attr($selectedPoint['Acs_Station_Branch_Destination'] ?? '') ); $order->save(); return; } } catch (\Throwable $e) { } //update_post_meta($order_id, self::$configuration['post_meta_field_name'], json_encode($selectedPoint, JSON_UNESCAPED_UNICODE)); //update_post_meta($order_id, self::$configuration['post_meta_field_name'].'_slug', esc_attr($selectedPoint['Acs_Station_Destination'] ?? '') . esc_attr($selectedPoint['Acs_Station_Branch_Destination'] ?? '')); } } public function show_point_details_in_customer($order) { $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; $pointDetailsRaw = get_post_meta($order_id, self::$configuration['post_meta_field_name'], true); if (!$pointDetailsRaw) { return; } $pointDetails = json_decode($pointDetailsRaw, true); $title = __(self::$labels['email_selected_point_title'], ACS_POINTS_PLUGIN_ID); $stationName = esc_attr($pointDetails['name']); $stationCode = esc_attr($pointDetails['Acs_Station_Destination'] ?? '') . esc_attr($pointDetails['Acs_Station_Branch_Destination'] ?? ''); echo '

' . esc_attr($title) . '

' . $stationName . ' (' . $stationCode . ')

'; } public function show_point_details_in_admin($order) { return $this->show_point_details_in_customer($order); } public static function fetch_points_json($force = true) { $cached = get_transient(ACS_POINTS_PLUGIN_ID . '-points'); if (!$force && $cached !== false) { return $cached; } $settings = self::getSettings(); $data = [ 'ACSAlias' => 'ACS_Get_Stations_For_Plugin', 'ACSInputParameters' => [ 'locale' => null, 'Company_ID' => $settings['acsCompanyID'] ?? null, 'Company_Password' => $settings['acsCompanyPassword'] ?? null, 'User_ID' => $settings['acsUserID'] ?? null, 'User_Password' => $settings['acsUserPassword'] ?? null, ], ]; $args = array( 'timeout' => 10, 'body' => json_encode($data), 'headers' => array( 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'ACSApiKey' => ($settings['acsApiKey'] ?? null),), ); $responseRaw = wp_remote_post('https://webservices.acscourier.net/ACSRestServices/api/ACSAutoRest', $args); $httpCode = wp_remote_retrieve_response_code($responseRaw); $response = wp_remote_retrieve_body($responseRaw); $response = json_decode($response, true); $points = $response['ACSOutputResponce']['ACSTableOutput']['Table_Data1'] ?? []; $points = array_values(array_filter($points, function ($item) use($settings) { if ($item['type'] == 'branch' && $item['Acs_Station_Branch_Destination'] != '1') { return false; } if ( isset($settings['countries_availability']) && $settings['countries_availability'] == 'cy' && $item['Country_Code'] != 'CY' ) { return false; } if ( isset($settings['countries_availability']) && $settings['countries_availability'] == 'gr' && $item['Country_Code'] != 'GR' ) { return false; } return true; })); $data = [ 'status_code' => $httpCode, 'meta' => $response['ACSOutputResponce']['ACSTableOutput']['Table_Data'] ?? [], 'points' => $points, ]; set_transient(ACS_POINTS_PLUGIN_ID . '-points', $data, 60 * 30); file_put_contents( __DIR__ . '/data.json', json_encode([ 'timestamp' => wp_date('Y-m-d H:i'), 'meta' => $response['ACSOutputResponce']['ACSTableOutput']['Table_Data'] ?? [], 'points' => $points, ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ); return $data; } } (new AcsPointsPlugin); }