PluginProbe
MakeCommerce for WooCommerce / 2.3.0
MakeCommerce for WooCommerce v2.3.0
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.3.0, at makecommerce-transport.php

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