PluginProbe
MakeCommerce for WooCommerce / 2.4.9
MakeCommerce for WooCommerce v2.4.9
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / makecommerce-transport.php

makecommerce-transport.php in MakeCommerce for WooCommerce 2.4.9, at makecommerce-transport.php

2,141 lines 95.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Parcel machine specific stuff
4 function transport_add_method() {
5
6 // Delete all the wc_ship transient scum, you aren’t wanted around here, move along.
7 // Same as being in shipping debug mode
8 global $wpdb;
9 $transients = $wpdb->get_col("SELECT option_name FROM $wpdb->options WHERE option_name LIKE '_transient_wc_ship%'");
10 if (count($transients)) {
11 foreach ($transients as $tr) {
12 $hash = substr($tr, 11);
13 delete_transient($hash);
14 }
15 }
16 $transient_value = get_transient('shipping-transient-version');
17 WC_Cache_Helper::delete_version_transients( $transient_value );
18 if (WC()->session) {
19 WC()->session->set('shipping_for_package', '');
20 }
21
22
23 if ( ! class_exists( 'WC_ParcelMachine_Shipping_Method' ) ) {
24
25 class WC_ParcelMachine_Shipping_Method extends WC_Shipping_Method {
26
27 function __construct($instance_id = 0) {
28 if (!$this->ext) {
29 throw new Exception('Do not call this class directly!');
30 }
31 $this->id = 'parcelmachine_' . mb_strtolower($this->ext);
32 $this->instance_id = absint($instance_id);
33 $this->method_title = $this->name_ext . ' ' . __(' Parcel Machine (MC)', 'wc_makecommerce_domain');
34 // $this->method_description = sprintf(__('Parcel machine transport method for %s', 'wc_makecommerce_domain'), $this->ext);
35 $this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal', 'settings');
36 $this->init();
37 $this->check_updates();
38 }
39
40 function check_updates() {
41 if (($cur_ver = get_site_option('wc_mc_version', 0)) < WC_MC_VERSION) {
42 update_site_option('wc_mc_version', WC_MC_VERSION);
43 error_log("Need to update from [{$cur_ver}] to [".WC_MC_VERSION."]");
44 if (WC_MC_VERSION === 2.0) {
45 // Check if omniva / smartpost is enabled and in which countries
46 // Convert to shipping zone
47 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) {
48 $transport_class_omniva = new $method_class();
49 if ($transport_class_omniva->enable === 'yes') {
50 $zones = WC_Shipping_Zones::get_zones();
51 if (!empty($transport_class_omniva->countries)) { continue; }
52 foreach ($transport_class_omniva->countries as $ecountry) {
53 $has_ecountry = false;
54 foreach ($zones as $zone) {
55 foreach ($zone['zone_locations'] as $location) {
56 if ($location->code === $ecountry) {
57 $has_ecountry = $zone['zone_id'];
58 foreach ($zone['shipping_methods'] as $method) {
59 if ($method->id === $method_name) {
60 continue 4;
61 }
62 }
63 }
64 }
65 }
66 if (!$has_ecountry) {
67 $zone = new WC_Shipping_Zone();
68 $zone->set_zone_name($ecountry);
69 $zone->add_location($ecountry, 'country');
70 $zone->save();
71 } else {
72 $zone = new WC_Shipping_Zone($has_ecountry);
73 }
74 $instance_id = $zone->add_shipping_method($method_name);
75 $transport_class = new $method_class($instance_id);
76 $price = !empty($transport_class->settings['price_'.strtolower($ecountry)]) ? $transport_class->settings['price_'.strtolower($ecountry)] : 0;
77 $free_shipping_min_amount = !empty($transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)]) ? $transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)] : 0;
78 $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));
79 $transport_class->process_admin_options();
80 }
81 }
82 }
83 }
84 }
85 }
86
87 function init() {
88 $this->init_form_fields();
89 $this->init_settings();
90
91 $this->enable = $this->settings['active'];
92 $this->title = !empty($this->settings['method_name']) ? $this->settings['method_name'] : '';
93 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) {
94 $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE];
95 }
96 if(!$this->title) {
97 $this->title = $this->method_title;
98 }
99 $this->availability = 'specific';
100 $this->countries = !empty($this->settings['countries']) ? $this->settings['countries'] : array();
101 $this->prioritization = $this->settings['prioritization'];
102 $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0;
103 $this->maximum_weight = (double)$this->settings['maximum_weight'];
104 $this->short_office_names = $this->settings['short_office_names'];
105 $this->order_country = 'unknown';
106 add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options'));
107 add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_parcelmachine_checkout_fields'));
108 add_action('woocommerce_checkout_process', array(&$this, 'check_parcelmachine_checkout_fields'));
109 add_action('woocommerce_after_checkout_validation', array(&$this, 'check_parcelmachine_checkout_fields'));
110 add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_parcelmachine_order_meta'));
111
112 // Woocommerce Multilingual overrides
113 if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) {
114 add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50);
115 add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2);
116 }
117 }
118
119 public function override_label_translation($rates) {
120 $id_instance = $this->id . ':' . $this->instance_id;
121 if(array_key_exists($id_instance, $rates)) {
122 $rates[$id_instance]->label = $this->title;
123 }
124 return $rates;
125 }
126
127 public function override_shipping_title_translation($items, $order) {
128 foreach ($items as $key => $item) {
129 if($item['type'] == 'shipping') {
130 foreach ($item['item_meta_array'] as $meta) {
131 if ($meta->key === 'method_id') {
132 $tmp = explode(':', $meta->value);
133 if ($tmp[0] == $this->id) {
134 $items[$key]['name'] = $this->title;
135 }
136 }
137 }
138 }
139 }
140 return $items;
141 }
142
143 public function generate_shipping_ui_javascript_html( $key, $data ) { ?>
144 <script type="text/javascript">
145 jQuery(document).ready(function($) {
146 var country_select = $('#woocommerce_parcelmachine_<?php echo strtolower($this->ext); ?>_countries');
147 var prices = [], free_shippings = [];
148 $.each(country_select.find('option'), function(i, option) {
149 prices[$(option).val()] = $('#woocommerce_parcelmachine_<?php echo strtolower($this->ext); ?>_price_' + $(option).val().toLowerCase()).closest('tr');
150 free_shippings[$(option).val()] = $('#woocommerce_parcelmachine_<?php echo strtolower($this->ext); ?>_free_shipping_min_amount_' + $(option).val().toLowerCase()).closest('tr');
151 });
152 country_select.on('change', function(){
153 var countries = $(this).val();
154 if(countries) {
155 hidePrices(countries);
156 }
157 });
158 hidePrices(country_select.val());
159 function hidePrices(countries) {
160 for (var key in prices) {
161 if(countries.indexOf(key) == -1){
162 prices[key].hide();
163 free_shippings[key].hide();
164 } else {
165 prices[key].show();
166 free_shippings[key].show();
167 }
168 }
169 }
170 });
171 </script>
172 <?php }
173
174 function calculate_shipping($package = array()) {
175 $price = isset($this->instance_settings['price']) ? $this->instance_settings['price'] : (isset($this->settings['price_'.$package['destination']['country']]) ? $this->settings['price_'.$package['destination']['country']] : 0);
176
177 $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount'];
178
179 if ($free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount) {
180 $price = 0;
181 }
182
183 $free_shipping = true;
184 foreach ($package['contents'] as $line) {
185 $free_shipping = get_post_meta($line['product_id'], '_no_shipping_cost', true) === 'yes' ? $free_shipping : false;
186 }
187 if($free_shipping) {
188 $price = 0;
189 }
190 $rate = array(
191 'id' => $this->get_rate_id(),
192 'label' => $this->title,
193 'cost' => $price,
194 'package' => $package,
195 'calc_tax' => 'per_order',
196 );
197 $this->add_rate( $rate );
198 }
199
200 function calculate_weight($package) {
201 $weight = 0;
202 foreach ($package['contents'] as $line) {
203 $weight += (double)$line['data']->get_weight();
204 }
205 return $weight;
206 }
207
208 function fits_parcel_machine($package) {
209 foreach ($package['contents'] as $line) {
210 if (get_post_meta($line['product_id'], '_no_parcel_machine', true) === 'yes') {
211 return false;
212 }
213 }
214 return true;
215 }
216
217 function is_available($package) {
218 if (!$this->fits_parcel_machine($package)) {
219 return false;
220 }
221 $package_weight = $this->calculate_weight($package);
222 if ($package_weight >= $this->maximum_weight) {
223 return false;
224 }
225 $is_available = $this->enable === 'yes';
226 if (!$is_available || !mk_is_api_set()) {
227 return false;
228 }
229 //if (is_array($this->countries) && !in_array($package['destination']['country'], $this->countries)) {
230 // $is_available = false;
231 //}
232 $this->order_country = $package['destination']['country'];
233 return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package);
234 }
235
236 function mk_get_machines() {
237 $machines = mk_get_machines($this->ext, $this->order_country);
238 if ($this->prioritization === 'yes') {
239 usort($machines, function($a, $b) {
240 $sortorder = array(
241 'tallinn', 'tartu linn','tartu', 'narva', 'pärnu linn','pärnu', 'viljandi', 'kohtla-järve', 'rakvere', 'maardu', 'sillamäe', 'kuressaare',
242 'helsinki', 'espoo', 'tampere', 'vantaa', 'oulu', 'turku', 'jüväskülä', 'lahti', 'kuopio', 'kouvola',
243 'rīga','riga', 'daugavpils', 'liepāja','liepaja', 'jelgava', 'jūrmala','jurmala', 'ventspils', 'rezekne', 'valmiera', 'jekabpils',
244 'vilnius', 'kaunas', 'klaipeda', 'siauliai', 'panevezys', 'alytus', 'mariampole', 'mazeikiai', 'jonava', 'utena'
245 );
246 $acity = mb_strtolower($a['city']);
247 $bcity = mb_strtolower($b['city']);
248 if (!$acity) { $acity = 'xxxxxxx'; }
249 if (!$bcity) { $bcity = 'xxxxxxx'; }
250 $aidx = array_search($acity, $sortorder);
251 $bidx = array_search($bcity, $sortorder);
252 if ($aidx !== false) {
253 $acity = str_pad($aidx, 4, "0", STR_PAD_LEFT) . '-' . $acity;
254 }
255 if ($bidx !== false) {
256 $bcity = str_pad($bidx, 4, "0", STR_PAD_LEFT) . '-' . $bcity;
257 }
258 $acity .= '-' . mb_strtolower($a['name']);
259 $bcity .= '-' . mb_strtolower($b['name']);
260 return $acity < $bcity ? -1 : 1;
261 });
262 }
263 return $machines;
264 }
265
266 function add_parcelmachine_checkout_fields($fragments) {
267 $this->order_country = WC()->customer->get_shipping_country();
268 $res = '';
269 $res .= '<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_parcelmachine_'.mb_strtolower($this->ext).'">';
270 // echo '<th>' . $this->title . '</th>';
271 // echo '<td>';
272 $res .= '<td colspan="2">';
273 $options = array();
274 $machines = $this->mk_get_machines();
275 $res .= '<p class="form-row" id="'.esc_attr($this->id).'_field">';
276 $res .= '<select class="select" name="'.esc_attr($this->id).'" id="'.esc_attr($this->id).'">';
277 $res .= '<option value="">'.__('-- select parcel machine --', 'wc_makecommerce_domain').'</option>';
278 $pcity = false;
279 foreach ($machines as $machine) {
280 $city = strtolower($machine['city']);
281 if ($city !== $pcity) {
282 if ($pcity) $res .= '</optgroup>';
283 $res .= '<optgroup label="'.$machine['city'].'">';
284 }
285 $mname = $machine['name'];
286 if ($this->short_office_names !== 'yes') $mname .= ' - ' . $machine['city'] . ', ' . $machine['address'];
287 $res .= '<option value="'.esc_attr($machine['carrier'].'||'.$machine['id']).'">'.$mname.'</option>';
288 $pcity = $city;
289 }
290 if ($pcity) $res .= '</optgroup>';
291 $res .= '</select>';
292 $res .= '</p>';
293 $res .= '</td></tr>';
294 echo $res;
295 }
296
297 function check_parcelmachine_checkout_fields() {
298 static $added1 = false;
299 static $added2 = false;
300 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
301 if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); }
302 if (!$added1 && $shipping_method_ext[0] === $this->id && empty($_POST[$shipping_method_ext[0]])) {
303 wc_add_notice(__('<strong>Parcel machine</strong> is a required field.', 'wc_makecommerce_domain'), 'error');
304 $added1 = true;
305 }
306 if (!$added2 && $shipping_method_ext[0] === $this->id && !$this->valid_phone_number($_POST['billing_phone'])) {
307 wc_add_notice(__('<strong>Parcel machine</strong> can be used with local phone number only. Please specify your phone number with international access code (like +372xxxxxxx)', 'wc_makecommerce_domain'), 'error');
308 $added2 = true;
309 }
310 }
311
312 function valid_phone_number($phone) {
313 $phone = trim($phone);
314 $phone = trim($phone, '+');
315 // Estonia for all
316 if (substr($phone, 0, 4) === '3725' || substr($phone, 0, 1) === '5') { return true; }
317 // Latvia for Omniva
318 if ($this->carrier === 'omniva' && substr($phone, 0, 3) === '371') { return true; }
319 // Lithuania for Omniva
320 if ($this->carrier === 'omniva' && substr($phone, 0, 3) === '370') { return true; }
321 // Finland for Smartpost
322 if ($this->carrier === 'smartpost' && substr($phone, 0, 3) === '358') { return true; }
323 return false;
324 }
325
326 function add_parcelmachine_order_meta($order_id) {
327 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
328 if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); }
329 if ($shipping_method_ext[0] === $this->id && !empty($_POST[$this->id])) {
330 list($carrier, $machine) = explode('||', $_POST[$this->id]);
331 $machine = mk_get_machine($carrier, $machine);
332 if (!empty($machine['id'])) {
333 update_post_meta($order_id, '_shipping_first_name', get_post_meta($order_id, '_billing_first_name', true));
334 update_post_meta($order_id, '_shipping_last_name', get_post_meta($order_id, '_billing_last_name', true));
335 update_post_meta($order_id, '_shipping_address_1', sanitize_text_field($machine['name']));
336 update_post_meta($order_id, '_shipping_address_2', sanitize_text_field($machine['address']));
337 update_post_meta($order_id, '_shipping_city', sanitize_text_field($machine['city']));
338 update_post_meta($order_id, '_shipping_postcode', '');
339 update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id]));
340 }
341 }
342 }
343 }
344
345
346
347 $enable = get_option('mk_transport_apt_omniva', 'yes');
348 if ($enable === 'yes') {
349 class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method {
350 function __construct($instance_id = 0) {
351 $this->ext = 'Omniva';
352 $this->name_ext = 'Omniva';
353 $this->carrier = 'omniva';
354 $this->type = 'apt';
355 parent::__construct($instance_id);
356 if (is_admin()) {
357 add_action( 'wp_ajax_verify_feature_swc', array(&$this, 'mc_check_feature_swc') );
358 }
359
360 }
361
362
363 public function mc_check_feature_swc() {
364 $status = $this->mc_check_feature('shipments_without_credentials');
365 wp_send_json(array('feature_name' => 'shipments_without_credentials', 'feature_status' => $status));
366 exit;
367 }
368
369
370 public function mc_check_feature($feature_name = '') {
371
372 $feature_enabled = false;
373
374 $request_params = array(
375 'environment' => json_encode(array(
376 'platform' => 'woocommerce '. WC()->version,
377 'module' => 'makecommerce feature_staus_check',
378 )),
379 );
380
381 $MK = mk_get_api();
382 if (!$MK) {
383 return false;
384 }
385
386 try {
387 $shopConfig = $MK->getShopConfig($request_params);
388 $features = $shopConfig->features;
389 } catch (Exception $e) {
390 error_log(print_r($e, 1));
391 return false;
392 }
393
394 if(isset($features)) {
395 foreach($features as $feature) {
396 if ($feature->name == $feature_name) {
397 $feature_enabled = $feature->enabled;
398 }
399
400 }
401 }
402
403 return $feature_enabled;
404 }
405
406
407 function init_form_fields() {
408
409 $this->instance_form_fields = array();
410 $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
411 $this->instance_form_fields['price'] = array(
412 'title' => __('Shipping price', 'wc_makecommerce_domain'),
413 'type' => 'text',
414 'default' => '5.00'
415 );
416 $this->instance_form_fields['free_shipping_min_amount'] = array(
417 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
418 'type' => 'number',
419 'default' => '0',
420 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
421 );
422
423 $this->form_fields = array();
424 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
425 $this->form_fields['generic'] = array(
426 'type' => 'title',
427 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
428 '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').'<br>'.__('You can mark there also a product as "Free shipping in parcel machine."', 'wc_makecommerce_domain'),
429 );
430 $this->form_fields['active'] = array(
431 'title' => __('Enable', 'wc_makecommerce_domain'),
432 'type' => 'checkbox',
433 'label' => __('enabled', 'wc_makecommerce_domain'),
434 'default' => 'no',
435 );
436 $this->form_fields['maximum_weight'] = array(
437 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
438 'type' => 'text',
439 'default' => '30'
440 );
441
442 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain'));
443
444 if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
445 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
446 }
447 else if ( defined( 'POLYLANG_VERSION' ) ) {
448 $pl_locales = pll_languages_list( array('fields'=>'locale') );
449 foreach ($pl_locales as $key => $locale_pl ) {
450 $language = array( 'id' => $key,
451 'code' => $locale_pl) ;
452 $languages[$locale_pl] = $language;
453 }
454 }
455
456 if (empty($languages)) {
457 $this->form_fields['method_name'] = array(
458 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
459 'type' => 'text',
460 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
461 );
462 } else {
463 foreach ($languages as $language_code => $language) {
464 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
465 $defaultName = __('Omniva Parcel Machine', 'wc_makecommerce_domain');
466 switch ($language_code) {
467 case 'et':
468 $defaultName = 'Omniva pakiautomaat';
469 break;
470 case 'ru':
471 $defaultName = 'Omniva посылочный автомат';
472 break;
473 case 'fi':
474 $defaultName = 'Omniva pakettiautomaatti';
475 break;
476 case 'lv':
477 $defaultName = 'Omniva Pakomāts';
478 break;
479 }
480 $this->form_fields['method_name_'.$language_code] = array(
481 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
482 'type' => 'text',
483 'default' => $defaultName
484 );
485 }
486 }
487 $this->form_fields['prioritization'] = array(
488 'title' => __('Prioritize', 'wc_makecommerce_domain'),
489 'type' => 'checkbox',
490 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
491 'default' => 'yes'
492 );
493 $this->form_fields['short_office_names'] = array(
494 'title' => __('Short names', 'wc_makecommerce_domain'),
495 'type' => 'checkbox',
496 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
497 'default' => 'no'
498 );
499
500 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => __('You can automatically register the shipments into Omniva system and print out the package labels right here, at the shop orders view. <br> (see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin homepage</a>.)', 'wc_makecommerce_domain'). '<br><br>' .__('If you do not have contract with Ominva, no worries - you can send the parcels using MakeCommerce transport mediation service.<br>The cost of transport will be deducted on your Makecommerce account. See more info and pricing <a href="https://makecommerce.net/service/delivery/">here</a> ', 'wc_makecommerce_domain').'<br>'.__('The service can be enabled in', 'wc_makecommerce_domain').' <a href="https://merchant-test.maksekeskus.ee/features.html">'.__('Merchant Portal', 'wc_makecommerce_domain').'</a>');
501
502 $this->form_fields['use_mk_contract'] = array(
503 'type' => 'select',
504 'title' => __('Contract', 'wc_makecommerce_domain'),
505 'options' => array(
506 false => __('use my own Omniva contract', 'wc_makecommerce_domain'),
507 true => __('use MakeCommerce transport mediation service', 'wc_makecommerce_domain'),
508 ),
509 'default' => false,
510 'description' => '',
511 );
512 $this->form_fields['verify_feature_swc'] = array(
513 'type' => 'verify_feature_swc',
514 'title' => __('Verify service status', 'wc_makecommerce_domain'),
515 'description' => __('You must enable the Transport mediation service before using it.', 'wc_makecommerce_domain'),
516 'desc_tip' => __('This will check if the Transport mediation service has been enabled for your shop.', 'wc_makecommerce_domain'),
517 'placeholder' => __('Verify', 'wc_makecommerce_domain'),
518 );
519 $this->form_fields['service_user'] = array(
520 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
521 'type' => 'text',
522 'default' => ''
523 );
524 $this->form_fields['service_password'] = array(
525 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
526 'type' => 'text',
527 'default' => ''
528 );
529 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => '<br><br>'.__('Return address', 'wc_makecommerce_domain'), 'description' => __('Return address is printed on parcel labels, and is also used when customer wants to return the parcel', 'wc_makecommerce_domain'));
530 $this->form_fields['shop_name'] = array(
531 'type' => 'text',
532 'title' => __('Shop name', 'wc_makecommerce_domain'),
533 'class' => 'input-text regular-input',
534 );
535 $this->form_fields['shop_phone'] = array(
536 'type' => 'text',
537 'title' => __('Shop phone', 'wc_makecommerce_domain'),
538 'title' => __('Shop phone', 'wc_makecommerce_domain'),
539 'class' => 'input-text regular-input',
540 );
541 $this->form_fields['shop_email'] = array(
542 'type' => 'text',
543 'title' => __('Shop email', 'wc_makecommerce_domain'),
544 'class' => 'input-text regular-input',
545 );
546 $this->form_fields['shop_address_country'] = array(
547 'type' => 'select',
548 'title' => __('Shop address country', 'wc_makecommerce_domain'),
549 'options' => array(
550 'EE' => 'EE',
551 'LV' => 'LV',
552 'LT' => 'LT',
553 ),
554 'default' => 'EE',
555 );
556 $this->form_fields['shop_address_city'] = array(
557 'type' => 'text',
558 'title' => __('Shop address city', 'wc_makecommerce_domain'),
559 'class' => 'input-text regular-input',
560 );
561 $this->form_fields['shop_address_street'] = array(
562 'type' => 'text',
563 'title' => __('Shop address street', 'wc_makecommerce_domain'),
564 'class' => 'input-text regular-input',
565 );
566 $this->form_fields['shop_postal_code'] = array(
567 'type' => 'text',
568 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
569 'class' => 'input-text regular-input',
570 );
571 $this->form_fields['shipping_ui_javascript'] = array(
572 'type' => 'shipping_ui_javascript',
573 );
574
575 }
576
577 public function generate_verify_feature_swc_html( $key, $data ) {
578 $field = $this->get_field_key( $key );
579 $defaults = array(
580 'title' => '',
581 'disabled' => false,
582 'class' => '',
583 'css' => '',
584 'placeholder' => '',
585 'type' => 'text',
586 'desc_tip' => false,
587 'description' => '',
588 'custom_attributes' => array()
589 );
590
591 $data = wp_parse_args( $data, $defaults );
592
593 ob_start();
594 ?>
595
596 <tr valign="top">
597 <th scope="row" class="titledesc">
598 <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
599 <?php echo $this->get_tooltip_html( $data ); ?>
600 </th>
601 <td class="forminp">
602 <fieldset>
603 <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
604 <input id="verify_feature_swc" class="button <?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $data['placeholder'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> />
605 <!-- <br><p><?php echo esc_attr( $data['description'] ); ?></p> -->
606 <script type="text/javascript">
607 jQuery('input#verify_feature_swc').on('click', function() {
608 init_mc_loading();
609 jQuery.ajax({
610 url: '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php',
611 type: 'POST',
612 data: 'action=verify_feature_swc',
613 success: function (output) {
614 if(output) {
615 if (output.feature_status == true) {
616 alert('<?php echo __('There Transport Mediation service is already enabled for your Shop. \n You are good to go!', 'wc_makecommerce_domain'); ?>');
617 } else {
618 alert('<?php echo __('There Transport Mediation service is NOT ENABLED enabled for your Shop. \n Please go to Merchant Portal to activate it!', 'wc_makecommerce_domain'); ?>');
619 }
620
621 } else {
622 alert('<?php echo __('There was an error with your request. Please try again.', 'wc_makecommerce_domain'); ?>');
623 }
624 },
625 complete: function() { stop_mc_loading(); }
626 });
627 });
628
629 function init_mc_loading() {
630 jQuery('input#verify_feature_swc').attr('disabled', 'disabled');
631 }
632
633 function stop_mc_loading() {
634 jQuery('input#verify_feature_swc').removeAttr('disabled');
635 }
636 </script>
637 </fieldset>
638 </td>
639 </tr>
640 <?php
641
642 return ob_get_clean();
643 }
644
645 }
646 }
647
648 $enable = get_option('mk_transport_apt_dpd', 'yes');
649 if ($enable === 'yes') {
650 class WC_ParcelMachine_Shipping_Method_DPD extends WC_ParcelMachine_Shipping_Method {
651 function __construct($instance_id = 0) {
652 $this->ext = 'DPD';
653 $this->name_ext = 'DPD';
654 $this->carrier = 'dpd';
655 $this->type = 'apt';
656 parent::__construct($instance_id);
657 $this->method_title = __('DPD Pickup Network', 'wc_makecommerce_domain').' (MC)';
658 }
659 function init_form_fields() {
660
661 $this->instance_form_fields = array();
662 $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
663 $this->instance_form_fields['price'] = array(
664 'title' => __('Shipping price', 'wc_makecommerce_domain'),
665 'type' => 'text',
666 'default' => '5.00'
667 );
668 $this->instance_form_fields['free_shipping_min_amount'] = array(
669 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
670 'type' => 'number',
671 'default' => '0',
672 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
673 );
674
675 $this->form_fields = array();
676 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
677 $this->form_fields['generic'] = array(
678 'type' => 'title',
679 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
680 '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').'<br>'.__('You can mark there also a product as "Free shipping in parcel machine."', 'wc_makecommerce_domain'),
681 );
682 $this->form_fields['active'] = array(
683 'title' => __('Enable', 'wc_makecommerce_domain'),
684 'type' => 'checkbox',
685 'label' => __('enabled', 'wc_makecommerce_domain'),
686 'default' => 'no',
687 );
688 $this->form_fields['maximum_weight'] = array(
689 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
690 'type' => 'text',
691 'default' => '30'
692 );
693
694 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain'));
695
696 if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
697 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
698 }
699 else if ( defined( 'POLYLANG_VERSION' ) ) {
700 $pl_locales = pll_languages_list( array('fields'=>'locale') );
701 foreach ($pl_locales as $key => $locale_pl ) {
702 $language = array( 'id' => $key,
703 'code' => $locale_pl) ;
704 $languages[$locale_pl] = $language;
705 }
706 }
707
708 if (empty($languages)) {
709 $this->form_fields['method_name'] = array(
710 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
711 'type' => 'text',
712 'default' => __('DPD Pickup Network', 'wc_makecommerce_domain')
713 );
714 } else {
715 foreach ($languages as $language_code => $language) {
716 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
717 $defaultName = __('DPD Pickup Network', 'wc_makecommerce_domain');
718 switch ($language_code) {
719 case 'et':
720 $defaultName = 'DPD Pickup võrgustik';
721 break;
722 case 'ru':
723 $defaultName = 'DPD Pickup сеть';
724 break;
725 case 'fi':
726 $defaultName = 'DPD Pickup verkko';
727 break;
728 case 'lv':
729 $defaultName = 'DPD Pickup tīkls';
730 break;
731 }
732 $this->form_fields['method_name_'.$language_code] = array(
733 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
734 'type' => 'text',
735 'default' => $defaultName
736 );
737 }
738 }
739 $this->form_fields['prioritization'] = array(
740 'title' => __('Prioritize', 'wc_makecommerce_domain'),
741 'type' => 'checkbox',
742 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
743 'default' => 'yes'
744 );
745 $this->form_fields['short_office_names'] = array(
746 'title' => __('Short names', 'wc_makecommerce_domain'),
747 'type' => 'checkbox',
748 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
749 'default' => 'no'
750 );
751 if (mk_wc_version("3.4.0")) $a = 'admin.php?page=wc-settings&tab=advanced&section=mk_api'; else $a = 'admin.php?page=wc-settings&tab=api&section=mk_api';
752 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => sprintf(__('You can automatically create shipments into DPD system, DPD will send parcel-labels automatically to your e-mail <br> Please set your InterConnect (IC) account credentials below here. <br>See more details on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>.', 'wc_makecommerce_domain'), admin_url($a)));
753 $this->form_fields['service_user'] = array(
754 'title' => __('DPD InterConnect username', 'wc_makecommerce_domain'),
755 'type' => 'text',
756 'default' => ''
757 );
758 $this->form_fields['service_password'] = array(
759 'title' => __('DPD InterConnect password', 'wc_makecommerce_domain'),
760 'type' => 'text',
761 'default' => ''
762 );
763 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => __('Return address', 'wc_makecommerce_domain'), 'description' => __('Please define return address for DPD shipments', 'wc_makecommerce_domain'));
764 $this->form_fields['shop_name'] = array(
765 'type' => 'text',
766 'title' => __('Shop name', 'wc_makecommerce_domain'),
767 'class' => 'input-text regular-input',
768 );
769 $this->form_fields['shop_phone'] = array(
770 'type' => 'text',
771 'title' => __('Shop phone', 'wc_makecommerce_domain'),
772 'class' => 'input-text regular-input',
773 );
774 $this->form_fields['shop_email'] = array(
775 'type' => 'text',
776 'title' => __('Shop email', 'wc_makecommerce_domain'),
777 'class' => 'input-text regular-input',
778 );
779 $this->form_fields['shop_address_country'] = array(
780 'type' => 'select',
781 'title' => __('Shop address country', 'wc_makecommerce_domain'),
782 'options' => array(
783 'EE' => 'EE',
784 'LV' => 'LV',
785 'LT' => 'LT',
786 ),
787 'default' => 'EE',
788 );
789 $this->form_fields['shop_address_city'] = array(
790 'type' => 'text',
791 'title' => __('Shop address city', 'wc_makecommerce_domain'),
792 'class' => 'input-text regular-input',
793 );
794 $this->form_fields['shop_address_street'] = array(
795 'type' => 'text',
796 'title' => __('Shop address street', 'wc_makecommerce_domain'),
797 'class' => 'input-text regular-input',
798 );
799 $this->form_fields['shop_postal_code'] = array(
800 'type' => 'text',
801 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
802 'class' => 'input-text regular-input',
803 );
804 $this->form_fields['shipping_ui_javascript'] = array(
805 'type' => 'shipping_ui_javascript',
806 );
807 }
808 }
809 }
810
811
812
813
814
815 $enable = get_option('mk_transport_apt_smartpost', 'yes');
816 if ($enable === 'yes') {
817 class WC_ParcelMachine_Shipping_Method_Smartpost extends WC_ParcelMachine_Shipping_Method {
818 function __construct($instance_id = 0) {
819 $this->ext = 'SmartPost';
820 $this->name_ext = 'SmartPOST';
821 $this->carrier = 'smartpost';
822 $this->type = 'apt';
823 parent::__construct($instance_id);
824 }
825 function init_form_fields() {
826
827 global $woocommerce;
828
829 $this->instance_form_fields = array();
830 $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
831
832 $this->instance_form_fields['price'] = array(
833 'title' => __('Shipping price', 'wc_makecommerce_domain'),
834 'type' => 'text',
835 'default' => '5.00'
836 );
837 $this->instance_form_fields['free_shipping_min_amount'] = array(
838 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
839 'type' => 'number',
840 'default' => '0',
841 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
842 );
843
844 $this->form_fields = array();
845 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
846 $this->form_fields['generic'] = array(
847 'type' => 'title',
848 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
849 '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').'<br>'.__('You can mark there also a product as "Free shipping in parcel machine."', 'wc_makecommerce_domain'),
850 );
851
852 $this->form_fields['active'] = array(
853 'title' => __('Enable', 'wc_makecommerce_domain'),
854 'type' => 'checkbox',
855 'label' => __('enabled', 'wc_makecommerce_domain'),
856 'default' => 'no',
857 );
858 $this->form_fields['maximum_weight'] = array(
859 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
860 'type' => 'text',
861 'default' => '30'
862 );
863
864 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on checkout page', 'wc_makecommerce_domain'));
865
866 if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
867 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
868 }
869 else if ( defined( 'POLYLANG_VERSION' ) ) {
870 $pl_locales = pll_languages_list( array('fields'=>'locale') );
871 foreach ($pl_locales as $key => $locale_pl ) {
872 $language = array( 'id' => $key,
873 'code' => $locale_pl) ;
874 $languages[$locale_pl] = $language;
875 }
876 }
877
878 if (empty($languages)) {
879 $this->form_fields['method_name'] = array(
880 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
881 'type' => 'text',
882 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
883 );
884 } else {
885 foreach ($languages as $language_code => $language) {
886 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
887 $defaultName = __('SmartPOST Parcel Machine', 'wc_makecommerce_domain');
888 switch ($language_code) {
889 case 'et':
890 $defaultName = 'SmartPOST pakiautomaat';
891 break;
892 case 'ru':
893 $defaultName = 'SmartPOST посылочный автомат';
894 break;
895 case 'fi':
896 $defaultName = 'SmartPOST pakettiautomaatti';
897 break;
898 case 'lv':
899 $defaultName = 'SmartPOST Pakomāts';
900 break;
901 }
902 $this->form_fields['method_name_'.$language_code] = array(
903 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
904 'type' => 'text',
905 'default' => $defaultName
906 );
907 }
908 }
909 $this->form_fields['prioritization'] = array(
910 'title' => __('Prioritize', 'wc_makecommerce_domain'),
911 'type' => 'checkbox',
912 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
913 'default' => 'yes'
914 );
915 $this->form_fields['short_office_names'] = array(
916 'title' => __('Short names', 'wc_makecommerce_domain'),
917 'type' => 'checkbox',
918 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
919 'default' => 'no'
920 );
921 if (mk_wc_version("3.4.0")) $a = 'admin.php?page=wc-settings&tab=advanced&section=mk_api'; else $a = 'admin.php?page=wc-settings&tab=api&section=mk_api';
922 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => sprintf(__('You can automatically create shipments into smartpost.ee system and print the out the package labels right here, at the shop orders view. <br> Please set your smartpost.ee account credentials below here. <br>(See more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain'), admin_url($a)));
923 $this->form_fields['service_user'] = array(
924 'title' => __('eteenindus.smartpost.ee username', 'wc_makecommerce_domain'),
925 'type' => 'text',
926 'default' => ''
927 );
928 $this->form_fields['service_password'] = array(
929 'title' => __('eteenindus.smartpost.ee password', 'wc_makecommerce_domain'),
930 'type' => 'text',
931 'default' => ''
932 );
933 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => __('Return address', 'wc_makecommerce_domain'), 'description' => __('Please define return address for SmartPOST shipments (used on parcel labels)', 'wc_makecommerce_domain'));
934 $this->form_fields['shop_name'] = array(
935 'type' => 'text',
936 'title' => __('Shop name', 'wc_makecommerce_domain'),
937 'class' => 'input-text regular-input',
938 );
939 $this->form_fields['shop_phone'] = array(
940 'type' => 'text',
941 'title' => __('Shop phone', 'wc_makecommerce_domain'),
942 'class' => 'input-text regular-input',
943 );
944 $this->form_fields['shop_email'] = array(
945 'type' => 'text',
946 'title' => __('Shop email', 'wc_makecommerce_domain'),
947 'class' => 'input-text regular-input',
948 );
949 $this->form_fields['shipping_ui_javascript'] = array(
950 'type' => 'shipping_ui_javascript',
951 );
952 }
953 }
954 }
955
956 }
957
958 if ( ! class_exists( 'WC_Courier_Shipping_Method' ) ) {
959
960 class WC_Courier_Shipping_Method extends WC_Shipping_Method {
961
962 public function __construct($instance_id = 0) {
963 $this->id = 'courier_'.$this->carrier;
964 $this->type = 'cou';
965 $this->instance_id = absint($instance_id);
966 $this->method_title = $this->name_ext .' '.__('courier (MC)', 'wc_makecommerce_domain');
967 // $this->method_description = sprintf(__('Courier transport method for %s', 'wc_makecommerce_domain'), $this->ext);
968 $this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal', 'settings');
969 $this->init();
970 }
971
972 public function init() {
973 $this->init_form_fields();
974 $this->init_settings();
975
976 $this->enable = $this->settings['active'];
977 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) {
978 $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE];
979 } else if (!empty($this->settings['method_name'])) {
980 $this->title = $this->settings['method_name'];
981 }
982 if (empty($this->title)) {
983 $this->title = $this->method_title;
984 }
985 $this->availability = 'specific';
986 $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0;
987 $this->maximum_weight = (double)$this->settings['maximum_weight'];
988 $this->order_country = 'unknown';
989 add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options'));
990 // !!! Commented out for now
991 // add_filter('woocommerce_order_shipping_to_display_shipped_via', array(&$this, 'add_admin_courier_via_field'));
992 // add_filter('woocommerce_order_shipping_method', array(&$this, 'add_admin_courier_via_field'));
993
994 // Woocommerce Multilingual overrides
995 if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) {
996 add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50);
997 add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2);
998 }
999 }
1000
1001 public function override_label_translation($rates) {
1002 $id_instance = $this->id . ':' . $this->instance_id;
1003 if(array_key_exists($id_instance, $rates)) {
1004 $rates[$id_instance]->label = $this->title;
1005 }
1006 return $rates;
1007 }
1008
1009 public function override_shipping_title_translation($items, $order) {
1010 foreach ($items as $key => $item) {
1011 if($item['type'] == 'shipping') {
1012 foreach ($item['item_meta_array'] as $meta) {
1013 if ($meta->key === 'method_id') {
1014 $tmp = explode(':', $meta->value);
1015 if ($tmp[0] == $this->id) {
1016 $items[$key]['name'] = $this->title;
1017 }
1018 }
1019 }
1020 }
1021 }
1022 return $items;
1023 }
1024
1025 public function calculate_shipping($package = array()) {
1026
1027 $price = $this->instance_settings['price'];
1028
1029 $free_shipping_min_amount = $this->instance_settings['free_shipping_min_amount'];
1030
1031 if ($free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount) {
1032 $price = 0;
1033 }
1034
1035 $rate = array(
1036 'id' => $this->get_rate_id(),
1037 'label' => $this->title,
1038 'cost' => $price,
1039 'package' => $package,
1040 'calc_tax' => 'per_order',
1041 );
1042 $this->add_rate( $rate );
1043 }
1044
1045 public function calculate_weight($package) {
1046 $weight = 0;
1047 foreach ($package['contents'] as $line) {
1048 $weight += (double)$line['data']->get_weight();
1049 }
1050 return $weight;
1051 }
1052
1053 public function is_available($package) {
1054
1055 $package_weight = $this->calculate_weight($package);
1056 if ($package_weight >= $this->maximum_weight) {
1057 return false;
1058 }
1059 $is_available = $this->enable === 'yes';
1060 if (!$is_available || !mk_is_api_set()) {
1061 return false;
1062 }
1063
1064 $this->order_country = $package['destination']['country'];
1065 return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package);
1066 }
1067
1068 public function add_admin_courier_via_field($order) {
1069 return ' <small>(via ' . $this->name_ext . ' ' . __('Courier', 'wc_makecommerce_domain') . ')</small>';
1070 }
1071
1072 }
1073
1074
1075 $enable = get_option('mk_transport_courier_omniva', 'yes');
1076 if ($enable === 'yes') {
1077 class WC_Courier_Shipping_Method_Omniva extends WC_Courier_Shipping_Method {
1078
1079 public function __construct($instance_id = 0) {
1080 $this->ext = 'Omniva';
1081 $this->name_ext = 'Omniva';
1082 $this->carrier = 'omniva';
1083 parent::__construct($instance_id);
1084 }
1085
1086 public function init_form_fields() {
1087
1088 global $woocommerce;
1089
1090 $this->instance_form_fields = array();
1091 $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1092 $this->instance_form_fields['price'] = array(
1093 'title' => __('Shipping price', 'wc_makecommerce_domain'),
1094 'type' => 'text',
1095 'default' => '4.95'
1096 );
1097 $this->instance_form_fields['free_shipping_min_amount'] = array(
1098 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
1099 'type' => 'number',
1100 'default' => '0',
1101 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
1102 );
1103
1104 $this->form_fields = array();
1105 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1106 $this->form_fields['generic'] = array(
1107 'type' => 'title',
1108 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
1109 // 'description' => __('', 'wc_makecommerce_domain'),
1110 );
1111 $this->form_fields['active'] = array(
1112 'title' => __('Enable', 'wc_makecommerce_domain'),
1113 'type' => 'checkbox',
1114 'label' => __('enabled', 'wc_makecommerce_domain'),
1115 'default' => 'no',
1116 );
1117 $this->form_fields['maximum_weight'] = array(
1118 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
1119 'type' => 'text',
1120 'default' => '60'
1121 );
1122
1123 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain'));
1124
1125 if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
1126 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1127 }
1128 else if ( defined( 'POLYLANG_VERSION' ) ) {
1129 $pl_locales = pll_languages_list( array('fields'=>'locale') );
1130 foreach ($pl_locales as $key => $locale_pl ) {
1131 $language = array( 'id' => $key,
1132 'code' => $locale_pl) ;
1133 $languages[$locale_pl] = $language;
1134 }
1135 }
1136
1137 if (empty($languages)) {
1138 $this->form_fields['method_name'] = array(
1139 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1140 'type' => 'text',
1141 'default' => __('Omniva Courier', 'wc_makecommerce_domain')
1142 );
1143 } else {
1144 foreach ($languages as $language_code => $language) {
1145 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1146 $defaultName = __('Omniva Courier', 'wc_makecommerce_domain');
1147 switch ($language_code) {
1148 case 'et':
1149 $defaultName = 'Omniva kuller';
1150 break;
1151 case 'ru':
1152 $defaultName = 'Omniva Курьер';
1153 break;
1154 case 'fi':
1155 $defaultName = 'Omniva kuriiri';
1156 break;
1157 case 'lv':
1158 $defaultName = 'Omniva kurjers';
1159 break;
1160 case 'lt':
1161 $defaultName = 'Omniva kurjeris';
1162 break;
1163 }
1164 $this->form_fields['method_name_'.$language_code] = array(
1165 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
1166 'type' => 'text',
1167 'default' => $defaultName
1168 );
1169 }
1170 }
1171
1172 if (mk_wc_version("3.4.0")) $a = 'admin.php?page=wc-settings&tab=advanced&section=mk_api'; else $a = 'admin.php?page=wc-settings&tab=api&section=mk_api';
1173 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => sprintf(__('You can automatically create shipments into Omniva system and print the out the package labels right here, at the shop orders view. <br> Please set your Omniva web servises account credentials below here. <br>(see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain'), admin_url($a)));
1174
1175 $this->form_fields['registerOnPaymentCode'] = array(
1176 'title' => __('Register shipments automatically as', 'wc_makecommerce_domain'),
1177 'type' => 'select',
1178 'label' => __('register on Payment', 'wc_makecommerce_domain'),
1179 'description' => __('Shipments will be automatically registered on payment using the selected Omniva service code', 'wc_makecommerce_domain'),
1180 'options' => array(
1181 'QP' => __('QP - handover to Omniva at Post Office', 'wc_makecommerce_domain'),
1182 'PK' => __('PK - handover to Omniva via Parcel Machine', 'wc_makecommerce_domain'),
1183 '' => __('None - do not register automatically', 'wc_makecommerce_domain'),
1184 ),
1185 'default' => 'QP',
1186 );
1187
1188 $this->form_fields['service_user'] = array(
1189 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
1190 'type' => 'text',
1191 'default' => ''
1192 );
1193 $this->form_fields['service_password'] = array(
1194 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
1195 'type' => 'text',
1196 'default' => ''
1197 );
1198 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => __('Return address', 'wc_makecommerce_domain'), 'description' => __('Please define return address for Omniva shipments', 'wc_makecommerce_domain'));
1199 $this->form_fields['shop_name'] = array(
1200 'type' => 'text',
1201 'title' => __('Shop name', 'wc_makecommerce_domain'),
1202 'class' => 'input-text regular-input',
1203 );
1204 $this->form_fields['shop_phone'] = array(
1205 'type' => 'text',
1206 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1207 'class' => 'input-text regular-input',
1208 );
1209 $this->form_fields['shop_email'] = array(
1210 'type' => 'text',
1211 'title' => __('Shop email', 'wc_makecommerce_domain'),
1212 'class' => 'input-text regular-input',
1213 );
1214 $this->form_fields['shop_address_country'] = array(
1215 'type' => 'select',
1216 'title' => __('Shop address country', 'wc_makecommerce_domain'),
1217 'options' => array(
1218 'EE' => 'EE',
1219 'LV' => 'LV',
1220 'LT' => 'LT',
1221 ),
1222 'default' => 'EE',
1223 );
1224 $this->form_fields['shop_address_city'] = array(
1225 'type' => 'text',
1226 'title' => __('Shop address city', 'wc_makecommerce_domain'),
1227 'class' => 'input-text regular-input',
1228 );
1229 $this->form_fields['shop_address_street'] = array(
1230 'type' => 'text',
1231 'title' => __('Shop address street', 'wc_makecommerce_domain'),
1232 'class' => 'input-text regular-input',
1233 );
1234 $this->form_fields['shop_postal_code'] = array(
1235 'type' => 'text',
1236 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
1237 'class' => 'input-text regular-input',
1238 );
1239 }
1240 }
1241 }
1242
1243 $enable = get_option('mk_transport_courier_smartpost', 'yes');
1244 if ($enable === 'yes') {
1245 class WC_Courier_Shipping_Method_SmartPost extends WC_Courier_Shipping_Method {
1246
1247 public function __construct($instance_id = 0) {
1248 $this->ext = 'SmartPost';
1249 $this->name_ext = 'SmartPost';
1250 $this->carrier = 'smartpost';
1251 add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_smartpost_courier_checkout_fields'));
1252 add_action('woocommerce_checkout_process', array(&$this, 'check_checkout_fields'));
1253 add_action('woocommerce_after_checkout_validation', array(&$this, 'check_checkout_fields'));
1254 add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_order_meta'));
1255 parent::__construct($instance_id);
1256 }
1257
1258 public function add_smartpost_courier_checkout_fields($fragment) {
1259 $res = '';
1260 $res .= '<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_courier_'.mb_strtolower($this->ext).'">';
1261 $res .= '<td colspan="2">';
1262 $res .= '<p class="form-row" id="'.esc_attr($this->id).'_field">';
1263 $res .= '<select class="select" name="'.esc_attr($this->id).'" id="'.esc_attr($this->id).'">';
1264 $res .= '<option value="">'.__('-- choose courier arrivel time --', 'wc_makecommerce_domain').'</option>';
1265 $res .= '<option value="1">'.__('Worktime (09:00..17:00)', 'wc_makecommerce_domain').'</option>';
1266 $res .= '<option value="2">'.__('After worktime (17:00..21:00)', 'wc_makecommerce_domain').'</option>';
1267 $res .= '</select>';
1268 $res .= '</p>';
1269 $res .= '</td></tr>';
1270 echo $res;
1271 }
1272
1273 function check_checkout_fields() {
1274 #static $added = false;
1275 #if (!$added && empty($_POST[$this->id])) {
1276 # wc_add_notice(__('<strong>Delivery time</strong> is a required field.', 'wc_makecommerce_domain'), 'error');
1277 # $added = true;
1278 #}
1279 }
1280
1281 function add_order_meta($order_id) {
1282 if (!empty($_POST[$this->id])) {
1283 update_post_meta($order_id, '_delivery_time', sanitize_text_field($_POST[$this->id]));
1284 }
1285 }
1286
1287 public function init_form_fields() {
1288
1289 global $woocommerce;
1290
1291 $this->instance_form_fields = array();
1292 $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1293 $this->instance_form_fields['price'] = array(
1294 'title' => __('Shipping price', 'wc_makecommerce_domain'),
1295 'type' => 'text',
1296 'default' => '4.95'
1297 );
1298 $this->instance_form_fields['free_shipping_min_amount'] = array(
1299 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
1300 'type' => 'number',
1301 'default' => '0',
1302 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
1303 );
1304
1305 $this->form_fields = array();
1306 $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1307 $this->form_fields['generic'] = array(
1308 'type' => 'title',
1309 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
1310 // 'description' => __('', 'wc_makecommerce_domain'),
1311 );
1312 $this->form_fields['active'] = array(
1313 'title' => __('Enable', 'wc_makecommerce_domain'),
1314 'type' => 'checkbox',
1315 'label' => __('enabled', 'wc_makecommerce_domain'),
1316 'default' => 'no',
1317 );
1318 $this->form_fields['maximum_weight'] = array(
1319 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
1320 'type' => 'text',
1321 'default' => '60'
1322 );
1323
1324 $this->form_fields['look_and_feel'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('Look and feel options', 'wc_makecommerce_domain'), 'description' => __('Options for presentation on check-out page', 'wc_makecommerce_domain'));
1325
1326 if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
1327 $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1328 }
1329 else if ( defined( 'POLYLANG_VERSION' ) ) {
1330 $pl_locales = pll_languages_list( array('fields'=>'locale') );
1331 foreach ($pl_locales as $key => $locale_pl ) {
1332 $language = array( 'id' => $key,
1333 'code' => $locale_pl) ;
1334 $languages[$locale_pl] = $language;
1335 }
1336 }
1337
1338 if (empty($languages)) {
1339 $this->form_fields['method_name'] = array(
1340 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1341 'type' => 'text',
1342 'default' => __('SmartPOST Courier', 'wc_makecommerce_domain')
1343 );
1344 } else {
1345 foreach ($languages as $language_code => $language) {
1346 $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1347 $defaultName = __('SmartPOST Courier', 'wc_makecommerce_domain');
1348 switch ($language_code) {
1349 case 'et':
1350 $defaultName = 'SmartPOST kuller';
1351 break;
1352 case 'ru':
1353 $defaultName = 'SmartPOST Курьер';
1354 break;
1355 case 'fi':
1356 $defaultName = 'SmartPOST kuriiri';
1357 break;
1358 case 'lv':
1359 $defaultName = 'SmartPOST kurjers';
1360 break;
1361 case 'lt':
1362 $defaultName = 'SmartPOST kurjeris';
1363 break;
1364 }
1365 $this->form_fields['method_name_'.$language_code] = array(
1366 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
1367 'type' => 'text',
1368 'default' => $defaultName
1369 );
1370 }
1371 }
1372
1373 if (mk_wc_version("3.4.0")) $a = 'admin.php?page=wc-settings&tab=advanced&section=mk_api'; else $a = 'admin.php?page=wc-settings&tab=api&section=mk_api';
1374 $this->form_fields['api_access'] = array( 'type' => 'title', 'title' => '<hr><br>'.__('API access for', 'wc_makecommerce_domain').' '.$this->ext, 'description' => sprintf(__('You can automatically create shipments into SmartPOST system and print the out the package labels right here, at the shop orders view. <br> Please set your SmartPOST web servises account credentials below here. <br>(see more on <a href="https://makecommerce.net/en/integration-modules/makecommerce-woocommerce-payment-plugin/#carriers-integration">MakeCommerce plugin page</a>. Don\'t forget to enable also <a href="%s">MC API keys</a>!)', 'wc_makecommerce_domain'), admin_url($a)));
1375
1376 $this->form_fields['service_user'] = array(
1377 'title' => __('eteenindus.smartpost.ee username', 'wc_makecommerce_domain'),
1378 'type' => 'text',
1379 'default' => ''
1380 );
1381 $this->form_fields['service_password'] = array(
1382 'title' => __('eteenindus.smartpost.ee password', 'wc_makecommerce_domain'),
1383 'type' => 'text',
1384 'default' => ''
1385 );
1386 $this->form_fields['return_address'] = array( 'type' => 'title', 'title' => __('Return address', 'wc_makecommerce_domain'), 'description' => __('Please define return address for SmartPOST shipments (used on parcel labels)', 'wc_makecommerce_domain'));
1387 $this->form_fields['shop_name'] = array(
1388 'type' => 'text',
1389 'title' => __('Shop name', 'wc_makecommerce_domain'),
1390 'class' => 'input-text regular-input',
1391 );
1392 $this->form_fields['shop_phone'] = array(
1393 'type' => 'text',
1394 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1395 'class' => 'input-text regular-input',
1396 );
1397 $this->form_fields['shop_email'] = array(
1398 'type' => 'text',
1399 'title' => __('Shop email', 'wc_makecommerce_domain'),
1400 'class' => 'input-text regular-input',
1401 );
1402 $this->form_fields['shop_postal_code'] = array(
1403 'type' => 'text',
1404 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
1405 'class' => 'input-text regular-input',
1406 );
1407 }
1408 }
1409 }
1410 }
1411 }
1412 add_action('woocommerce_shipping_init', 'transport_add_method');
1413
1414
1415 function add_mc_shipping_method($methods) {
1416 $methods['parcelmachine_omniva'] = 'WC_ParcelMachine_Shipping_Method_Omniva';
1417 $methods['parcelmachine_smartpost'] = 'WC_ParcelMachine_Shipping_Method_Smartpost';
1418 $methods['parcelmachine_dpd'] = 'WC_ParcelMachine_Shipping_Method_DPD';
1419 $methods['courier_omniva'] = 'WC_Courier_Shipping_Method_Omniva';
1420 $methods['courier_smartpost'] = 'WC_Courier_Shipping_Method_Smartpost';
1421 return $methods;
1422 }
1423 add_filter('woocommerce_shipping_methods', 'add_mc_shipping_method');
1424
1425 function mk_mc_add_assets() {
1426 wp_enqueue_style('parcelmachine-css', plugin_dir_url(__FILE__).'/css/parcelmachine.css');
1427 wp_enqueue_script('parcelmachine-js', plugin_dir_url(__FILE__).'/scripts/parcelmachine.js', array('jquery'));
1428 }
1429 function mk_mc_add_assets_admin() {
1430 wp_enqueue_style('parcelmachine-css-admin', plugin_dir_url(__FILE__).'/css/parcelmachine-admin.css');
1431 wp_enqueue_script('parcelmachine-js-admin', plugin_dir_url(__FILE__).'/scripts/parcelmachine-admin.js', array('jquery'));
1432 }
1433 add_action('wp_enqueue_scripts', 'mk_mc_add_assets');
1434 add_action('admin_enqueue_scripts', 'mk_mc_add_assets_admin');
1435
1436 add_action('woocommerce_admin_field_api_javascript_ui', 'api_javascript_ui');
1437
1438
1439
1440 // ======== Woo Admin dialogs related stuff
1441
1442
1443 function mk_admin_restrict_manage_posts() {
1444 global $typenow;
1445 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
1446 $selected_method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
1447 $methods = WC()->shipping->load_shipping_methods();
1448 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
1449 echo '<option value="">'.__('-- filter by shipping method', 'wc_makecommerce_domain') . '</option>';
1450 foreach ($methods as $method) {
1451 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>';
1452 }
1453 echo '</select>';
1454 }
1455 }
1456 function mk_admin_shipping_filter($where, $wp_query) {
1457 global $pagenow, $wpdb;
1458 $method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
1459 if (is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty($method) ) {
1460 $where .= $GLOBALS['wpdb']->prepare( ' AND ID
1461 IN (
1462 SELECT items.order_id
1463 FROM '.$wpdb->prefix.'woocommerce_order_itemmeta meta, '.$wpdb->prefix.'woocommerce_order_items items
1464 WHERE meta.order_item_id = items.order_item_id
1465 AND meta.meta_key = "method_id"
1466 AND meta.meta_value = %s
1467 ) ', $method );
1468 }
1469 return $where;
1470 }
1471 add_filter('restrict_manage_posts', 'mk_admin_restrict_manage_posts');
1472 add_filter('posts_where', 'mk_admin_shipping_filter', 10, 2);
1473
1474 function mk_admin_bulk_actions() {
1475 wp_enqueue_script('jquery-ui-core');
1476 wp_enqueue_script('jquery-ui-datepicker');
1477 wp_enqueue_script('wc_mk_timepicker', plugins_url('/scripts/jquery-ui.timepicker.js', __FILE__));
1478 wp_enqueue_style('wc_mk_timepicker-css', plugins_url('/css/jquery-ui.timepicker.css', __FILE__));
1479 global $post_type;
1480 if ('shop_order' === $post_type) { ?>
1481 <script type="text/javascript">
1482 jQuery(function() {
1483 jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1484 jQuery('<option>').val('parcel_machine_labels').text('<?php _e( 'Register parcel machine shipments', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1485 jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1486 jQuery('<option>').val('parcel_machine_print_labels').text('<?php _e( 'Print parcel machine labels', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1487 //jQuery('<option>').val('parcel_machine_order_courier').text('<?php _e( 'Order courier for parcel machine deliveries', 'wc_makecommerce_domain' )?>').appendTo('select[name="action"]');
1488 //jQuery('<option>').val('parcel_machine_order_courier').text('<?php _e( 'Order courier for parcel machine deliveries', 'wc_makecommerce_domain' )?>').appendTo('select[name="action2"]');
1489 jQuery('select[name="action"]').after('<input type="text" name="transport_time" class="hidden timepicker"/>');
1490 jQuery('select[name="action"], select[name="action2"]').on('change', function(){
1491 var val = jQuery(this).val();
1492 if (val === 'parcel_machine_order_courier') {
1493 jQuery('.timepicker').removeClass('hidden');
1494 } else {
1495 jQuery('.timepicker').addClass('hidden');
1496 }
1497 });
1498 jQuery('.tablenav.bottom .actions.bulkactions').hide();
1499 var now = new Date();
1500 var mins = now.getMinutes();
1501 var quarterHours = Math.ceil(mins/15);
1502 if (quarterHours == 4) { now.setHours(now.getHours()+1); }
1503 var rounded = (quarterHours*15)%60;
1504 now.setMinutes(rounded);
1505 jQuery('.timepicker').datetimepicker({dateFormat: 'dd/mm/yy', timeFormat: 'HH:mm', stepMinute: 15, minDateTime: now, devaultValue: jQuery.datepicker.formatTime('dd/mm/yyyy HH:mm', now)});
1506 });
1507 </script>
1508 <?php if (!empty($_REQUEST['mk_err'])) { ?>
1509 <script type="text/javascript">
1510 jQuery(function(){
1511 alert('<?php echo htmlspecialchars($_REQUEST['mk_err']) ?>');
1512 });
1513 </script>
1514 <?php } ?>
1515 <?php if (!empty($_REQUEST['mk_pdf'])) { ?>
1516 <script type="text/javascript">
1517 jQuery(function(){
1518 window.open('<?php echo $_REQUEST['mk_pdf'] ?>', 'pdf');
1519 });
1520 </script>
1521 <?php }
1522 }
1523 }
1524 function mk_admin_bulk_action_labels() {
1525 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1526 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1527 mk_get_shipment_ids($post_ids);
1528 }
1529 function mk_admin_bulk_action_print() {
1530 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1531 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1532 mk_get_labels($post_ids);
1533 }
1534 function mk_admin_bulk_action_order_courier() {
1535 return;
1536 $wp_list_table = _get_list_table('WP_Posts_List_Table');
1537 $post_ids = array_map('absint', (array)$_REQUEST['post']);
1538 $transport_time = !empty($_REQUEST['transport_time']) ? strtotime(str_replace('/', '-', $_REQUEST['transport_time'])) : false;
1539 if ($transport_time && $transport_time > time()) {
1540 mk_get_shipment_ids($post_ids, 'omniva');
1541 } else {
1542 die('Please select transport time that is in the future');
1543 }
1544 }
1545 add_action('admin_footer', 'mk_admin_bulk_actions', 11);
1546 add_action('admin_action_parcel_machine_labels', 'mk_admin_bulk_action_labels');
1547 add_action('admin_action_parcel_machine_print_labels', 'mk_admin_bulk_action_print');
1548 add_action('admin_action_parcel_machine_order_courier', 'mk_admin_bulk_action_order_courier');
1549
1550 function mk_admin_save_post($post_id) {
1551 $post_type = get_post_type($post_id);
1552 if ('shop_order' !== $post_type) { return; }
1553 if (isset($_POST['_mk_machine_id'])) {
1554 update_post_meta($post_id, '_parcel_machine', $_POST['_mk_machine_id']);
1555 mk_get_shipment_ids($post_id);
1556 }
1557 }
1558
1559 add_action('save_post', 'mk_admin_save_post');
1560
1561
1562
1563 function mk_admin_parcelmachine_order_meta($order) {
1564 echo '</div>';
1565 echo '<div class="order_data_column" style="float: right;">';
1566 $order_id = $order->get_order_number();
1567 $machine_id = get_post_meta($order_id, '_parcel_machine', true);
1568 if (!empty($machine_id)) {
1569 list($carrier, $machine) = explode('||', $machine_id);
1570 if (!empty($machine)) {
1571 $machine = mk_get_machine($carrier, $machine);
1572 if ($machine) {
1573 echo '<h3>'.__('Selected parcel machine', 'wc_makecommerce_domain').'<a href="#" class="edit_address">'.__('Edit').'</a></h3>';
1574 echo '<div class="edit_address">';
1575 echo '<p class="form-field form-field-wide _mk_machine_id">';
1576 echo '<label for="_mk_machine_id">'.__('Parcel machine', 'wc_makecommerce_domain').'</label>';
1577 echo '<select id="_mk_machine_id" name="_mk_machine_id" class="wc-enhanced-select select">';
1578 $res = '';
1579 $machines = mk_get_machines($carrier, method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : $order->shipping_country);
1580 $res .= '<option value="">'.__('-- select parcel machine --', 'wc_makecommerce_domain').'</option>';
1581 $pcity = false;
1582 foreach ($machines as $smachine) {
1583 $city = strtolower($smachine['city']);
1584 if ($city !== $pcity) {
1585 if ($pcity) $res .= '</optgroup>';
1586 $res .= '<optgroup label="'.$smachine['city'].'">';
1587 }
1588 $mname = $smachine['name'];
1589 $res .= '<option value="'.esc_attr($smachine['carrier'].'||'.$smachine['id']).'" '.($machine['id'] === $smachine['id'] ? ' selected="selected"' : '').'>'.$mname.'</option>';
1590 $pcity = $city;
1591 }
1592 if ($pcity) $res .= '</optgroup>';
1593 echo $res;
1594 echo '</select>';
1595 echo '</p>';
1596 echo '</div>';
1597 echo '<div class="address">';
1598 echo '<p>'.$carrier.'</strong><br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>';
1599 echo '</div>';
1600 }
1601 }
1602 }
1603 if (empty($carrier)) {
1604 $carrier = '';
1605 }
1606 echo '<div style="clear: both;">';
1607 $delivery_time = get_post_meta($order_id, '_delivery_time', true);
1608 if ($delivery_time === '1') {
1609 echo '<p><strong>'.__('Delivery time', 'wc_makecommerce_domain').'</strong> - 09:00..17:00</p>';
1610 }
1611 if ($delivery_time === '2') {
1612 echo '<p><strong>'.__('Delivery time', 'wc_makecommerce_domain').'</strong> - 17:00..21:00</p>';
1613 }
1614
1615 $shipment_id = get_post_meta($order_id, '_parcel_machine_shipment_id', true);
1616 $shipment_id_error = get_post_meta($order_id, '_parcel_machine_error', true);
1617 $shipment_manifest = get_post_meta($order_id, '_parcel_machine_manifest', true);
1618 if ($shipment_id) {
1619 switch ($carrier) {
1620 case 'omniva':
1621 echo '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').'</strong> ('. $carrier .'): <br/> <a target="_blank " href="https://www.omniva.ee/abi/jalgimine?barcode=' . $shipment_id . '">'.$shipment_id.'</a>' ;
1622 break;
1623 case 'dpd':
1624 echo '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').'</strong> ('. $carrier .'): <br/> <a target="_blank " href="https://tracking.dpd.de/parcelstatus?locale=et_EE&Tracking=Track&query=' . $shipment_id . '">'.$shipment_id.'</a>' ;
1625 break;
1626 default:
1627 echo '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').':</strong><br/>' . $shipment_id . '</small></p>';
1628 break;
1629 }
1630 }
1631 if ($shipment_manifest) {
1632 echo '<p><strong>'.__('Shipments pickup manifest').':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>';
1633 }
1634 if ($shipment_id_error) {
1635 echo '<p><strong style="color: red;">'.__('Shipment registration error').':</strong><br/>' . $shipment_id_error . '</small></p>';
1636 }
1637 echo '</div>';
1638 }
1639
1640 function mk_admin_render_shop_order_columns( $column ) {
1641 global $post, $woocommerce, $the_order;
1642 if (!$the_order) return;
1643 $the_order_id = $the_order->get_order_number();
1644 if (empty($the_order) || $the_order_id != $post->ID) {
1645 $the_order = wc_get_order($post->ID);
1646 }
1647 if ($column === 'shipping_address') {
1648 $machine_id = get_post_meta($the_order_id, '_parcel_machine', true);
1649 $shipment_id = get_post_meta($the_order_id, '_parcel_machine_shipment_id', true);
1650 $shipment_id_error = get_post_meta($the_order_id, '_parcel_machine_error', true);
1651 if ($machine_id && !$shipment_id) {
1652 //echo __('Shipment ID not generated for delivery', 'wc_makecommerce_domain');
1653 }
1654 if ($shipment_id) {
1655 echo __('Shipment tracking code', 'wc_makecommerce_domain') . ': ' . $shipment_id . '<br/>';
1656 echo '<span class="has_shipment_id"></span>';
1657 }
1658 else if ($shipment_id_error) echo '<span style="color: red;">'.__('Package shipment generation error:', 'wc_makecommerce_domain') . '</span><br/>' .$shipment_id_error;
1659 }
1660 }
1661 function mk_add_email_customer_details_fields($fields, $sent_to_admin, $order) {
1662 $machine_id = get_post_meta($order->get_order_number(), '_parcel_machine', true);
1663 if (empty($machine_id)) return $fields;
1664 list($carrier, $machine) = explode('||', $machine_id);
1665 if (empty($machine)) return $fields;
1666 $machine = mk_get_machine($carrier, $machine);
1667 if (!$machine) return $fields;
1668 $fields[] = array('label' => __('Parcel machine', 'wc_makecommerce_domain').' ('.$carrier.')', 'value' => $machine['name'].' - '.$machine['address']);
1669 return $fields;
1670 }
1671 function mk_add_order_customer_details_fields($order) {
1672 $machine_id = get_post_meta($order->get_order_number(), '_parcel_machine', true);
1673 if (empty($machine_id)) return;
1674 list($carrier, $machine) = explode('||', $machine_id);
1675 if (empty($machine)) return;
1676 $machine = mk_get_machine($carrier, $machine);
1677 if (!$machine) return;
1678 echo '<tr>';
1679 echo '<th>'.__('Parcel machine', 'wc_makecommerce_domain').' ('.$carrier.') </th>';
1680 echo '<td>'.$machine['name'].'<br/>'.$machine['address'].'</td>';
1681 echo '</tr>';
1682 }
1683 function mk_admin_product_option_fields($fields) {
1684 echo '<div class="options_group">';
1685 woocommerce_wp_checkbox(
1686 array(
1687 'id' => '_no_parcel_machine',
1688 'label' => __('Does not fit parcel machine', 'wc_makecommerce_domain'),
1689 'description' => __('When this is checked, parcel machine shipping option is not available for a cart with this product', 'wc_makecommerce_domain')
1690 )
1691 );
1692 woocommerce_wp_checkbox(
1693 array(
1694 'id' => '_no_shipping_cost',
1695 'label' => __('Free parcel machine', 'wc_makecommerce_domain'),
1696 'description' => __('When this is checked, parcel machine is free for this product', 'wc_makecommerce_domain')
1697 )
1698 );
1699 echo '</div>';
1700 }
1701 function mk_admin_product_fields_save($post_id) {
1702 $no_parcel_machine = isset($_POST['_no_parcel_machine']) ? 'yes' : 'no';
1703 update_post_meta($post_id, '_no_parcel_machine', $no_parcel_machine);
1704 $no_shipping_cost = isset($_POST['_no_shipping_cost']) ? 'yes' : 'no';
1705 update_post_meta($post_id, '_no_shipping_cost', $no_shipping_cost);
1706 }
1707
1708
1709
1710
1711
1712
1713
1714 add_action('woocommerce_product_options_shipping', 'mk_admin_product_option_fields');
1715 add_action('woocommerce_process_product_meta', 'mk_admin_product_fields_save');
1716 add_action('woocommerce_admin_order_data_after_shipping_address', 'mk_admin_parcelmachine_order_meta', 10, 1);
1717 add_action('manage_shop_order_posts_custom_column', 'mk_admin_render_shop_order_columns', 3);
1718 add_filter('woocommerce_email_customer_details_fields', 'mk_add_email_customer_details_fields', 10, 3 );
1719 add_action('woocommerce_order_details_after_customer_details', 'mk_add_order_customer_details_fields');
1720 add_action('woocommerce_order_status_processing', 'mk_get_shipment_ids');
1721
1722
1723
1724
1725 // ===== MK API communication related stuff
1726
1727 function mk_get_shipment_ids($post_ids, $select_carrier = null) {
1728 if (!is_array($post_ids)) {
1729 $post_ids = array($post_ids);
1730 }
1731 transport_add_method();
1732 $shipping_request = array('credentials' => array(), 'orders' => array());
1733 $shipping_classes_map = array(
1734 'parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva',
1735 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_Smartpost',
1736 'parcelmachine_dpd' => 'WC_ParcelMachine_Shipping_Method_DPD',
1737 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva',
1738 'courier_smartpost' => 'WC_Courier_Shipping_Method_Smartpost'
1739 );
1740 foreach ($post_ids as $post_id) {
1741 $order = new WC_Order($post_id);
1742 $shipping_methods = $order->get_shipping_methods();
1743 if (empty($shipping_methods)) { continue; }
1744 foreach ($shipping_methods as $shipping_method) {
1745 $shipping_id = explode(':', $shipping_method['method_id']);
1746 $shipping_class = $shipping_id[0];
1747 $shipping_instance = !empty($shipping_id[1]) ? $shipping_id[1] : null;
1748 if (empty($shipping_classes_map[$shipping_class])) {
1749 continue;
1750 }
1751
1752 $transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance);
1753 $carrier_uc = mb_strtoupper($transport_class->carrier);
1754
1755 if ($transport_class->type === 'apt') {
1756 $parcel_machine = get_post_meta($post_id, '_parcel_machine', true);
1757 if (!$parcel_machine) { continue; }
1758 list($carrier, $machine_id) = explode('||', $parcel_machine);
1759 if (!$carrier || !$machine_id) { continue; }
1760 }
1761
1762 if (empty($shipping_request['credentials'][$carrier_uc])) {
1763 $api_user = $transport_class->settings['service_user'];
1764 $api_password = $transport_class->settings['service_password'];
1765
1766 $use_mk_contract = false;
1767 if ( isset($transport_class->settings['use_mk_contract']) ) {
1768 $use_mk_contract = $transport_class->settings['use_mk_contract'];
1769 }
1770
1771 if ( (!$api_user || !$api_password) and (!$use_mk_contract) ) {
1772 add_action('admin_notices', 'mk_admin_error');
1773 continue;
1774 }
1775
1776 // if $use_mk_contract is enabled, do not add credentials array to the request
1777 if (!$use_mk_contract) {
1778 $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password);
1779 }
1780 }
1781 $sender = array(
1782 'name' => !empty($transport_class->settings['shop_name']) ? $transport_class->settings['shop_name'] : '',
1783 'phone' => !empty($transport_class->settings['shop_phone']) ? $transport_class->settings['shop_phone'] : '',
1784 'email' => !empty($transport_class->settings['shop_email']) ? $transport_class->settings['shop_email'] : '',
1785 'country' => !empty($transport_class->settings['shop_address_country']) ? $transport_class->settings['shop_address_country'] : '',
1786 'city' => !empty($transport_class->settings['shop_address_city']) ? $transport_class->settings['shop_address_city'] : '',
1787 'street' => !empty($transport_class->settings['shop_address_street']) ? $transport_class->settings['shop_address_street'] : '',
1788 'postalCode' => !empty($transport_class->settings['shop_postal_code']) ? $transport_class->settings['shop_postal_code'] : ''
1789 );
1790 $s_first_name = method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : $order->shipping_first_name;
1791 $s_last_name = method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : $order->shipping_last_name;
1792 $b_first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name;
1793 $b_last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name;
1794 $b_phone = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone;
1795 $b_email = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email;
1796 $recipient_name = $s_first_name && $s_last_name ? $s_first_name . ' ' . $s_last_name : $b_first_name . ' ' . $b_last_name;
1797 $sr_order = array(
1798 'carrier' => $carrier_uc,
1799 'orderId' => $order->get_order_number(),
1800 'recipient' => array('name' => $recipient_name, 'phone' => $b_phone, 'email' => $b_email),
1801 'sender' => $sender,
1802 'destination' => array(),
1803 );
1804 $m_service_type = null;
1805 if ($transport_class->carrier === 'omniva' && $transport_class->type === 'atp') {
1806 $m_service_type = 'PA';
1807 } else if (!empty($transport_class->settings['registerOnPaymentCode'])) {
1808 $m_service_type = $transport_class->settings['registerOnPaymentCode'];
1809 }
1810
1811 if (!empty($machine_id)) {
1812 $sr_order['destination']['destinationId'] = $machine_id;
1813 }
1814 if ($transport_class->type === 'cou') {
1815 $s_postcode = method_exists($order, 'get_shipping_postcode') ? $order->get_shipping_postcode() : $order->shipping_postcode;
1816 $s_country = method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : $order->shipping_country;
1817 $s_state = method_exists($order, 'get_shipping_state') ? $order->get_shipping_state() : $order->shipping_state;
1818 $s_city = method_exists($order, 'get_shipping_city') ? $order->get_shipping_city() : $order->shipping_city;
1819 $s_address_1 = method_exists($order, 'get_shipping_address_1') ? $order->get_shipping_address_1() : $order->shipping_address_1;
1820 $s_address_2 = method_exists($order, 'get_shipping_address_2') ? $order->get_shipping_address_2() : $order->shipping_address_2;
1821 $sr_order['destination'] = array(
1822 'postalCode' => $s_postcode,
1823 'country' => $s_country,
1824 'county' => $s_state,
1825 'city' => $s_city,
1826 'street' => $s_address_1 . ' ' . $s_address_2
1827 );
1828 $delivery_time = get_post_meta($post_id, '_delivery_time', true);
1829 if ($delivery_time) { $sr_order['destination']['timeWindow'] = $delivery_time; }
1830 }
1831
1832 if ($m_service_type) {
1833 $sr_order['services'] = array('serviceType' => $m_service_type);
1834 }
1835
1836 $shipping_request['orders'][] = $sr_order;
1837 }
1838 }
1839 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
1840 if (empty($shipping_request['orders'])) {
1841 return;
1842 }
1843 $MK = mk_get_api();
1844 if (!$MK) {
1845 return;
1846 }
1847 try {
1848 $response = $MK->createShipments($shipping_request);
1849 } catch (Exception $e) {
1850 error_log('Error while creating shipment ['.$e->getMessage().']');
1851 return;
1852 }
1853 $shipments = !empty($response->shipments) ? $response->shipments : $response;
1854 $manifest = !empty($response->manifests) ? $response->manifests[0] : false;
1855
1856 foreach ($shipments as $order) {
1857 if ($manifest) {
1858 update_post_meta((int)$order->orderId, '_parcel_machine_manifest', sanitize_text_field($manifest));
1859 }
1860 if (!empty($order->orderId) && !empty($order->shipmentId)) {
1861 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->shipmentId));
1862 delete_post_meta((int)$order->orderId, '_parcel_machine_error');
1863 } else if (!empty($order->orderId) && !empty($order->barCode)) {
1864 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->barCode));
1865 delete_post_meta((int)$order->orderId, '_parcel_machine_error');
1866 } else if (!empty($order->orderId) && !empty($order->errorMessage)) {
1867 update_post_meta((int)$order->orderId, '_parcel_machine_error', sanitize_text_field($order->errorMessage));
1868 }
1869 }
1870 }
1871
1872 function mk_get_labels($post_ids, $ajax = false) {
1873 if (!is_array($post_ids)) {
1874 $post_ids = array($post_ids);
1875 }
1876 transport_add_method();
1877 $shipping_request = array('credentials' => array(), 'orders' => array(), 'printFormat' => 'A4');
1878 $shipping_classes_map = array(
1879 'parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva',
1880 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_Smartpost',
1881 'parcelmachine_dpd' => 'WC_ParcelMachine_Shipping_Method_DPD',
1882 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva',
1883 'courier_smartpost' => 'WC_Courier_Shipping_Method_Smartpost'
1884 );
1885 $missing_ids = array();
1886 foreach ($post_ids as $post_id) {
1887 $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
1888 if (!$shipment_id) {
1889 $missing_ids[] = $post_id;
1890 continue;
1891 }
1892 $order = new WC_Order($post_id);
1893 $shipping_methods = $order->get_shipping_methods();
1894 if (empty($shipping_methods)) { continue; }
1895 foreach ($shipping_methods as $shipping_method) {
1896 $shipping_id = explode(':', $shipping_method['method_id']);
1897 $shipping_class = $shipping_id[0];
1898 $shipping_instance = !empty($shipping_id[1]) ? $shipping_id[1] : null;
1899 if (empty($shipping_classes_map[$shipping_class])) {
1900 continue;
1901 }
1902
1903 $transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance);
1904 $carrier_uc = mb_strtoupper($transport_class->carrier);
1905
1906 if ($transport_class->type === 'apt') {
1907 $parcel_machine = get_post_meta($post_id, '_parcel_machine', true);
1908 if (!$parcel_machine) { continue; }
1909 list($carrier, $machine_id) = explode('||', $parcel_machine);
1910 if (!$carrier || !$machine_id) { continue; }
1911 }
1912 if (empty($shipping_request['credentials'][$carrier_uc])) {
1913 $api_user = $transport_class->settings['service_user'];
1914 $api_password = $transport_class->settings['service_password'];
1915
1916 $use_mk_contract = false;
1917 if ( isset($transport_class->settings['use_mk_contract']) ) {
1918 $use_mk_contract = $transport_class->settings['use_mk_contract'];
1919 }
1920
1921 if ( (!$api_user || !$api_password) and (!$use_mk_contract) ) {
1922 add_action('admin_notices', 'mk_admin_error');
1923 continue;
1924 }
1925 if (!$use_mk_contract) {
1926 $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password);
1927 }
1928 }
1929 $sender = array(
1930 'name' => $transport_class->settings['shop_name'],
1931 'phone' => $transport_class->settings['shop_phone'],
1932 'email' => $transport_class->settings['shop_email'],
1933 'postalCode' => !empty($transport_class->settings['shop_postal_code']) ? $transport_class->settings['shop_postal_code'] : '',
1934 'country' => !empty($transport_class->settings['shop_address_country']) ? $transport_class->settings['shop_address_country'] : '',
1935 'city' => !empty($transport_class->settings['shop_address_city']) ? $transport_class->settings['shop_address_city'] : '',
1936 'street' => !empty($transport_class->settings['shop_address_street']) ? $transport_class->settings['shop_address_street'] : ''
1937 );
1938 $s_first_name = method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : $order->shipping_first_name;
1939 $s_last_name = method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : $order->shipping_last_name;
1940 $recipient = trim($s_first_name . ' ' . $s_last_name);
1941 if (empty($recipient)) {
1942 $b_first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name;
1943 $b_last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name;
1944 $recipient = trim($b_first_name . ' ' . $b_last_name);
1945 }
1946 $b_phone = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone;
1947 $b_email = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email;
1948 $sr_order = array(
1949 'carrier' => $carrier_uc,
1950 'orderId' => $order->get_order_number(),
1951 'recipient' => array('name' => $recipient, 'phone' => $b_phone, 'email' => $b_email),
1952 'sender' => $sender,
1953 'shipmentId' => $shipment_id
1954 );
1955 $m_service_type = null;
1956 if ($transport_class->carrier === 'omniva' && $transport_class->type === 'atp') {
1957 $m_service_type = 'PA';
1958 } else if (!empty($transport_class->settings['registerOnPaymentCode'])) {
1959 $m_service_type = $transport_class->settings['registerOnPaymentCode'];
1960 }
1961
1962 if (!empty($machine_id)) {
1963 $sr_order['destination']['destinationId'] = $machine_id;
1964 }
1965 if ($transport_class->type === 'cou') {
1966 $sr_order['destination'] = array(
1967 'postalCode' => $order->shipping_postcode,
1968 'country' => $order->shipping_country,
1969 'county' => $order->shipping_state,
1970 'city' => $order->shipping_city,
1971 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2
1972 );
1973 $delivery_time = get_post_meta($post_id, '_delivery_time', true);
1974 if ($delivery_time) { $sr_order['destination']['timeWindow'] = $delivery_time; }
1975 }
1976
1977 if ($m_service_type) {
1978 $sr_order['services'] = array('serviceType' => $m_service_type);
1979 }
1980
1981 $shipping_request['orders'][] = $sr_order;
1982 }
1983 }
1984 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
1985 if (empty($shipping_request['orders'])) {
1986 return;
1987 }
1988 $MK = mk_get_api();
1989 if (!$MK) {
1990 return;
1991 }
1992 try {
1993 $response = $MK->createLabels($shipping_request);
1994 } catch (Exception $e) {
1995 echo $e->getMessage();
1996 exit();
1997 }
1998 if (empty($response->labelUrl)) {
1999 return;
2000 }
2001 if ($ajax) {
2002 return $response->labelUrl;
2003 }
2004 $mk_err = false;
2005 if (!empty($missing_ids)) { $mk_err = sprintf(__('Orders %s did not have shipment ID attached so no labels was printed! Please register those packages if you think this is an error!', 'wc_makecommerce_domain'), join(', ', $missing_ids)); }
2006 $sendback = add_query_arg(array('post_type' => 'shop_order', 'mk_pdf' => urlencode($response->labelUrl), 'mk_err' => urlencode($mk_err)), '');
2007 wp_redirect(esc_url_raw($sendback));
2008 exit();
2009 }
2010
2011 function mk_get_machines($carrier, $country = null) {
2012 $data = get_option('mk_machines_cache', false);
2013 $data_expires = get_option('mk_machines_expires', false);
2014 if (!$data || empty($data) || $data_expires < time()) {
2015 $MK = mk_get_api();
2016 if(!$MK) {
2017 return array();
2018 }
2019 $data = $MK->getDestinations(array('type' => 'APT,PUP'));
2020 update_option('mk_machines_cache', $data, 'no');
2021 update_option('mk_machines_expires', time()+3*60*60, 'no');
2022 }
2023
2024 $machines = array();
2025 if (!$data || empty($data)) {
2026 return array();
2027 }
2028 foreach ($data as $machine) {
2029 if ((!$country || $machine->country === $country) && ($machine->type === 'APT' || $machine->type === 'PUP') && ($carrier === '*' || strtolower($carrier) === strtolower($machine->carrier))) {
2030 $machines[] = array(
2031 'carrier' => strtolower($machine->carrier),
2032 'id' => $machine->id,
2033 'name' => $machine->name,
2034 'city' => $machine->city,
2035 'address' => !empty($machine->address) ? $machine->address : '',
2036 );
2037 }
2038 }
2039 usort($machines, function($a, $b){
2040 if ($a['city'] === $b['city']) {
2041 return $a['name'] > $b['name'];
2042 }
2043 return $a['city'] > $b['city'];
2044 });
2045 return $machines;
2046 }
2047
2048 function mk_get_machine($carrier, $id) {
2049 $machines = mk_get_machines($carrier);
2050 foreach ($machines as $machine) {
2051 if ($machine['carrier'] === $carrier && $machine['id'] == $id) {
2052 return $machine;
2053 }
2054 }
2055 return false;
2056 }
2057
2058 function print_parcel_machine_label_action( $post_id ) {
2059 $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
2060 if (!$shipment_id) { return; }
2061 ?>
2062 <li class="wide">
2063 <a id="print_parcel_machine_label" href="#" class="button"><?php _e('Print parcel label', 'wc_makecommerce_domain');?></a>
2064 <img class="mc_loading" src="<?php echo site_url('/wp-admin/images/loading.gif');?>">
2065 <script type="text/javascript">
2066 jQuery(function($){
2067 var data = {
2068 'action': 'print_pml',
2069 'id': '<?php echo $post_id; ?>'
2070 };
2071 var loading = $('.mc_loading');
2072 $('#print_parcel_machine_label').click(function(e){
2073 e.preventDefault();
2074 var button = $(this);
2075 button.addClass('disabled');
2076 loading.show();
2077 $.post(ajaxurl, data, function(response) {
2078 button.removeClass('disabled');
2079 loading.hide();
2080 window.open(response, 'pdf');
2081 });
2082 });
2083 });
2084 </script>
2085 </li>
2086 <?php
2087 }
2088 add_action('woocommerce_order_actions_end', 'print_parcel_machine_label_action');
2089
2090 function ajax_print_pml() {
2091 if(!current_user_can('manage_woocommerce') ) {
2092 die();
2093 }
2094 echo mk_get_labels(array(intval($_POST['id'])), true);
2095 die();
2096 }
2097 add_action( 'wp_ajax_print_pml', 'ajax_print_pml' );
2098
2099
2100 function clear_wc_shipping_rates_cache(){
2101 $packages = WC()->cart->get_shipping_packages();
2102 foreach ($packages as $key => $value) {
2103 $shipping_session = "shipping_for_package_$key";
2104 unset(WC()->session->$shipping_session);
2105 }
2106 }
2107 add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');
2108
2109
2110
2111 function api_javascript_ui( $data ) {
2112 ?>
2113 <script type="text/javascript">
2114 jQuery(document).ready(function($) {
2115 var env_select = $('#mk_api_type');
2116 var test_fields = ['mk_test_shop_id', 'mk_test_private_key', 'mk_test_public_key'];
2117 var live_fields = ['mk_shop_id', 'mk_private_key', 'mk_public_key'];
2118
2119 env_select.on('change', function(){
2120 hideFileds($(this).val());
2121 });
2122 hideFileds(env_select.val());
2123 function hideFileds(type) {
2124 var hide = type == 'live' ? test_fields : live_fields;
2125 var show = type == 'live' ? live_fields : test_fields;
2126 $.each(hide, function(i, elem) {
2127 $('#'+elem).closest('tr').hide();
2128 });
2129 $.each(show, function(i, elem) {
2130 $('#'+elem).closest('tr').show();
2131 });
2132 }
2133 });
2134 </script>
2135 <?php
2136 }
2137
2138
2139
2140
2141