get_col("SELECT option_name FROM $wpdb->options WHERE option_name LIKE '_transient_wc_ship%'"); if (count($transients)) { foreach ($transients as $tr) { $hash = substr($tr, 11); delete_transient($hash); } } $transient_value = get_transient('shipping-transient-version'); WC_Cache_Helper::delete_version_transients( $transient_value ); if (WC()->session) { WC()->session->set('shipping_for_package', ''); } if ( ! class_exists( 'WC_ParcelMachine_Shipping_Method' ) ) { class WC_ParcelMachine_Shipping_Method extends WC_Shipping_Method { function __construct($instance_id = 0) { if (!$this->ext) { throw new Exception('Do not call this class directly!'); } $this->id = 'parcelmachine_' . mb_strtolower($this->ext); $this->instance_id = absint($instance_id); $this->method_title = $this->name_ext . ' ' . __(' Parcel Machine (MC)', 'wc_makecommerce_domain'); // $this->method_description = sprintf(__('Parcel machine transport method for %s', 'wc_makecommerce_domain'), $this->ext); $this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal', 'settings'); $this->init(); $this->check_updates(); } function check_updates() { if (($cur_ver = get_site_option('wc_mc_version', 0)) < WC_MC_VERSION) { update_site_option('wc_mc_version', WC_MC_VERSION); error_log("Need to update from [{$cur_ver}] to [".WC_MC_VERSION."]"); if (WC_MC_VERSION === 2.0) { // Check if omniva / smartpost is enabled and in which countries // Convert to shipping zone foreach (array('parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva', 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_SmartPost', 'parcelmachine_dpd' => 'WC_ParcelMachine_Shipping_Method_DPD') as $method_name => $method_class) { $transport_class_omniva = new $method_class(); if ($transport_class_omniva->enable === 'yes') { $zones = WC_Shipping_Zones::get_zones(); if (!empty($transport_class_omniva->countries)) { continue; } foreach ($transport_class_omniva->countries as $ecountry) { $has_ecountry = false; foreach ($zones as $zone) { foreach ($zone['zone_locations'] as $location) { if ($location->code === $ecountry) { $has_ecountry = $zone['zone_id']; foreach ($zone['shipping_methods'] as $method) { if ($method->id === $method_name) { continue 4; } } } } } if (!$has_ecountry) { $zone = new WC_Shipping_Zone(); $zone->set_zone_name($ecountry); $zone->add_location($ecountry, 'country'); $zone->save(); } else { $zone = new WC_Shipping_Zone($has_ecountry); } $instance_id = $zone->add_shipping_method($method_name); $transport_class = new $method_class($instance_id); $price = !empty($transport_class->settings['price_'.strtolower($ecountry)]) ? $transport_class->settings['price_'.strtolower($ecountry)] : 0; $free_shipping_min_amount = !empty($transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)]) ? $transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)] : 0; $transport_class->set_post_data(array($transport_class->get_field_key('price') => $price, $transport_class->get_field_key('free_shipping_min_amount') => $free_shipping_min_amount)); $transport_class->process_admin_options(); } } } } } } function init() { $this->init_form_fields(); $this->init_settings(); $this->enable = $this->settings['active']; $this->title = !empty($this->settings['method_name']) ? $this->settings['method_name'] : ''; if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) { $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE]; } if(!$this->title) { $this->title = $this->method_title; } $this->availability = 'specific'; $this->countries = !empty($this->settings['countries']) ? $this->settings['countries'] : array(); $this->prioritization = $this->settings['prioritization']; $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0; $this->maximum_weight = (double)$this->settings['maximum_weight']; $this->short_office_names = $this->settings['short_office_names']; $this->order_country = 'unknown'; add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options')); add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_parcelmachine_checkout_fields')); add_action('woocommerce_checkout_process', array(&$this, 'check_parcelmachine_checkout_fields')); add_action('woocommerce_after_checkout_validation', array(&$this, 'check_parcelmachine_checkout_fields')); add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_parcelmachine_order_meta')); // Woocommerce Multilingual overrides if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) { add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50); add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2); } } public function override_label_translation($rates) { $id_instance = $this->id . ':' . $this->instance_id; if(array_key_exists($id_instance, $rates)) { $rates[$id_instance]->label = $this->title; } return $rates; } public function override_shipping_title_translation($items, $order) { foreach ($items as $key => $item) { if($item['type'] == 'shipping') { foreach ($item['item_meta_array'] as $meta) { if ($meta->key === 'method_id') { $tmp = explode(':', $meta->value); if ($tmp[0] == $this->id) { $items[$key]['name'] = $this->title; } } } } } return $items; } public function generate_shipping_ui_javascript_html( $key, $data ) { ?> instance_settings['price']) ? $this->instance_settings['price'] : (isset($this->settings['price_'.$package['destination']['country']]) ? $this->settings['price_'.$package['destination']['country']] : 0); $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount']; if ($free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount) { $price = 0; } $free_shipping = true; foreach ($package['contents'] as $line) { $free_shipping = get_post_meta($line['product_id'], '_no_shipping_cost', true) === 'yes' ? $free_shipping : false; } if($free_shipping) { $price = 0; } $rate = array( 'id' => $this->get_rate_id(), 'label' => $this->title, 'cost' => $price, 'package' => $package, 'calc_tax' => 'per_order', ); $this->add_rate( $rate ); } function calculate_weight($package) { $weight = 0; foreach ($package['contents'] as $line) { $weight += (double)$line['data']->get_weight(); } return $weight; } function fits_parcel_machine($package) { foreach ($package['contents'] as $line) { if (get_post_meta($line['product_id'], '_no_parcel_machine', true) === 'yes') { return false; } } return true; } function is_available($package) { if (!$this->fits_parcel_machine($package)) { return false; } $package_weight = $this->calculate_weight($package); if ($package_weight >= $this->maximum_weight) { return false; } $is_available = $this->enable === 'yes'; if (!$is_available || !mk_is_api_set()) { return false; } //if (is_array($this->countries) && !in_array($package['destination']['country'], $this->countries)) { // $is_available = false; //} $this->order_country = $package['destination']['country']; return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package); } function mk_get_machines() { $machines = mk_get_machines($this->ext, $this->order_country); if ($this->prioritization === 'yes') { usort($machines, function($a, $b) { $sortorder = array( 'tallinn', 'tartu linn','tartu', 'narva', 'pärnu linn','pärnu', 'viljandi', 'kohtla-järve', 'rakvere', 'maardu', 'sillamäe', 'kuressaare', 'helsinki', 'espoo', 'tampere', 'vantaa', 'oulu', 'turku', 'jüväskülä', 'lahti', 'kuopio', 'kouvola', 'rīga','riga', 'daugavpils', 'liepāja','liepaja', 'jelgava', 'jūrmala','jurmala', 'ventspils', 'rezekne', 'valmiera', 'jekabpils', 'vilnius', 'kaunas', 'klaipeda', 'siauliai', 'panevezys', 'alytus', 'mariampole', 'mazeikiai', 'jonava', 'utena' ); $acity = mb_strtolower($a['city']); $bcity = mb_strtolower($b['city']); if (!$acity) { $acity = 'xxxxxxx'; } if (!$bcity) { $bcity = 'xxxxxxx'; } $aidx = array_search($acity, $sortorder); $bidx = array_search($bcity, $sortorder); if ($aidx !== false) { $acity = str_pad($aidx, 4, "0", STR_PAD_LEFT) . '-' . $acity; } if ($bidx !== false) { $bcity = str_pad($bidx, 4, "0", STR_PAD_LEFT) . '-' . $bcity; } $acity .= '-' . mb_strtolower($a['name']); $bcity .= '-' . mb_strtolower($b['name']); return $acity < $bcity ? -1 : 1; }); } return $machines; } function add_parcelmachine_checkout_fields($fragments) { $this->order_country = WC()->customer->get_shipping_country(); $res = ''; $res .= '
'; echo $res; } function check_parcelmachine_checkout_fields() { static $added1 = false; static $added2 = false; $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false; if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); } if (!$added1 && $shipping_method_ext[0] === $this->id && empty($_POST[$shipping_method_ext[0]])) { wc_add_notice(__('Parcel machine is a required field.', 'wc_makecommerce_domain'), 'error'); $added1 = true; } if (!$added2 && $shipping_method_ext[0] === $this->id && !$this->valid_phone_number($_POST['billing_phone'])) { wc_add_notice(__('Parcel machine can be used with local phone number only. Please specify your phone number with international access code (like +372xxxxxxx)', 'wc_makecommerce_domain'), 'error'); $added2 = true; } } function valid_phone_number($phone) { $phone = trim($phone); $phone = trim($phone, '+'); // Estonia for all if (substr($phone, 0, 4) === '3725' || substr($phone, 0, 1) === '5') { return true; } // Latvia for Omniva if ($this->carrier === 'omniva' && substr($phone, 0, 3) === '371') { return true; } // Lithuania for Omniva if ($this->carrier === 'omniva' && substr($phone, 0, 3) === '370') { return true; } // Finland for Smartpost if ($this->carrier === 'smartpost' && substr($phone, 0, 3) === '358') { return true; } return false; } function add_parcelmachine_order_meta($order_id) { $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false; if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); } if ($shipping_method_ext[0] === $this->id && !empty($_POST[$this->id])) { list($carrier, $machine) = explode('||', $_POST[$this->id]); $machine = mk_get_machine($carrier, $machine); if (!empty($machine['id'])) { update_post_meta($order_id, '_shipping_first_name', get_post_meta($order_id, '_billing_first_name', true)); update_post_meta($order_id, '_shipping_last_name', get_post_meta($order_id, '_billing_last_name', true)); update_post_meta($order_id, '_shipping_address_1', sanitize_text_field($machine['name'])); update_post_meta($order_id, '_shipping_address_2', sanitize_text_field($machine['address'])); update_post_meta($order_id, '_shipping_city', sanitize_text_field($machine['city'])); update_post_meta($order_id, '_shipping_postcode', ''); update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id])); } } } } $enable = get_option('mk_transport_apt_omniva', 'yes'); if ($enable === 'yes') { class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method { function __construct($instance_id = 0) { $this->ext = 'Omniva'; $this->name_ext = 'Omniva'; $this->carrier = 'omniva'; $this->type = 'apt'; parent::__construct($instance_id); if (is_admin()) { add_action( 'wp_ajax_verify_feature_swc', array(&$this, 'mc_check_feature_swc') ); } } public function mc_check_feature_swc() { $status = $this->mc_check_feature('shipments_without_credentials'); wp_send_json(array('feature_name' => 'shipments_without_credentials', 'feature_status' => $status)); exit; } public function mc_check_feature($feature_name = '') { $feature_enabled = false; $request_params = array( 'environment' => json_encode(array( 'platform' => 'woocommerce '. WC()->version, 'module' => 'makecommerce feature_staus_check', )), ); $MK = mk_get_api(); if (!$MK) { return false; } try { $shopConfig = $MK->getShopConfig($request_params); $features = $shopConfig->features; } catch (Exception $e) { error_log(print_r($e, 1)); return false; } if(isset($features)) { foreach($features as $feature) { if ($feature->name == $feature_name) { $feature_enabled = $feature->enabled; } } } return $feature_enabled; } function init_form_fields() { $this->instance_form_fields = array(); $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html()); $this->instance_form_fields['price'] = array( 'title' => __('Shipping price', 'wc_makecommerce_domain'), 'type' => 'text', 'default' => '5.00' ); $this->instance_form_fields['free_shipping_min_amount'] = array( 'title' => __('Free shipping amount', 'wc_makecommerce_domain'), 'type' => 'number', 'default' => '0', 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'), ); $this->form_fields = array(); $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html()); $this->form_fields['generic'] = array( 'type' => 'title', 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'), 'description' => __('You can always exclude a product from being available for the Parcel Machine delivery by clicking "Does not fit parcel machine" in the product\'s shipping options.', 'wc_makecommerce_domain').''; echo ''; echo ''; echo '
'; echo ''.$carrier.'
'.$machine['name'] . '
' . $machine['address'] . '
'.__('Delivery time', 'wc_makecommerce_domain').' - 09:00..17:00
'; } if ($delivery_time === '2') { echo ''.__('Delivery time', 'wc_makecommerce_domain').' - 17:00..21:00
'; } $shipment_id = get_post_meta($order_id, '_parcel_machine_shipment_id', true); $shipment_id_error = get_post_meta($order_id, '_parcel_machine_error', true); $shipment_manifest = get_post_meta($order_id, '_parcel_machine_manifest', true); if ($shipment_id) { switch ($carrier) { case 'omniva': echo ''.__('Shipment tracking code', 'wc_makecommerce_domain').' ('. $carrier .'):
'.$shipment_id.'' ;
break;
case 'dpd':
echo '
'.__('Shipment tracking code', 'wc_makecommerce_domain').' ('. $carrier .'):
'.$shipment_id.'' ;
break;
default:
echo '
'.__('Shipment tracking code', 'wc_makecommerce_domain').':
' . $shipment_id . '
'.__('Shipments pickup manifest').':
link
'.__('Shipment registration error').':
' . $shipment_id_error . '