PluginProbe
MakeCommerce for WooCommerce / 2.6.2
MakeCommerce for WooCommerce v2.6.2
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
← All changes | makecommerce-transport.php +1513 -563 2.1.22.6.2 View file →
@@ -29,20 +29,68 @@
29 29 throw new Exception('Do not call this class directly!');
30 30 }
31 31 $this->id = 'parcelmachine_' . mb_strtolower($this->ext);
32 32 $this->instance_id = absint($instance_id);
33 - $this->method_title = $this->name_ext . __(' Parcel Machine (MC)', 'wc_makecommerce_domain');
33 + $this->method_title = $this->name_ext . ' ' . __(' Parcel Machine (MC)', 'wc_makecommerce_domain');
34 34 // $this->method_description = sprintf(__('Parcel machine transport method for %s', 'wc_makecommerce_domain'), $this->ext);
35 35 $this->supports = array('shipping-zones', 'instance-settings', 'instance-settings-modal', 'settings');
36 36 $this->init();
37 + $this->check_updates();
37 38 }
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 +
39 87 function init() {
40 88 $this->init_form_fields();
41 89 $this->init_settings();
42 90
43 91 $this->enable = $this->settings['active'];
44 - $this->title = $this->settings['method_name'];
92 + $this->title = !empty($this->settings['method_name']) ? $this->settings['method_name'] : '';
45 93 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) {
46 94 $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE];
47 95 }
48 96 if(!$this->title) {
@@ -48,9 +96,9 @@
48 96 if(!$this->title) {
49 97 $this->title = $this->method_title;
50 98 }
51 99 $this->availability = 'specific';
52 - $this->countries = $this->settings['countries'];
100 + $this->countries = !empty($this->settings['countries']) ? $this->settings['countries'] : array();
53 101 $this->prioritization = $this->settings['prioritization'];
54 102 $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0;
55 103 $this->maximum_weight = (double)$this->settings['maximum_weight'];
56 104 $this->short_office_names = $this->settings['short_office_names'];
@@ -56,15 +104,14 @@
56 104 $this->short_office_names = $this->settings['short_office_names'];
57 105 $this->order_country = 'unknown';
58 106 add_action('woocommerce_update_options_shipping_' . $this->id, array(&$this, 'process_admin_options'));
59 107 add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_parcelmachine_checkout_fields'));
60 - //add_filter('woocommerce_update_order_review_fragments' , array(&$this, 'add_parcelmachine_checkout_fields'));
61 108 add_action('woocommerce_checkout_process', array(&$this, 'check_parcelmachine_checkout_fields'));
62 109 add_action('woocommerce_after_checkout_validation', array(&$this, 'check_parcelmachine_checkout_fields'));
63 110 add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_parcelmachine_order_meta'));
64 111
65 112 // Woocommerce Multilingual overrides
66 - if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
113 + if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) {
67 114 add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50);
68 115 add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2);
69 116 }
70 117 }
@@ -80,11 +127,13 @@
80 127 public function override_shipping_title_translation($items, $order) {
81 128 foreach ($items as $key => $item) {
82 129 if($item['type'] == 'shipping') {
83 130 foreach ($item['item_meta_array'] as $meta) {
84 - $tmp = explode(':', $meta->value);
85 - if($meta->key == 'method_id' && $tmp[0] == $this->id) {
86 - $items[$key]['name'] = $this->title;
131 + if ($meta->key === 'method_id') {
132 + $tmp = explode(':', $meta->value);
133 + if ($tmp[0] == $this->id) {
134 + $items[$key]['name'] = $this->title;
135 + }
87 136 }
88 137 }
89 138 }
90 139 }
@@ -132,13 +181,27 @@
132 181 }
133 182
134 183 $free_shipping = true;
135 184 foreach ($package['contents'] as $line) {
185 +
136 186 $free_shipping = get_post_meta($line['product_id'], '_no_shipping_cost', true) === 'yes' ? $free_shipping : false;
137 187 }
188 +
138 189 if($free_shipping) {
139 190 $price = 0;
140 191 }
192 +
193 + //check if there is a free shipping coupon (if it's free then allow free shipping)
194 + if (isset($package['applied_coupons']) && $this->instance_settings["allow_free_shipping_coupons"] === "yes") {
195 + foreach ($package['applied_coupons'] as $coupon_code) {
196 + $coupon = new WC_Coupon($coupon_code);
197 + if ($coupon->get_free_shipping() === true) {
198 + $price = 0;
199 + break;
200 + }
201 + }
202 + }
203 +
141 204 $rate = array(
142 205 'id' => $this->get_rate_id(),
143 206 'label' => $this->title,
144 207 'cost' => $price,
@@ -144,19 +207,12 @@
144 207 'cost' => $price,
145 208 'package' => $package,
146 209 'calc_tax' => 'per_order',
147 210 );
211 +
148 212 $this->add_rate( $rate );
149 213 }
150 214
151 - function calculate_weight($package) {
152 - $weight = 0;
153 - foreach ($package['contents'] as $line) {
154 - $weight += $line['data']->get_weight();
155 - }
156 - return $weight;
157 - }
158 -
159 215 function fits_parcel_machine($package) {
160 216 foreach ($package['contents'] as $line) {
161 217 if (get_post_meta($line['product_id'], '_no_parcel_machine', true) === 'yes') {
162 218 return false;
@@ -168,19 +224,19 @@
168 224 function is_available($package) {
169 225 if (!$this->fits_parcel_machine($package)) {
170 226 return false;
171 227 }
172 - $package_weight = $this->calculate_weight($package);
173 - if ($package_weight >= $this->maximum_weight) {
228 +
229 + //check if cart weight exceeds max weight
230 + if (WC()->cart->cart_contents_weight > $this->maximum_weight) {
174 231 return false;
175 232 }
233 +
176 234 $is_available = $this->enable === 'yes';
177 235 if (!$is_available || !mk_is_api_set()) {
178 236 return false;
179 237 }
180 - if (is_array($this->countries) && !in_array($package['destination']['country'], $this->countries)) {
181 - $is_available = false;
182 - }
238 +
183 239 $this->order_country = $package['destination']['country'];
184 240 return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package);
185 241 }
186 242
@@ -245,21 +301,55 @@
245 301 echo $res;
246 302 }
247 303
248 304 function check_parcelmachine_checkout_fields() {
305 + static $added1 = false;
306 + static $added2 = false;
249 307 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
250 308 if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); }
251 - if ($shipping_method_ext[0] === $this->id && empty($_POST[$shipping_method_ext[0]])) {
252 - wc_add_notice(__('<strong>Parcel machine</strong> is a required field.'), 'error');
309 + if (!$added1 && $shipping_method_ext[0] === $this->id && empty($_POST[$shipping_method_ext[0]])) {
310 + wc_add_notice(__('<strong>Parcel machine</strong> is a required field.', 'wc_makecommerce_domain'), 'error');
311 + $added1 = true;
253 312 }
313 + if (!$added2 && $shipping_method_ext[0] === $this->id && !$this->valid_phone_number($_POST['billing_phone'])) {
314 + 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');
315 + $added2 = true;
316 + }
254 317 }
255 318
319 + function valid_phone_number($phone) {
320 + $phone = trim($phone);
321 + $phone = trim($phone, '+');
322 + // Estonia for all
323 + if (substr($phone, 0, 4) === '3725' || substr($phone, 0, 1) === '5') { return true; }
324 + // Latvia for Omniva +3712
325 + if ($this->carrier === 'omniva' && substr($phone, 0, 3) === '371') { return true; }
326 + // Lithuania for Omniva
327 + if ($this->carrier === 'omniva' && substr($phone, 0, 3) === '370') { return true; }
328 + // Finland for Smartpost
329 + if ($this->carrier === 'smartpost' && substr($phone, 0, 3) === '358') { return true; }
330 + // FI, LT, LV, EE, SE for DPD
331 + if ($this->carrier === 'dpd' && (substr($phone, 0, 3) === '358' || substr($phone, 0, 3) === '370' || substr($phone, 0, 3) === '371' || substr($phone, 0, 3) === '372' || substr($phone, 0, 2) === '46')) {
332 + return true;
333 + }
334 + return false;
335 + }
336 +
256 337 function add_parcelmachine_order_meta($order_id) {
257 338 $shipping_method = !empty($_POST['shipping_method']) ? $_POST['shipping_method'] : false;
258 339 if (!empty($shipping_method[0])) { $shipping_method_ext = explode(':', $shipping_method[0]); }
259 340 if ($shipping_method_ext[0] === $this->id && !empty($_POST[$this->id])) {
260 - update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id]));
261 - // update_post_meta($order_id, '_makecommerce_carrier', $this->carrier);
341 + list($carrier, $machine) = explode('||', $_POST[$this->id]);
342 + $machine = mk_get_machine($carrier, $machine);
343 + if (!empty($machine['id'])) {
344 + update_post_meta($order_id, '_shipping_first_name', get_post_meta($order_id, '_billing_first_name', true));
345 + update_post_meta($order_id, '_shipping_last_name', get_post_meta($order_id, '_billing_last_name', true));
346 + update_post_meta($order_id, '_shipping_address_1', sanitize_text_field($machine['name']));
347 + update_post_meta($order_id, '_shipping_address_2', sanitize_text_field($machine['address']));
348 + update_post_meta($order_id, '_shipping_city', sanitize_text_field($machine['city']));
349 + update_post_meta($order_id, '_shipping_postcode', '');
350 + update_post_meta($order_id, '_parcel_machine', sanitize_text_field($_POST[$this->id]));
351 + }
262 352 }
263 353 }
264 354 }
265 355
@@ -264,355 +354,652 @@
264 354 }
265 355
266 356
267 357
268 - class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method {
269 - function __construct($instance_id = 0) {
270 - $this->ext = 'Omniva';
271 - $this->name_ext = 'Omniva';
272 - $this->carrier = 'omniva';
273 - $this->type = 'apt';
274 - parent::__construct($instance_id);
275 - }
276 - function init_form_fields() {
358 + $enable = get_option('mk_transport_apt_omniva', 'yes');
359 + if ($enable === 'yes') {
360 + class WC_ParcelMachine_Shipping_Method_Omniva extends WC_ParcelMachine_Shipping_Method {
361 + function __construct($instance_id = 0) {
362 + $this->ext = 'Omniva';
363 + $this->name_ext = 'Omniva';
364 + $this->carrier = 'omniva';
365 + $this->type = 'apt';
366 + parent::__construct($instance_id);
367 + if (is_admin()) {
368 + add_action( 'wp_ajax_verify_feature_swc', array(&$this, 'mc_check_feature_swc') );
369 + }
370 +
371 + }
277 372
278 - $this->instance_form_fields = array();
279 - $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
280 - $this->instance_form_fields['price'] = array(
281 - 'title' => __('Shipping price', 'wc_makecommerce_domain'),
282 - 'type' => 'text',
283 - 'default' => '2.99'
373 +
374 + public function mc_check_feature_swc() {
375 + $status = $this->mc_check_feature('shipments_without_credentials');
376 + wp_send_json(array('feature_name' => 'shipments_without_credentials', 'feature_status' => $status));
377 + exit;
378 + }
379 +
380 +
381 + public function mc_check_feature($feature_name = '') {
382 +
383 + $feature_enabled = false;
384 +
385 + $request_params = array(
386 + 'environment' => json_encode(array(
387 + 'platform' => 'woocommerce '. WC()->version,
388 + 'module' => 'makecommerce feature_staus_check',
389 + )),
284 390 );
285 - $this->instance_form_fields['free_shipping_min_amount'] = array(
286 - 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
287 - 'type' => 'number',
288 - 'default' => '0',
289 - 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
290 - );
291 391
292 - $this->form_fields = array();
293 - $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
294 - $this->form_fields['generic'] = array(
295 - 'type' => 'title',
296 - 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
297 - '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'),
298 - );
299 - $this->form_fields['active'] = array(
300 - 'title' => __('Enable', 'wc_makecommerce_domain'),
301 - 'type' => 'checkbox',
302 - 'label' => __('enabled', 'wc_makecommerce_domain'),
303 - 'default' => 'no',
304 - );
305 - $this->form_fields['maximum_weight'] = array(
306 - 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
307 - 'type' => 'text',
308 - 'default' => '30'
309 - );
392 + $MK = mk_get_api();
393 + if (!$MK) {
394 + return false;
395 + }
396 +
397 + try {
398 + $shopConfig = $MK->getShopConfig($request_params);
399 + $features = $shopConfig->features;
400 + } catch (Exception $e) {
401 + error_log(print_r($e, 1));
402 + return false;
403 + }
404 +
405 + if(isset($features)) {
406 + foreach($features as $feature) {
407 + if ($feature->name == $feature_name) {
408 + $feature_enabled = $feature->enabled;
409 + }
310 410
311 - $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'));
411 + }
412 + }
413 +
414 + return $feature_enabled;
415 + }
312 416
313 - if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
314 - $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
315 - }
316 - else if ( defined( 'POLYLANG_VERSION' ) ) {
317 - $pl_locales = pll_languages_list( array('fields'=>'locale') );
318 - foreach ($pl_locales as $key => $locale_pl ) {
319 - $language = array( 'id' => $key,
320 - 'code' => $locale_pl) ;
321 - $languages[$locale_pl] = $language;
322 - }
323 - }
324 417
325 - if (empty($languages)) {
326 - $this->form_fields['method_name'] = array(
327 - 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
418 + function init_form_fields() {
419 +
420 + $this->instance_form_fields = array();
421 + $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
422 + $this->instance_form_fields['price'] = array(
423 + 'title' => __('Shipping price', 'wc_makecommerce_domain'),
328 424 'type' => 'text',
329 - 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
425 + 'default' => '5.00'
330 426 );
331 - } else {
332 - foreach ($languages as $language_code => $language) {
333 - $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
334 - $defaultName = __('Omniva Parcel Machine', 'wc_makecommerce_domain');
335 - switch ($language_code) {
336 - case 'et':
337 - $defaultName = 'Omniva pakiautomaat';
338 - break;
339 - case 'ru':
340 - $defaultName = 'Omniva посылочный автомат';
341 - break;
342 - case 'fi':
343 - $defaultName = 'Omniva pakettiautomaatti';
344 - break;
345 - case 'lv':
346 - $defaultName = 'Omniva Pakomāts';
347 - break;
348 - }
349 - $this->form_fields['method_name_'.$language_code] = array(
350 - 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
427 + $this->instance_form_fields['free_shipping_min_amount'] = array(
428 + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
429 + 'type' => 'number',
430 + 'default' => '0',
431 + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
432 + );
433 +
434 + $this->instance_form_fields['allow_free_shipping_coupons'] = array(
435 + 'title' => __('Free shipping coupons', 'wc_makecommerce_domain'),
436 + 'type' => 'checkbox',
437 + 'default' => 'no',
438 + 'desc_tip' => __('Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain'),
439 + );
440 +
441 + $this->form_fields = array();
442 + $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
443 + $this->form_fields['generic'] = array(
444 + 'type' => 'title',
445 + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
446 + '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'),
447 + );
448 + $this->form_fields['active'] = array(
449 + 'title' => __('Enable', 'wc_makecommerce_domain'),
450 + 'type' => 'checkbox',
451 + 'label' => __('enabled', 'wc_makecommerce_domain'),
452 + 'default' => 'no',
453 + );
454 + $this->form_fields['maximum_weight'] = array(
455 + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
456 + 'type' => 'text',
457 + 'default' => '30'
458 + );
459 +
460 + $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'));
461 +
462 + if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) {
463 + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
464 + }
465 + else if (function_exists('pll_languages_list')) {
466 + $pl_locales = pll_languages_list( array('fields'=>'locale') );
467 + foreach ($pl_locales as $key => $locale_pl ) {
468 + $language = array( 'id' => $key,
469 + 'code' => $locale_pl) ;
470 + $languages[$locale_pl] = $language;
471 + }
472 + }
473 +
474 + if (empty($languages)) {
475 + $this->form_fields['method_name'] = array(
476 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
351 477 'type' => 'text',
352 - 'default' => $defaultName
478 + 'default' => __('Omniva Parcel Machine', 'wc_makecommerce_domain')
353 479 );
480 + } else {
481 + foreach ($languages as $language_code => $language) {
482 + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
483 + $defaultName = __('Omniva Parcel Machine', 'wc_makecommerce_domain');
484 + switch (substr($language_code, 0, 2)) {
485 + case 'et':
486 + $defaultName = 'Omniva pakiautomaat';
487 + break;
488 + case 'ru':
489 + $defaultName = 'Omniva посылочный автомат';
490 + break;
491 + case 'fi':
492 + $defaultName = 'Omniva pakettiautomaatti';
493 + break;
494 + case 'lv':
495 + $defaultName = 'Omniva Pakomāts';
496 + break;
497 + }
498 + $this->form_fields['method_name_'.substr($language_code, 0, 2)] = array(
499 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
500 + 'type' => 'text',
501 + 'default' => $defaultName
502 + );
503 + }
354 504 }
355 - }
356 - $this->form_fields['prioritization'] = array(
357 - 'title' => __('Prioritize', 'wc_makecommerce_domain'),
358 - 'type' => 'checkbox',
359 - 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
360 - 'default' => 'yes'
505 + $this->form_fields['prioritization'] = array(
506 + 'title' => __('Prioritize', 'wc_makecommerce_domain'),
507 + 'type' => 'checkbox',
508 + 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
509 + 'default' => 'yes'
510 + );
511 + $this->form_fields['short_office_names'] = array(
512 + 'title' => __('Short names', 'wc_makecommerce_domain'),
513 + 'type' => 'checkbox',
514 + 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
515 + 'default' => 'no'
516 + );
517 +
518 + $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>');
519 +
520 + $this->form_fields['use_mk_contract'] = array(
521 + 'type' => 'select',
522 + 'title' => __('Contract', 'wc_makecommerce_domain'),
523 + 'options' => array(
524 + false => __('use my own Omniva contract', 'wc_makecommerce_domain'),
525 + true => __('use MakeCommerce transport mediation service', 'wc_makecommerce_domain'),
526 + ),
527 + 'default' => false,
528 + 'description' => '',
529 + );
530 + $this->form_fields['verify_feature_swc'] = array(
531 + 'type' => 'verify_feature_swc',
532 + 'title' => __('Verify service status', 'wc_makecommerce_domain'),
533 + 'description' => __('You must enable the Transport mediation service before using it.', 'wc_makecommerce_domain'),
534 + 'desc_tip' => __('This will check if the Transport mediation service has been enabled for your shop.', 'wc_makecommerce_domain'),
535 + 'placeholder' => __('Verify', 'wc_makecommerce_domain'),
536 + );
537 + $this->form_fields['service_carrier'] = array(
538 + 'type' => 'select',
539 + 'title' => __('Integration country', 'wc_makecommerce_domain'),
540 + 'options' => array(
541 + "OMNIVA" => __('Estonia', 'wc_makecommerce_domain'),
542 + "OMNIVA_LV" => __('Latvia', 'wc_makecommerce_domain'),
543 + "OMNIVA_LT" => __('Lithuania', 'wc_makecommerce_domain'),
544 + ),
545 + 'default' => "ee",
546 + 'description' => __("Which country's carrier gave you the credentials", 'wc_makecommerce_domain'),
547 + );
548 + $this->form_fields['service_user'] = array(
549 + 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
550 + 'type' => 'text',
551 + 'default' => ''
552 + );
553 + $this->form_fields['service_password'] = array(
554 + 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
555 + 'type' => 'text',
556 + 'default' => ''
557 + );
558 + $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'));
559 + $this->form_fields['shop_name'] = array(
560 + 'type' => 'text',
561 + 'title' => __('Shop name', 'wc_makecommerce_domain'),
562 + 'class' => 'input-text regular-input',
563 + );
564 + $this->form_fields['shop_phone'] = array(
565 + 'type' => 'text',
566 + 'title' => __('Shop phone', 'wc_makecommerce_domain'),
567 + 'title' => __('Shop phone', 'wc_makecommerce_domain'),
568 + 'class' => 'input-text regular-input',
569 + );
570 + $this->form_fields['shop_email'] = array(
571 + 'type' => 'text',
572 + 'title' => __('Shop email', 'wc_makecommerce_domain'),
573 + 'class' => 'input-text regular-input',
574 + );
575 + $this->form_fields['shop_address_country'] = array(
576 + 'type' => 'select',
577 + 'title' => __('Shop address country', 'wc_makecommerce_domain'),
578 + 'options' => array(
579 + 'EE' => 'EE',
580 + 'LV' => 'LV',
581 + 'LT' => 'LT',
582 + ),
583 + 'default' => 'EE',
584 + );
585 + $this->form_fields['shop_address_city'] = array(
586 + 'type' => 'text',
587 + 'title' => __('Shop address city', 'wc_makecommerce_domain'),
588 + 'class' => 'input-text regular-input',
589 + );
590 + $this->form_fields['shop_address_street'] = array(
591 + 'type' => 'text',
592 + 'title' => __('Shop address street', 'wc_makecommerce_domain'),
593 + 'class' => 'input-text regular-input',
594 + );
595 + $this->form_fields['shop_postal_code'] = array(
596 + 'type' => 'text',
597 + 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
598 + 'class' => 'input-text regular-input',
599 + );
600 + $this->form_fields['shipping_ui_javascript'] = array(
601 + 'type' => 'shipping_ui_javascript',
361 602 );
362 - $this->form_fields['short_office_names'] = array(
363 - 'title' => __('Short names', 'wc_makecommerce_domain'),
364 - 'type' => 'checkbox',
365 - 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
366 - 'default' => 'no'
367 - );
368 - $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')));
369 - $this->form_fields['service_user'] = array(
370 - 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
371 - 'type' => 'text',
372 - 'default' => ''
373 - );
374 - $this->form_fields['service_password'] = array(
375 - 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
376 - 'type' => 'text',
377 - 'default' => ''
378 - );
379 - $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'));
380 - $this->form_fields['shop_name'] = array(
381 - 'type' => 'text',
382 - 'title' => __('Shop name', 'wc_makecommerce_domain'),
383 - 'class' => 'input-text regular-input',
384 - );
385 - $this->form_fields['shop_phone'] = array(
386 - 'type' => 'text',
387 - 'title' => __('Shop phone', 'wc_makecommerce_domain'),
388 - 'class' => 'input-text regular-input',
389 - );
390 - $this->form_fields['shop_email'] = array(
391 - 'type' => 'text',
392 - 'title' => __('Shop email', 'wc_makecommerce_domain'),
393 - 'class' => 'input-text regular-input',
394 - );
395 - $this->form_fields['shop_address_country'] = array(
396 - 'type' => 'text',
397 - 'title' => __('Shop address country', 'wc_makecommerce_domain'),
398 - 'description' => __('Put here short country code, i.e. EE, LV, FI', 'wc_makecommerce_domain'),
399 - 'class' => 'input-text regular-input',
400 - );
401 - $this->form_fields['shop_address_city'] = array(
402 - 'type' => 'text',
403 - 'title' => __('Shop address city', 'wc_makecommerce_domain'),
404 - 'class' => 'input-text regular-input',
405 - );
406 - $this->form_fields['shop_address_street'] = array(
407 - 'type' => 'text',
408 - 'title' => __('Shop address street', 'wc_makecommerce_domain'),
409 - 'class' => 'input-text regular-input',
410 - );
411 - $this->form_fields['shop_postal_code'] = array(
412 - 'type' => 'text',
413 - 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
414 - 'class' => 'input-text regular-input',
415 - );
416 - $this->form_fields['shipping_ui_javascript'] = array(
417 - 'type' => 'shipping_ui_javascript',
418 - );
419 - }
420 - }
421 603
604 + }
422 605
606 + public function generate_verify_feature_swc_html( $key, $data ) {
607 + $field = $this->get_field_key( $key );
608 + $defaults = array(
609 + 'title' => '',
610 + 'disabled' => false,
611 + 'class' => '',
612 + 'css' => '',
613 + 'placeholder' => '',
614 + 'type' => 'text',
615 + 'desc_tip' => false,
616 + 'description' => '',
617 + 'custom_attributes' => array()
618 + );
423 619
424 - class WC_ParcelMachine_Shipping_Method_Smartpost extends WC_ParcelMachine_Shipping_Method {
425 - function __construct($instance_id = 0) {
426 - $this->ext = 'SmartPost';
427 - $this->name_ext = 'SmartPOST';
428 - $this->carrier = 'smartpost';
429 - $this->type = 'apt';
430 - parent::__construct($instance_id);
431 - }
432 - function init_form_fields() {
620 + $data = wp_parse_args( $data, $defaults );
433 621
434 - global $woocommerce;
622 + ob_start();
623 + ?>
435 624
436 - $this->instance_form_fields = array();
437 - $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
625 + <tr valign="top">
626 + <th scope="row" class="titledesc">
627 + <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
628 + <?php echo $this->get_tooltip_html( $data ); ?>
629 + </th>
630 + <td class="forminp">
631 + <fieldset>
632 + <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
633 + <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 ); ?> />
634 + <!-- <br><p><?php echo esc_attr( $data['description'] ); ?></p> -->
635 + <script type="text/javascript">
636 + jQuery('input#verify_feature_swc').on('click', function() {
637 + init_mc_loading();
638 + jQuery.ajax({
639 + url: '<?php echo get_site_url(); ?>/wp-admin/admin-ajax.php',
640 + type: 'POST',
641 + data: 'action=verify_feature_swc',
642 + success: function (output) {
643 + if(output) {
644 + if (output.feature_status == true) {
645 + alert('<?php echo __('There Transport Mediation service is already enabled for your Shop. \n You are good to go!', 'wc_makecommerce_domain'); ?>');
646 + } else {
647 + 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'); ?>');
648 + }
649 +
650 + } else {
651 + alert('<?php echo __('There was an error with your request. Please try again.', 'wc_makecommerce_domain'); ?>');
652 + }
653 + },
654 + complete: function() { stop_mc_loading(); }
655 + });
656 + });
657 +
658 + function init_mc_loading() {
659 + jQuery('input#verify_feature_swc').attr('disabled', 'disabled');
660 + }
661 +
662 + function stop_mc_loading() {
663 + jQuery('input#verify_feature_swc').removeAttr('disabled');
664 + }
665 + </script>
666 + </fieldset>
667 + </td>
668 + </tr>
669 + <?php
438 670
439 - $this->instance_form_fields['price'] = array(
440 - 'title' => __('Shipping price', 'wc_makecommerce_domain'),
441 - 'type' => 'text',
442 - 'default' => '2.99'
443 - );
444 - $this->instance_form_fields['free_shipping_min_amount'] = array(
445 - 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
446 - 'type' => 'number',
447 - 'default' => '0',
448 - 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
449 - );
671 + return ob_get_clean();
672 + }
450 673
451 - $this->form_fields = array();
452 - $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
453 - $this->form_fields['generic'] = array(
454 - 'type' => 'title',
455 - 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
456 - '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'),
457 - );
674 + }
675 + }
458 676
459 - $this->form_fields['active'] = array(
460 - 'title' => __('Enable', 'wc_makecommerce_domain'),
461 - 'type' => 'checkbox',
462 - 'label' => __('enabled', 'wc_makecommerce_domain'),
463 - 'default' => 'no',
464 - );
465 - $this->form_fields['maximum_weight'] = array(
466 - 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
467 - 'type' => 'text',
468 - 'default' => '30'
469 - );
677 + $enable = get_option('mk_transport_apt_dpd', 'yes');
678 + if ($enable === 'yes') {
679 + class WC_ParcelMachine_Shipping_Method_DPD extends WC_ParcelMachine_Shipping_Method {
680 + function __construct($instance_id = 0) {
681 + $this->ext = 'DPD';
682 + $this->name_ext = 'DPD';
683 + $this->carrier = 'dpd';
684 + $this->type = 'apt';
685 + parent::__construct($instance_id);
686 + $this->method_title = __('DPD', 'wc_makecommerce_domain').' '. __('parcel machine', 'wc_makecommerce_domain') .' (MC)';
687 + }
688 + function init_form_fields() {
470 689
471 - $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'));
690 + $this->instance_form_fields = array();
691 + $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
692 + $this->instance_form_fields['price'] = array(
693 + 'title' => __('Shipping price', 'wc_makecommerce_domain'),
694 + 'type' => 'text',
695 + 'default' => '5.00'
696 + );
697 + $this->instance_form_fields['free_shipping_min_amount'] = array(
698 + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
699 + 'type' => 'number',
700 + 'default' => '0',
701 + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
702 + );
703 + $this->instance_form_fields['allow_free_shipping_coupons'] = array(
704 + 'title' => __('Free shipping coupons', 'wc_makecommerce_domain'),
705 + 'type' => 'checkbox',
706 + 'default' => 'no',
707 + 'desc_tip' => __('Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain'),
708 + );
472 709
473 - if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
474 - $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
475 - }
476 - else if ( defined( 'POLYLANG_VERSION' ) ) {
477 - $pl_locales = pll_languages_list( array('fields'=>'locale') );
478 - foreach ($pl_locales as $key => $locale_pl ) {
479 - $language = array( 'id' => $key,
480 - 'code' => $locale_pl) ;
481 - $languages[$locale_pl] = $language;
482 - }
483 - }
484 -
485 - if (empty($languages)) {
486 - $this->form_fields['method_name'] = array(
487 - 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
710 + $this->form_fields = array();
711 + $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
712 + $this->form_fields['generic'] = array(
713 + 'type' => 'title',
714 + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
715 + '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'),
716 + );
717 + $this->form_fields['active'] = array(
718 + 'title' => __('Enable', 'wc_makecommerce_domain'),
719 + 'type' => 'checkbox',
720 + 'label' => __('enabled', 'wc_makecommerce_domain'),
721 + 'default' => 'no',
722 + );
723 + $this->form_fields['maximum_weight'] = array(
724 + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
488 725 'type' => 'text',
489 - 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
726 + 'default' => '30'
490 727 );
491 - } else {
492 - foreach ($languages as $language_code => $language) {
493 - $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
494 - $defaultName = __('SmartPOST Parcel Machine', 'wc_makecommerce_domain');
495 - switch ($language_code) {
496 - case 'et':
497 - $defaultName = 'SmartPOST pakiautomaat';
498 - break;
499 - case 'ru':
500 - $defaultName = 'SmartPOST посылочный автомат';
501 - break;
502 - case 'fi':
503 - $defaultName = 'SmartPOST pakettiautomaatti';
504 - break;
505 - case 'lv':
506 - $defaultName = 'SmartPOST Pakomāts';
507 - break;
728 +
729 + $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'));
730 +
731 + if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) {
732 + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
508 733 }
509 - $this->form_fields['method_name_'.$language_code] = array(
510 - 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
734 + else if (function_exists('pll_languages_list')) {
735 + $pl_locales = pll_languages_list( array('fields'=>'locale') );
736 + foreach ($pl_locales as $key => $locale_pl ) {
737 + $language = array( 'id' => $key,
738 + 'code' => $locale_pl) ;
739 + $languages[$locale_pl] = $language;
740 + }
741 + }
742 +
743 + if (empty($languages)) {
744 + $this->form_fields['method_name'] = array(
745 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
511 746 'type' => 'text',
512 - 'default' => $defaultName
747 + 'default' => __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain')
513 748 );
749 + } else {
750 + foreach ($languages as $language_code => $language) {
751 + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
752 + $defaultName = __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain');
753 + $this->form_fields['method_name_'.substr($language_code, 0, 2)] = array(
754 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
755 + 'type' => 'text',
756 + 'default' => $defaultName
757 + );
758 + }
514 759 }
760 + $this->form_fields['prioritization'] = array(
761 + 'title' => __('Prioritize', 'wc_makecommerce_domain'),
762 + 'type' => 'checkbox',
763 + 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
764 + 'default' => 'yes'
765 + );
766 + $this->form_fields['short_office_names'] = array(
767 + 'title' => __('Short names', 'wc_makecommerce_domain'),
768 + 'type' => 'checkbox',
769 + 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
770 + 'default' => 'no'
771 + );
772 + 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';
773 + $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)));
774 + $this->form_fields['service_carrier'] = array(
775 + 'type' => 'select',
776 + 'title' => __('Integration country', 'wc_makecommerce_domain'),
777 + 'options' => array(
778 + "DPD" => __('Estonia', 'wc_makecommerce_domain'),
779 + "DPD_LV" => __('Latvia', 'wc_makecommerce_domain'),
780 + "DPD_LT" => __('Lithuania', 'wc_makecommerce_domain'),
781 + ),
782 + 'default' => "ee",
783 + 'description' => __("Which country's carrier gave you the credentials", 'wc_makecommerce_domain'),
784 + );
785 + $this->form_fields['service_user'] = array(
786 + 'title' => __('DPD InterConnect username', 'wc_makecommerce_domain'),
787 + 'type' => 'text',
788 + 'default' => ''
789 + );
790 + $this->form_fields['service_password'] = array(
791 + 'title' => __('DPD InterConnect password', 'wc_makecommerce_domain'),
792 + 'type' => 'text',
793 + 'default' => ''
794 + );
795 + $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'));
796 + $this->form_fields['shop_name'] = array(
797 + 'type' => 'text',
798 + 'title' => __('Shop name', 'wc_makecommerce_domain'),
799 + 'class' => 'input-text regular-input',
800 + );
801 + $this->form_fields['shop_phone'] = array(
802 + 'type' => 'text',
803 + 'title' => __('Shop phone', 'wc_makecommerce_domain'),
804 + 'class' => 'input-text regular-input',
805 + );
806 + $this->form_fields['shop_email'] = array(
807 + 'type' => 'text',
808 + 'title' => __('Shop email', 'wc_makecommerce_domain'),
809 + 'class' => 'input-text regular-input',
810 + );
811 + $this->form_fields['shop_address_country'] = array(
812 + 'type' => 'select',
813 + 'title' => __('Shop address country', 'wc_makecommerce_domain'),
814 + 'options' => array(
815 + 'EE' => 'EE',
816 + 'LV' => 'LV',
817 + 'LT' => 'LT',
818 + ),
819 + 'default' => 'EE',
820 + );
821 + $this->form_fields['shop_address_city'] = array(
822 + 'type' => 'text',
823 + 'title' => __('Shop address city', 'wc_makecommerce_domain'),
824 + 'class' => 'input-text regular-input',
825 + );
826 + $this->form_fields['shop_address_street'] = array(
827 + 'type' => 'text',
828 + 'title' => __('Shop address street', 'wc_makecommerce_domain'),
829 + 'class' => 'input-text regular-input',
830 + );
831 + $this->form_fields['shop_postal_code'] = array(
832 + 'type' => 'text',
833 + 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
834 + 'class' => 'input-text regular-input',
835 + );
836 + $this->form_fields['shipping_ui_javascript'] = array(
837 + 'type' => 'shipping_ui_javascript',
838 + );
515 839 }
516 - $this->form_fields['prioritization'] = array(
517 - 'title' => __('Prioritize', 'wc_makecommerce_domain'),
518 - 'type' => 'checkbox',
519 - 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
520 - 'default' => 'yes'
521 - );
522 - $this->form_fields['short_office_names'] = array(
523 - 'title' => __('Short names', 'wc_makecommerce_domain'),
524 - 'type' => 'checkbox',
525 - 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
526 - 'default' => 'no'
527 - );
528 - $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')));
529 - $this->form_fields['service_user'] = array(
530 - 'title' => __('eteenindus.smartpost.ee username', 'wc_makecommerce_domain'),
531 - 'type' => 'text',
532 - 'default' => ''
533 - );
534 - $this->form_fields['service_password'] = array(
535 - 'title' => __('eteenindus.smartpost.ee password', 'wc_makecommerce_domain'),
536 - 'type' => 'text',
537 - 'default' => ''
538 - );
539 - $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'));
540 - $this->form_fields['shop_name'] = array(
541 - 'type' => 'text',
542 - 'title' => __('Shop name', 'wc_makecommerce_domain'),
543 - 'class' => 'input-text regular-input',
544 - );
545 - $this->form_fields['shop_phone'] = array(
546 - 'type' => 'text',
547 - 'title' => __('Shop phone', 'wc_makecommerce_domain'),
548 - 'class' => 'input-text regular-input',
549 - );
550 - $this->form_fields['shop_email'] = array(
551 - 'type' => 'text',
552 - 'title' => __('Shop email', 'wc_makecommerce_domain'),
553 - 'class' => 'input-text regular-input',
554 - );
555 - $this->form_fields['shipping_ui_javascript'] = array(
556 - 'type' => 'shipping_ui_javascript',
557 - );
558 840 }
559 841 }
560 842
561 - if (($cur_ver = get_site_option('wc_mc_version', 0)) < WC_MC_VERSION) {
562 - error_log("Need to update from [{$cur_ver}] to [".WC_MC_VERSION."]");
563 - if (WC_MC_VERSION === 2.0) {
564 - // Check if omniva / smartpost is enabled and in which countries
565 - // Convert to shipping zone
566 - foreach (array('parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva', 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_SmartPost') as $method_name => $method_class) {
567 - $transport_class_omniva = new $method_class();
568 - if ($transport_class_omniva->enable === 'yes') {
569 - $zones = WC_Shipping_Zones::get_zones();
570 - foreach ($transport_class_omniva->countries as $ecountry) {
571 - $has_ecountry = false;
572 - foreach ($zones as $zone) {
573 - foreach ($zone['zone_locations'] as $location) {
574 - if ($location->code === $ecountry) {
575 - $has_ecountry = $zone['zone_id'];
576 - foreach ($zone['shipping_methods'] as $method) {
577 - if ($method->id === $method_name) {
578 - continue 4;
579 - }
580 - }
581 - }
582 - }
843 +
844 +
845 +
846 +
847 + $enable = get_option('mk_transport_apt_smartpost', 'yes');
848 + if ($enable === 'yes') {
849 + class WC_ParcelMachine_Shipping_Method_Smartpost extends WC_ParcelMachine_Shipping_Method {
850 + function __construct($instance_id = 0) {
851 + $this->ext = 'SmartPost';
852 + $this->name_ext = 'SmartPOST';
853 + $this->carrier = 'smartpost';
854 + $this->type = 'apt';
855 + parent::__construct($instance_id);
856 + }
857 + function init_form_fields() {
858 +
859 + global $woocommerce;
860 +
861 + $this->instance_form_fields = array();
862 + $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
863 +
864 + $this->instance_form_fields['price'] = array(
865 + 'title' => __('Shipping price', 'wc_makecommerce_domain'),
866 + 'type' => 'text',
867 + 'default' => '5.00'
868 + );
869 + $this->instance_form_fields['free_shipping_min_amount'] = array(
870 + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
871 + 'type' => 'number',
872 + 'default' => '0',
873 + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
874 + );
875 + $this->instance_form_fields['allow_free_shipping_coupons'] = array(
876 + 'title' => __('Free shipping coupons', 'wc_makecommerce_domain'),
877 + 'type' => 'checkbox',
878 + 'default' => 'no',
879 + 'desc_tip' => __('Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain'),
880 + );
881 +
882 + $this->form_fields = array();
883 + $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
884 + $this->form_fields['generic'] = array(
885 + 'type' => 'title',
886 + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
887 + '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'),
888 + );
889 +
890 + $this->form_fields['active'] = array(
891 + 'title' => __('Enable', 'wc_makecommerce_domain'),
892 + 'type' => 'checkbox',
893 + 'label' => __('enabled', 'wc_makecommerce_domain'),
894 + 'default' => 'no',
895 + );
896 + $this->form_fields['maximum_weight'] = array(
897 + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
898 + 'type' => 'text',
899 + 'default' => '30'
900 + );
901 +
902 + $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'));
903 +
904 + if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) {
905 + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
906 + }
907 + else if (function_exists('pll_languages_list')) {
908 + $pl_locales = pll_languages_list( array('fields'=>'locale') );
909 + foreach ($pl_locales as $key => $locale_pl ) {
910 + $language = array( 'id' => $key,
911 + 'code' => $locale_pl) ;
912 + $languages[$locale_pl] = $language;
583 913 }
584 - if (!$has_ecountry) {
585 - $zone = new WC_Shipping_Zone();
586 - $zone->set_zone_name($ecountry);
587 - $zone->add_location($ecountry, 'country');
588 - $zone->save();
589 - } else {
590 - $zone = new WC_Shipping_Zone($has_ecountry);
914 + }
915 +
916 + if (empty($languages)) {
917 + $this->form_fields['method_name'] = array(
918 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
919 + 'type' => 'text',
920 + 'default' => __('SmartPOST Parcel Machine', 'wc_makecommerce_domain')
921 + );
922 + } else {
923 + foreach ($languages as $language_code => $language) {
924 + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
925 + $defaultName = __('SmartPOST Parcel Machine', 'wc_makecommerce_domain');
926 + switch (substr($language_code, 0, 2)) {
927 + case 'et':
928 + $defaultName = 'SmartPOST pakiautomaat';
929 + break;
930 + case 'ru':
931 + $defaultName = 'SmartPOST посылочный автомат';
932 + break;
933 + case 'fi':
934 + $defaultName = 'SmartPOST pakettiautomaatti';
935 + break;
936 + case 'lv':
937 + $defaultName = 'SmartPOST Pakomāts';
938 + break;
591 939 }
592 - $instance_id = $zone->add_shipping_method($method_name);
593 - $transport_class = new $method_class($instance_id);
594 - $price = !empty($transport_class->settings['price_'.strtolower($ecountry)]) ? $transport_class->settings['price_'.strtolower($ecountry)] : 0;
595 - $free_shipping_min_amount = !empty($transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)]) ? $transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)] : 0;
596 - $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));
597 - $transport_class->process_admin_options();
940 + $this->form_fields['method_name_'.substr($language_code, 0, 2)] = array(
941 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
942 + 'type' => 'text',
943 + 'default' => $defaultName
944 + );
598 945 }
599 946 }
947 + $this->form_fields['prioritization'] = array(
948 + 'title' => __('Prioritize', 'wc_makecommerce_domain'),
949 + 'type' => 'checkbox',
950 + 'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'),
951 + 'default' => 'yes'
952 + );
953 + $this->form_fields['short_office_names'] = array(
954 + 'title' => __('Short names', 'wc_makecommerce_domain'),
955 + 'type' => 'checkbox',
956 + 'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'),
957 + 'default' => 'no'
958 + );
959 + 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';
960 + $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)));
961 + $this->form_fields['service_user'] = array(
962 + 'title' => __('eservice.smartpost.ee username', 'wc_makecommerce_domain'),
963 + 'type' => 'text',
964 + 'default' => ''
965 + );
966 + $this->form_fields['service_password'] = array(
967 + 'title' => __('eservice.smartpost.ee password', 'wc_makecommerce_domain'),
968 + 'type' => 'text',
969 + 'default' => ''
970 + );
971 + $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'));
972 + $this->form_fields['shop_name'] = array(
973 + 'type' => 'text',
974 + 'title' => __('Shop name', 'wc_makecommerce_domain'),
975 + 'class' => 'input-text regular-input',
976 + );
977 + $this->form_fields['shop_phone'] = array(
978 + 'type' => 'text',
979 + 'title' => __('Shop phone', 'wc_makecommerce_domain'),
980 + 'class' => 'input-text regular-input',
981 + );
982 + $this->form_fields['shop_email'] = array(
983 + 'type' => 'text',
984 + 'title' => __('Shop email', 'wc_makecommerce_domain'),
985 + 'class' => 'input-text regular-input',
986 + );
987 + $this->form_fields['shipping_ui_javascript'] = array(
988 + 'type' => 'shipping_ui_javascript',
989 + );
600 990 }
601 991 }
602 - update_site_option('wc_mc_version', WC_MC_VERSION);
603 992 }
993 +
604 994 }
605 995
606 - if ( ! class_exists( 'WC_Courier_Shipping_Method_Omniva' ) ) {
996 + if ( ! class_exists( 'WC_Courier_Shipping_Method' ) ) {
607 997
608 - class WC_Courier_Shipping_Method_Omniva extends WC_Shipping_Method {
998 + class WC_Courier_Shipping_Method extends WC_Shipping_Method {
609 999
610 1000 public function __construct($instance_id = 0) {
611 - $this->ext = 'Omniva';
612 - $this->name_ext = 'Omniva';
613 - $this->id = 'courier_omniva';
614 - $this->carrier = 'omniva';
1001 + $this->id = 'courier_'.$this->carrier;
615 1002 $this->type = 'cou';
616 1003 $this->instance_id = absint($instance_id);
617 1004 $this->method_title = $this->name_ext .' '.__('courier (MC)', 'wc_makecommerce_domain');
618 1005 // $this->method_description = sprintf(__('Courier transport method for %s', 'wc_makecommerce_domain'), $this->ext);
@@ -626,12 +1013,12 @@
626 1013
627 1014 $this->enable = $this->settings['active'];
628 1015 if (defined('ICL_LANGUAGE_CODE') && !empty($this->settings['method_name_'.ICL_LANGUAGE_CODE])) {
629 1016 $this->title = $this->settings['method_name_'.ICL_LANGUAGE_CODE];
630 - } else {
1017 + } else if (!empty($this->settings['method_name'])) {
631 1018 $this->title = $this->settings['method_name'];
632 1019 }
633 - if(!$this->title) {
1020 + if (empty($this->title)) {
634 1021 $this->title = $this->method_title;
635 1022 }
636 1023 $this->availability = 'specific';
637 1024 $this->free_shipping_min_amount = !empty($this->settings['free_shipping_min_amount']) ? $this->settings['free_shipping_min_amount'] : 0;
@@ -642,9 +1029,9 @@
642 1029 // add_filter('woocommerce_order_shipping_to_display_shipped_via', array(&$this, 'add_admin_courier_via_field'));
643 1030 // add_filter('woocommerce_order_shipping_method', array(&$this, 'add_admin_courier_via_field'));
644 1031
645 1032 // Woocommerce Multilingual overrides
646 - if ( in_array( 'woocommerce-multilingual/wpml-woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
1033 + if (is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) {
647 1034 add_filter('woocommerce_package_rates', array($this, 'override_label_translation'), 50);
648 1035 add_filter('woocommerce_order_get_items', array($this, 'override_shipping_title_translation'), 50, 2);
649 1036 }
650 1037 }
@@ -660,11 +1047,13 @@
660 1047 public function override_shipping_title_translation($items, $order) {
661 1048 foreach ($items as $key => $item) {
662 1049 if($item['type'] == 'shipping') {
663 1050 foreach ($item['item_meta_array'] as $meta) {
664 - $tmp = explode(':', $meta->value);
665 - if($meta->key == 'method_id' && $tmp[0] == $this->id) {
666 - $items[$key]['name'] = $this->title;
1051 + if ($meta->key === 'method_id') {
1052 + $tmp = explode(':', $meta->value);
1053 + if ($tmp[0] == $this->id) {
1054 + $items[$key]['name'] = $this->title;
1055 + }
667 1056 }
668 1057 }
669 1058 }
670 1059 }
@@ -670,159 +1059,8 @@
670 1059 }
671 1060 return $items;
672 1061 }
673 1062
674 - public function init_form_fields() {
675 -
676 - global $woocommerce;
677 -
678 - $this->instance_form_fields = array();
679 - $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
680 - $this->instance_form_fields['price'] = array(
681 - 'title' => __('Shipping price', 'wc_makecommerce_domain'),
682 - 'type' => 'text',
683 - 'default' => '4.95'
684 - );
685 - $this->instance_form_fields['free_shipping_min_amount'] = array(
686 - 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
687 - 'type' => 'number',
688 - 'default' => '0',
689 - 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
690 - );
691 -
692 - $this->form_fields = array();
693 - $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
694 - $this->form_fields['generic'] = array(
695 - 'type' => 'title',
696 - 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
697 - // 'description' => __('', 'wc_makecommerce_domain'),
698 - );
699 - $this->form_fields['active'] = array(
700 - 'title' => __('Enable', 'wc_makecommerce_domain'),
701 - 'type' => 'checkbox',
702 - 'label' => __('enabled', 'wc_makecommerce_domain'),
703 - 'default' => 'no',
704 - );
705 - $this->form_fields['maximum_weight'] = array(
706 - 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
707 - 'type' => 'text',
708 - 'default' => '60'
709 - );
710 -
711 - $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'));
712 -
713 - if ( function_exists('icl_object_id') && ! defined( 'POLYLANG_VERSION' ) ) {
714 - $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
715 - }
716 - else if ( defined( 'POLYLANG_VERSION' ) ) {
717 - $pl_locales = pll_languages_list( array('fields'=>'locale') );
718 - foreach ($pl_locales as $key => $locale_pl ) {
719 - $language = array( 'id' => $key,
720 - 'code' => $locale_pl) ;
721 - $languages[$locale_pl] = $language;
722 - }
723 - }
724 -
725 - if (empty($languages)) {
726 - $this->form_fields['method_name'] = array(
727 - 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
728 - 'type' => 'text',
729 - 'default' => __('Omniva Courier', 'wc_makecommerce_domain')
730 - );
731 - } else {
732 - foreach ($languages as $language_code => $language) {
733 - $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
734 - $defaultName = __('Omniva Courier', 'wc_makecommerce_domain');
735 - switch ($language_code) {
736 - case 'et':
737 - $defaultName = 'Omniva kuller';
738 - break;
739 - case 'ru':
740 - $defaultName = 'Omniva посылочный автомат';
741 - break;
742 - case 'fi':
743 - $defaultName = 'Курьер Omniva';
744 - break;
745 - case 'lv':
746 - $defaultName = 'Omniva kurjers';
747 - break;
748 - case 'lt':
749 - $defaultName = 'Omniva kurjeris';
750 - break;
751 - }
752 - $this->form_fields['method_name_'.$language_code] = array(
753 - 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', $language_code),
754 - 'type' => 'text',
755 - 'default' => $defaultName
756 - );
757 - }
758 - }
759 -
760 -
761 - $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')));
762 -
763 - $this->form_fields['registerOnPaymentCode'] = array(
764 - 'title' => __('Register shipments automatically as', 'wc_makecommerce_domain'),
765 - 'type' => 'select',
766 - 'label' => __('register on Payment', 'wc_makecommerce_domain'),
767 - 'description' => __('Shipments will be automatically registered on payment using the selected Omniva service code', 'wc_makecommerce_domain'),
768 - 'options' => array(
769 - 'QP' => __('QP - handover to Omniva at Post Office', 'wc_makecommerce_domain'),
770 - 'PK' => __('PK - handover to Omniva via Parcel Machine', 'wc_makecommerce_domain'),
771 - '' => __('None - do not register automatically', 'wc_makecommerce_domain'),
772 - ),
773 - 'default' => 'QP',
774 - );
775 -
776 - $this->form_fields['service_user'] = array(
777 - 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
778 - 'type' => 'text',
779 - 'default' => ''
780 - );
781 - $this->form_fields['service_password'] = array(
782 - 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
783 - 'type' => 'text',
784 - 'default' => ''
785 - );
786 - $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'));
787 - $this->form_fields['shop_name'] = array(
788 - 'type' => 'text',
789 - 'title' => __('Shop name', 'wc_makecommerce_domain'),
790 - 'class' => 'input-text regular-input',
791 - );
792 - $this->form_fields['shop_phone'] = array(
793 - 'type' => 'text',
794 - 'title' => __('Shop phone', 'wc_makecommerce_domain'),
795 - 'class' => 'input-text regular-input',
796 - );
797 - $this->form_fields['shop_email'] = array(
798 - 'type' => 'text',
799 - 'title' => __('Shop email', 'wc_makecommerce_domain'),
800 - 'class' => 'input-text regular-input',
801 - );
802 - $this->form_fields['shop_address_country'] = array(
803 - 'type' => 'text',
804 - 'title' => __('Shop address country', 'wc_makecommerce_domain'),
805 - 'description' => __('Put here short country code, i.e. EE, LV, FI', 'wc_makecommerce_domain'),
806 - 'class' => 'input-text regular-input',
807 - );
808 - $this->form_fields['shop_address_city'] = array(
809 - 'type' => 'text',
810 - 'title' => __('Shop address city', 'wc_makecommerce_domain'),
811 - 'class' => 'input-text regular-input',
812 - );
813 - $this->form_fields['shop_address_street'] = array(
814 - 'type' => 'text',
815 - 'title' => __('Shop address street', 'wc_makecommerce_domain'),
816 - 'class' => 'input-text regular-input',
817 - );
818 - $this->form_fields['shop_postal_code'] = array(
819 - 'type' => 'text',
820 - 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
821 - 'class' => 'input-text regular-input',
822 - );
823 - }
824 -
825 1063 public function calculate_shipping($package = array()) {
826 1064
827 1065 $price = $this->instance_settings['price'];
828 1066
@@ -831,8 +1069,19 @@
831 1069 if ($free_shipping_min_amount && $package['contents_cost'] >= $free_shipping_min_amount) {
832 1070 $price = 0;
833 1071 }
834 1072
1073 + //check if there is a free shipping coupon (if it's free then allow free shipping)
1074 + if (isset($package['applied_coupons']) && $this->instance_settings["allow_free_shipping_coupons"] === "yes") {
1075 + foreach ($package['applied_coupons'] as $coupon_code) {
1076 + $coupon = new WC_Coupon($coupon_code);
1077 + if ($coupon->get_free_shipping() === true) {
1078 + $price = 0;
1079 + break;
1080 + }
1081 + }
1082 + }
1083 +
835 1084 $rate = array(
836 1085 'id' => $this->get_rate_id(),
837 1086 'label' => $this->title,
838 1087 'cost' => $price,
@@ -841,22 +1090,14 @@
841 1090 );
842 1091 $this->add_rate( $rate );
843 1092 }
844 1093
845 - public function calculate_weight($package) {
846 - $weight = 0;
847 - foreach ($package['contents'] as $line) {
848 - $weight += $line['data']->get_weight();
849 - }
850 - return $weight;
851 - }
852 -
853 1094 public function is_available($package) {
854 -
855 - $package_weight = $this->calculate_weight($package);
856 - if ($package_weight >= $this->maximum_weight) {
1095 + //check if cart weight exceeds max weight
1096 + if (WC()->cart->cart_contents_weight > $this->maximum_weight) {
857 1097 return false;
858 1098 }
1099 +
859 1100 $is_available = $this->enable === 'yes';
860 1101 if (!$is_available || !mk_is_api_set()) {
861 1102 return false;
862 1103 }
@@ -861,16 +1102,377 @@
861 1102 return false;
862 1103 }
863 1104
864 1105 $this->order_country = $package['destination']['country'];
1106 +
865 1107 return apply_filters('woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package);
866 1108 }
867 1109
868 1110 public function add_admin_courier_via_field($order) {
869 - return ' <small>(aaa via ' . $this->name_ext . ' ' . __('Courier', 'wc_makecommerce_domain') . ')</small>';
1111 + return ' <small>(via ' . $this->name_ext . ' ' . __('Courier', 'wc_makecommerce_domain') . ')</small>';
870 1112 }
871 1113
872 1114 }
1115 +
1116 +
1117 + $enable = get_option('mk_transport_courier_omniva', 'yes');
1118 + if ($enable === 'yes') {
1119 + class WC_Courier_Shipping_Method_Omniva extends WC_Courier_Shipping_Method {
1120 +
1121 + public function __construct($instance_id = 0) {
1122 + $this->ext = 'Omniva';
1123 + $this->name_ext = 'Omniva';
1124 + $this->carrier = 'omniva';
1125 + parent::__construct($instance_id);
1126 + }
1127 +
1128 + public function init_form_fields() {
1129 +
1130 + global $woocommerce;
1131 +
1132 + $this->instance_form_fields = array();
1133 + $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1134 + $this->instance_form_fields['price'] = array(
1135 + 'title' => __('Shipping price', 'wc_makecommerce_domain'),
1136 + 'type' => 'text',
1137 + 'default' => '4.95'
1138 + );
1139 + $this->instance_form_fields['free_shipping_min_amount'] = array(
1140 + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
1141 + 'type' => 'number',
1142 + 'default' => '0',
1143 + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
1144 + );
1145 + $this->instance_form_fields['allow_free_shipping_coupons'] = array(
1146 + 'title' => __('Free shipping coupons', 'wc_makecommerce_domain'),
1147 + 'type' => 'checkbox',
1148 + 'default' => 'no',
1149 + 'desc_tip' => __('Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain'),
1150 + );
1151 +
1152 + $this->form_fields = array();
1153 + $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1154 + $this->form_fields['generic'] = array(
1155 + 'type' => 'title',
1156 + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
1157 + // 'description' => __('', 'wc_makecommerce_domain'),
1158 + );
1159 + $this->form_fields['active'] = array(
1160 + 'title' => __('Enable', 'wc_makecommerce_domain'),
1161 + 'type' => 'checkbox',
1162 + 'label' => __('enabled', 'wc_makecommerce_domain'),
1163 + 'default' => 'no',
1164 + );
1165 + $this->form_fields['maximum_weight'] = array(
1166 + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
1167 + 'type' => 'text',
1168 + 'default' => '60'
1169 + );
1170 +
1171 + $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'));
1172 +
1173 + if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) {
1174 + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1175 + }
1176 + else if (function_exists('pll_languages_list')) {
1177 + $pl_locales = pll_languages_list( array('fields'=>'locale') );
1178 + foreach ($pl_locales as $key => $locale_pl ) {
1179 + $language = array( 'id' => $key,
1180 + 'code' => $locale_pl) ;
1181 + $languages[$locale_pl] = $language;
1182 + }
1183 + }
1184 +
1185 + if (empty($languages)) {
1186 + $this->form_fields['method_name'] = array(
1187 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1188 + 'type' => 'text',
1189 + 'default' => __('Omniva Courier', 'wc_makecommerce_domain')
1190 + );
1191 + } else {
1192 + foreach ($languages as $language_code => $language) {
1193 + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1194 + $defaultName = __('Omniva Courier', 'wc_makecommerce_domain');
1195 + switch (substr($language_code, 0, 2)) {
1196 + case 'et':
1197 + $defaultName = 'Omniva kuller';
1198 + break;
1199 + case 'ru':
1200 + $defaultName = 'Omniva Курьер';
1201 + break;
1202 + case 'fi':
1203 + $defaultName = 'Omniva kuriiri';
1204 + break;
1205 + case 'lv':
1206 + $defaultName = 'Omniva kurjers';
1207 + break;
1208 + case 'lt':
1209 + $defaultName = 'Omniva kurjeris';
1210 + break;
1211 + }
1212 + $this->form_fields['method_name_'.substr($language_code, 0, 2)] = array(
1213 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
1214 + 'type' => 'text',
1215 + 'default' => $defaultName
1216 + );
1217 + }
1218 + }
1219 +
1220 + 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';
1221 + $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)));
1222 +
1223 + $this->form_fields['registerOnPaymentCode'] = array(
1224 + 'title' => __('Register shipments automatically as', 'wc_makecommerce_domain'),
1225 + 'type' => 'select',
1226 + 'label' => __('register on Payment', 'wc_makecommerce_domain'),
1227 + 'description' => __('Shipments will be automatically registered on payment using the selected Omniva service code', 'wc_makecommerce_domain'),
1228 + 'options' => array(
1229 + 'QP' => __('QP - handover to Omniva at Post Office', 'wc_makecommerce_domain'),
1230 + 'PK' => __('PK - handover to Omniva via Parcel Machine', 'wc_makecommerce_domain'),
1231 + '' => __('None - do not register automatically', 'wc_makecommerce_domain'),
1232 + ),
1233 + 'default' => 'QP',
1234 + );
1235 + $this->form_fields['service_carrier'] = array(
1236 + 'type' => 'select',
1237 + 'title' => __('Integration country', 'wc_makecommerce_domain'),
1238 + 'options' => array(
1239 + "OMNIVA" => __('Estonia', 'wc_makecommerce_domain'),
1240 + "OMNIVA_LV" => __('Latvia', 'wc_makecommerce_domain'),
1241 + "OMNIVA_LT" => __('Lithuania', 'wc_makecommerce_domain'),
1242 + ),
1243 + 'default' => "ee",
1244 + 'description' => __("Which country's carrier gave you the credentials", 'wc_makecommerce_domain'),
1245 + );
1246 + $this->form_fields['service_user'] = array(
1247 + 'title' => __('Omniva web services username', 'wc_makecommerce_domain'),
1248 + 'type' => 'text',
1249 + 'default' => ''
1250 + );
1251 + $this->form_fields['service_password'] = array(
1252 + 'title' => __('Omniva web services password', 'wc_makecommerce_domain'),
1253 + 'type' => 'text',
1254 + 'default' => ''
1255 + );
1256 + $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'));
1257 + $this->form_fields['shop_name'] = array(
1258 + 'type' => 'text',
1259 + 'title' => __('Shop name', 'wc_makecommerce_domain'),
1260 + 'class' => 'input-text regular-input',
1261 + );
1262 + $this->form_fields['shop_phone'] = array(
1263 + 'type' => 'text',
1264 + 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1265 + 'class' => 'input-text regular-input',
1266 + );
1267 + $this->form_fields['shop_email'] = array(
1268 + 'type' => 'text',
1269 + 'title' => __('Shop email', 'wc_makecommerce_domain'),
1270 + 'class' => 'input-text regular-input',
1271 + );
1272 + $this->form_fields['shop_address_country'] = array(
1273 + 'type' => 'select',
1274 + 'title' => __('Shop address country', 'wc_makecommerce_domain'),
1275 + 'options' => array(
1276 + 'EE' => 'EE',
1277 + 'LV' => 'LV',
1278 + 'LT' => 'LT',
1279 + ),
1280 + 'default' => 'EE',
1281 + );
1282 + $this->form_fields['shop_address_city'] = array(
1283 + 'type' => 'text',
1284 + 'title' => __('Shop address city', 'wc_makecommerce_domain'),
1285 + 'class' => 'input-text regular-input',
1286 + );
1287 + $this->form_fields['shop_address_street'] = array(
1288 + 'type' => 'text',
1289 + 'title' => __('Shop address street', 'wc_makecommerce_domain'),
1290 + 'class' => 'input-text regular-input',
1291 + );
1292 + $this->form_fields['shop_postal_code'] = array(
1293 + 'type' => 'text',
1294 + 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
1295 + 'class' => 'input-text regular-input',
1296 + );
1297 + }
1298 + }
1299 + }
1300 +
1301 + $enable = get_option('mk_transport_courier_smartpost', 'yes');
1302 + if ($enable === 'yes') {
1303 + class WC_Courier_Shipping_Method_SmartPost extends WC_Courier_Shipping_Method {
1304 +
1305 + public function __construct($instance_id = 0) {
1306 + $this->ext = 'SmartPost';
1307 + $this->name_ext = 'SmartPost';
1308 + $this->carrier = 'smartpost';
1309 + add_filter('woocommerce_review_order_after_shipping' , array(&$this, 'add_smartpost_courier_checkout_fields'));
1310 + add_action('woocommerce_checkout_process', array(&$this, 'check_checkout_fields'));
1311 + add_action('woocommerce_after_checkout_validation', array(&$this, 'check_checkout_fields'));
1312 + add_action('woocommerce_checkout_update_order_meta', array(&$this, 'add_order_meta'));
1313 + parent::__construct($instance_id);
1314 + }
1315 +
1316 + public function add_smartpost_courier_checkout_fields($fragment) {
1317 + $res = '';
1318 + $res .= '<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_courier_'.mb_strtolower($this->ext).'">';
1319 + $res .= '<td colspan="2">';
1320 + $res .= '<p class="form-row" id="'.esc_attr($this->id).'_field">';
1321 + $res .= '<label class="courier_arrival_time_window_label">'.__('Pick courier arrival time window', 'wc_makecommerce_domain').'</label>';
1322 + $res .= '<select class="select" name="'.esc_attr($this->id).'" id="'.esc_attr($this->id).'">';
1323 + $res .= '<option value="1">'.__('Any time', 'wc_makecommerce_domain').'</option>';
1324 + $res .= '<option value="2">'.__('Worktime (09:00..17:00)', 'wc_makecommerce_domain').'</option>';
1325 + $res .= '<option value="3">'.__('After worktime (17:00..21:00)', 'wc_makecommerce_domain').'</option>';
1326 + $res .= '</select>';
1327 + $res .= '</p>';
1328 + $res .= '</td></tr>';
1329 + echo $res;
1330 + }
1331 +
1332 + function check_checkout_fields() {
1333 + #static $added = false;
1334 + #if (!$added && empty($_POST[$this->id])) {
1335 + # wc_add_notice(__('<strong>Delivery time</strong> is a required field.', 'wc_makecommerce_domain'), 'error');
1336 + # $added = true;
1337 + #}
1338 + }
1339 +
1340 + function add_order_meta($order_id) {
1341 + if (!empty($_POST[$this->id])) {
1342 + update_post_meta($order_id, '_delivery_time', sanitize_text_field($_POST[$this->id]));
1343 + }
1344 + }
1345 +
1346 + public function init_form_fields() {
1347 +
1348 + global $woocommerce;
1349 +
1350 + $this->instance_form_fields = array();
1351 + $this->instance_form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1352 + $this->instance_form_fields['price'] = array(
1353 + 'title' => __('Shipping price', 'wc_makecommerce_domain'),
1354 + 'type' => 'text',
1355 + 'default' => '4.95'
1356 + );
1357 + $this->instance_form_fields['free_shipping_min_amount'] = array(
1358 + 'title' => __('Free shipping amount', 'wc_makecommerce_domain'),
1359 + 'type' => 'number',
1360 + 'default' => '0',
1361 + 'desc_tip' => __('(0 means no free shipping)', 'wc_makecommerce_domain'),
1362 + );
1363 + $this->instance_form_fields['allow_free_shipping_coupons'] = array(
1364 + 'title' => __('Free shipping coupons', 'wc_makecommerce_domain'),
1365 + 'type' => 'checkbox',
1366 + 'default' => 'no',
1367 + 'desc_tip' => __('Allow using free shipping coupons to be used with this method', 'wc_makecommerce_domain'),
1368 + );
1369 +
1370 + $this->form_fields = array();
1371 + $this->form_fields['logo'] = array('type' => 'title', 'description' => __get_logo_html());
1372 + $this->form_fields['generic'] = array(
1373 + 'type' => 'title',
1374 + 'title' => __('Generic and pricing options', 'wc_makecommerce_domain'),
1375 + // 'description' => __('', 'wc_makecommerce_domain'),
1376 + );
1377 + $this->form_fields['active'] = array(
1378 + 'title' => __('Enable', 'wc_makecommerce_domain'),
1379 + 'type' => 'checkbox',
1380 + 'label' => __('enabled', 'wc_makecommerce_domain'),
1381 + 'default' => 'no',
1382 + );
1383 + $this->form_fields['maximum_weight'] = array(
1384 + 'title' => sprintf(__('Maximum weight allowed for shipping (%s)', 'wc_makecommerce_domain'), get_option('woocommerce_weight_unit' )),
1385 + 'type' => 'text',
1386 + 'default' => '60'
1387 + );
1388 +
1389 + $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'));
1390 +
1391 + if ( function_exists('icl_object_id') && !function_exists('pll_languages_list')) {
1392 + $languages = apply_filters('wpml_active_languages', NULL, 'skip_missing=0');
1393 + }
1394 + else if (function_exists('pll_languages_list')) {
1395 + $pl_locales = pll_languages_list( array('fields'=>'locale') );
1396 + foreach ($pl_locales as $key => $locale_pl ) {
1397 + $language = array( 'id' => $key,
1398 + 'code' => $locale_pl) ;
1399 + $languages[$locale_pl] = $language;
1400 + }
1401 + }
1402 +
1403 + if (empty($languages)) {
1404 + $this->form_fields['method_name'] = array(
1405 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain'),
1406 + 'type' => 'text',
1407 + 'default' => __('SmartPOST Courier', 'wc_makecommerce_domain')
1408 + );
1409 + } else {
1410 + foreach ($languages as $language_code => $language) {
1411 + $language_name = !empty($language['translated_name']) ? $language['translated_name'] : $language_code;
1412 + $defaultName = __('SmartPOST Courier', 'wc_makecommerce_domain');
1413 + switch (substr($language_code, 0, 2)) {
1414 + case 'et':
1415 + $defaultName = 'SmartPOST kuller';
1416 + break;
1417 + case 'ru':
1418 + $defaultName = 'SmartPOST Курьер';
1419 + break;
1420 + case 'fi':
1421 + $defaultName = 'SmartPOST kuriiri';
1422 + break;
1423 + case 'lv':
1424 + $defaultName = 'SmartPOST kurjers';
1425 + break;
1426 + case 'lt':
1427 + $defaultName = 'SmartPOST kurjeris';
1428 + break;
1429 + }
1430 + $this->form_fields['method_name_'.substr($language_code, 0, 2)] = array(
1431 + 'title' => __('Shipping Method Title', 'wc_makecommerce_domain').sprintf(' (%s)', substr($language_code, 0, 2)),
1432 + 'type' => 'text',
1433 + 'default' => $defaultName
1434 + );
1435 + }
1436 + }
1437 +
1438 + 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';
1439 + $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)));
1440 +
1441 + $this->form_fields['service_user'] = array(
1442 + 'title' => __('eservice.smartpost.ee username', 'wc_makecommerce_domain'),
1443 + 'type' => 'text',
1444 + 'default' => ''
1445 + );
1446 + $this->form_fields['service_password'] = array(
1447 + 'title' => __('eservice.smartpost.ee password', 'wc_makecommerce_domain'),
1448 + 'type' => 'text',
1449 + 'default' => ''
1450 + );
1451 + $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'));
1452 + $this->form_fields['shop_name'] = array(
1453 + 'type' => 'text',
1454 + 'title' => __('Shop name', 'wc_makecommerce_domain'),
1455 + 'class' => 'input-text regular-input',
1456 + );
1457 + $this->form_fields['shop_phone'] = array(
1458 + 'type' => 'text',
1459 + 'title' => __('Shop phone', 'wc_makecommerce_domain'),
1460 + 'class' => 'input-text regular-input',
1461 + );
1462 + $this->form_fields['shop_email'] = array(
1463 + 'type' => 'text',
1464 + 'title' => __('Shop email', 'wc_makecommerce_domain'),
1465 + 'class' => 'input-text regular-input',
1466 + );
1467 + $this->form_fields['shop_postal_code'] = array(
1468 + 'type' => 'text',
1469 + 'title' => __('Shop postal code', 'wc_makecommerce_domain'),
1470 + 'class' => 'input-text regular-input',
1471 + );
1472 + }
1473 + }
1474 + }
873 1475 }
874 1476 }
875 1477 add_action('woocommerce_shipping_init', 'transport_add_method');
876 1478
@@ -877,9 +1479,11 @@
877 1479
878 1480 function add_mc_shipping_method($methods) {
879 1481 $methods['parcelmachine_omniva'] = 'WC_ParcelMachine_Shipping_Method_Omniva';
880 1482 $methods['parcelmachine_smartpost'] = 'WC_ParcelMachine_Shipping_Method_Smartpost';
1483 + $methods['parcelmachine_dpd'] = 'WC_ParcelMachine_Shipping_Method_DPD';
881 1484 $methods['courier_omniva'] = 'WC_Courier_Shipping_Method_Omniva';
1485 + $methods['courier_smartpost'] = 'WC_Courier_Shipping_Method_Smartpost';
882 1486 return $methods;
883 1487 }
884 1488 add_filter('woocommerce_shipping_methods', 'add_mc_shipping_method');
885 1489
@@ -886,9 +1490,14 @@
886 1490 function mk_mc_add_assets() {
887 1491 wp_enqueue_style('parcelmachine-css', plugin_dir_url(__FILE__).'/css/parcelmachine.css');
888 1492 wp_enqueue_script('parcelmachine-js', plugin_dir_url(__FILE__).'/scripts/parcelmachine.js', array('jquery'));
889 1493 }
1494 + function mk_mc_add_assets_admin() {
1495 + wp_enqueue_style('parcelmachine-css-admin', plugin_dir_url(__FILE__).'/css/parcelmachine-admin.css');
1496 + wp_enqueue_script('parcelmachine-js-admin', plugin_dir_url(__FILE__).'/scripts/parcelmachine-admin.js', array('jquery'));
1497 + }
890 1498 add_action('wp_enqueue_scripts', 'mk_mc_add_assets');
1499 + add_action('admin_enqueue_scripts', 'mk_mc_add_assets_admin');
891 1500
892 1501 add_action('woocommerce_admin_field_api_javascript_ui', 'api_javascript_ui');
893 1502
894 1503
@@ -894,9 +1503,9 @@
894 1503
895 1504
896 1505 // ======== Woo Admin dialogs related stuff
897 1506
898 - // PP: mis need 2 funktsiooni teevad?
1507 +
899 1508 function mk_admin_restrict_manage_posts() {
900 1509 global $typenow;
901 1510 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
902 1511 $selected_method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
@@ -908,9 +1517,9 @@
908 1517 }
909 1518 echo '</select>';
910 1519 }
911 1520 }
912 - function mk_admin_shipping_filter($where, &$wp_query) {
1521 + function mk_admin_shipping_filter($where, $wp_query) {
913 1522 global $pagenow, $wpdb;
914 1523 $method = !empty($_REQUEST['_shipping_method']) ? $_REQUEST['_shipping_method'] : false;
915 1524 if (is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty($method) ) {
916 1525 $where .= $GLOBALS['wpdb']->prepare( ' AND ID
@@ -927,8 +1536,10 @@
927 1536 add_filter('restrict_manage_posts', 'mk_admin_restrict_manage_posts');
928 1537 add_filter('posts_where', 'mk_admin_shipping_filter', 10, 2);
929 1538
930 1539 function mk_admin_bulk_actions() {
1540 + wp_enqueue_script('jquery-ui-core');
1541 + wp_enqueue_script('jquery-ui-datepicker');
931 1542 wp_enqueue_script('wc_mk_timepicker', plugins_url('/scripts/jquery-ui.timepicker.js', __FILE__));
932 1543 wp_enqueue_style('wc_mk_timepicker-css', plugins_url('/css/jquery-ui.timepicker.css', __FILE__));
933 1544 global $post_type;
934 1545 if ('shop_order' === $post_type) { ?>
@@ -958,8 +1569,15 @@
958 1569 now.setMinutes(rounded);
959 1570 jQuery('.timepicker').datetimepicker({dateFormat: 'dd/mm/yy', timeFormat: 'HH:mm', stepMinute: 15, minDateTime: now, devaultValue: jQuery.datepicker.formatTime('dd/mm/yyyy HH:mm', now)});
960 1571 });
961 1572 </script>
1573 + <?php if (!empty($_REQUEST['mk_err'])) { ?>
1574 + <script type="text/javascript">
1575 + jQuery(function(){
1576 + alert('<?php echo htmlspecialchars($_REQUEST['mk_err']) ?>');
1577 + });
1578 + </script>
1579 + <?php } ?>
962 1580 <?php if (!empty($_REQUEST['mk_pdf'])) { ?>
963 1581 <script type="text/javascript">
964 1582 jQuery(function(){
965 1583 window.open('<?php echo $_REQUEST['mk_pdf'] ?>', 'pdf');
@@ -993,83 +1611,314 @@
993 1611 add_action('admin_action_parcel_machine_labels', 'mk_admin_bulk_action_labels');
994 1612 add_action('admin_action_parcel_machine_print_labels', 'mk_admin_bulk_action_print');
995 1613 add_action('admin_action_parcel_machine_order_courier', 'mk_admin_bulk_action_order_courier');
996 1614
1615 + function mk_admin_save_post($post_id) {
1616 + $post_type = get_post_type($post_id);
1617 + if ('shop_order' !== $post_type) { return; }
1618 + if (isset($_POST['_mk_machine_id'])) {
1619 + update_post_meta($post_id, '_parcel_machine', $_POST['_mk_machine_id']);
1620 + mk_get_shipment_ids($post_id);
1621 + }
1622 + }
997 1623
1624 + add_action('save_post', 'mk_admin_save_post');
998 1625
1626 +
1627 +
999 1628 function mk_admin_parcelmachine_order_meta($order) {
1000 - $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1629 + echo '</div>';
1630 + echo '<div class="order_data_column" style="float: right;">';
1631 + $order_id = $order->get_id();
1632 + $machine_id = get_post_meta($order_id, '_parcel_machine', true);
1001 1633 if (!empty($machine_id)) {
1002 1634 list($carrier, $machine) = explode('||', $machine_id);
1003 1635 if (!empty($machine)) {
1004 - $machine = mk_get_machine($carrier, (int)$machine);
1636 + $machine = mk_get_machine($carrier, $machine);
1005 1637 if ($machine) {
1006 - echo '<p><strong>'.__('Parcel machine', 'wc_makecommerce_domain').'</strong> ('. $carrier .'):<br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>';
1638 + echo '<h3>'.__('Selected parcel machine', 'wc_makecommerce_domain').'<a href="#" class="edit_address">'.__('Edit').'</a></h3>';
1639 + echo '<div class="edit_address">';
1640 + echo '<p class="form-field form-field-wide _mk_machine_id">';
1641 + echo '<label for="_mk_machine_id">'.__('Parcel machine', 'wc_makecommerce_domain').'</label>';
1642 + echo '<select id="_mk_machine_id" name="_mk_machine_id" class="wc-enhanced-select select">';
1643 + $res = '';
1644 + $machines = mk_get_machines($carrier, method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : $order->shipping_country);
1645 + $res .= '<option value="">'.__('-- select parcel machine --', 'wc_makecommerce_domain').'</option>';
1646 + $pcity = false;
1647 + foreach ($machines as $smachine) {
1648 + $city = strtolower($smachine['city']);
1649 + if ($city !== $pcity) {
1650 + if ($pcity) $res .= '</optgroup>';
1651 + $res .= '<optgroup label="'.$smachine['city'].'">';
1652 + }
1653 + $mname = $smachine['name'];
1654 + $res .= '<option value="'.esc_attr($smachine['carrier'].'||'.$smachine['id']).'" '.($machine['id'] === $smachine['id'] ? ' selected="selected"' : '').'>'.$mname.'</option>';
1655 + $pcity = $city;
1656 + }
1657 + if ($pcity) $res .= '</optgroup>';
1658 + echo $res;
1659 + echo '</select>';
1660 + echo '</p>';
1661 + echo '</div>';
1662 + echo '<div class="address">';
1663 + echo '<p>'.$carrier.'</strong><br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>';
1664 + echo '</div>';
1007 1665 }
1008 1666 }
1009 1667 }
1668 +
1010 1669 if (empty($carrier)) {
1011 1670 $carrier = '';
1012 1671 }
1013 1672
1014 - $shipment_id = get_post_meta($order->id, '_parcel_machine_shipment_id', true);
1015 - $shipment_id_error = get_post_meta($order->id, '_parcel_machine_error', true);
1016 - $shipment_manifest = get_post_meta($order->id, '_parcel_machine_manifest', true);
1017 - if ($shipment_id) {
1018 - switch ($carrier) {
1019 - case 'omniva':
1020 - echo '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').':</strong><br/> <a target="_blank "href="https://www.omniva.ee/abi/jalgimine?barcode=' . $shipment_id . '">'.$shipment_id.'</a>' ;
1021 - break;
1022 - default:
1023 - echo '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').':</strong><br/>' . $shipment_id . '</small></p>';
1024 - break;
1673 + $shipment_id = get_post_meta($order_id, '_parcel_machine_shipment_id', true);
1674 + $shipment_id_error = get_post_meta($order_id, '_parcel_machine_error', true);
1675 + $shipment_manifest = get_post_meta($order_id, '_parcel_machine_manifest', true);
1676 +
1677 + //currently the only delivery time option exists for smartpost courier shipping method.
1678 + $showDeliveryTime = false;
1679 + foreach ($order->get_shipping_methods() as $shippingMethod) {
1680 + if ($shippingMethod["method_id"] == "courier_smartpost") {
1681 + $showDeliveryTime = true;
1025 1682 }
1026 1683 }
1684 +
1685 + $delivery_time = get_post_meta($order_id, '_delivery_time', true);
1686 + if (!$shipment_id_error && $showDeliveryTime) {
1687 + if ($delivery_time === '1') {
1688 + echo '<p><strong>'.__('Delivery time', 'wc_makecommerce_domain').'</strong> - '.__('Any time', 'wc_makecommerce_domain').'</p>';
1689 + }
1690 +
1691 + if ($delivery_time === '2') {
1692 + echo '<p><strong>'.__('Delivery time', 'wc_makecommerce_domain').'</strong> - 09:00..17:00</p>';
1693 + }
1694 +
1695 + if ($delivery_time === '3') {
1696 + echo '<p><strong>'.__('Delivery time', 'wc_makecommerce_domain').'</strong> - 17:00..21:00</p>';
1697 + }
1698 + }
1699 +
1700 + if ($shipment_id) {
1701 + $trackinglink = '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').'</strong>';
1702 +
1703 + //check if its omniva courier
1704 + if ($carrier == "") {
1705 + foreach ($order->get_shipping_methods() as $shippingMethod) {
1706 + if ($shippingMethod["method_id"] == "courier_omniva") {
1707 + $carrier = "omniva";
1708 + }
1709 + }
1710 + }
1711 +
1712 + $theLink = get_tracking_link($carrier, $shipment_id, $order_id, true);
1713 + if ($carrier != "" && $theLink != "") {
1714 + $trackinglink .= ' ('. ucfirst($carrier) .'): <br/> <a target="_blank" href="'.$theLink.$shipment_id.'">'.$shipment_id.'</a>';
1715 + } else {
1716 + $trackinglink .= ': <br/>'.$shipment_id;
1717 + }
1718 +
1719 + echo $trackinglink.'</p>';
1720 + }
1721 +
1027 1722 if ($shipment_manifest) {
1028 1723 echo '<p><strong>'.__('Shipments pickup manifest').':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>';
1029 1724 }
1725 +
1030 1726 if ($shipment_id_error) {
1031 1727 echo '<p><strong style="color: red;">'.__('Shipment registration error').':</strong><br/>' . $shipment_id_error . '</small></p>';
1032 1728 }
1729 +
1730 + echo '</div>';
1033 1731 }
1034 1732
1733 + //returns the correct link to be used for tracking information
1734 + function get_tracking_link($carrier, $order_id, $shopLocation = false)
1735 + {
1736 + //do we use shop location or delivery location
1737 + if ($shopLocation) {
1738 + //get_base_country returns either EE, LT or LV. Everything else is irrelevant
1739 + $tld = substr(strtolower(WC()->countries->get_base_country()), 0, 2);
1740 + /* $userLang = get_locale();//en_, lv_, et_, lt_, ru_ <- this could be used aswell */
1741 + $userLang = $tld;
1742 + } else {
1743 + //get order delivery location. Returns EE, LT or LV. Evertyhing else is irrelevant
1744 + $tld = substr(strtolower(get_post_meta($order_id, '_shipping_country', true)), 0, 2);
1745 + $userLang = substr(strtolower(get_post_meta($order_id, 'wpml_language', true)), 0, 2); //returns nothing if it doesnt exist, otherwise returns en, et, lt, lv, ru
1746 + }
1747 +
1748 + //
1749 + // create array for different options [carrier][tld][lang]
1750 + // first one in the list must be default, this goes for the tld aswell as for the userLang.
1751 + //
1752 +
1753 + /* Omniva */
1754 + //EST
1755 + $trackingURLS["omniva"]["ee"]["ee"] = 'https://www.omniva.ee/abi/jalgimine?barcode=';
1756 + $trackingURLS["omniva"]["ee"]["en"] = 'https://www.omniva.ee/private/track_and_trace?barcode=';
1757 + $trackingURLS["omniva"]["ee"]["ru"] = 'https://www.omniva.ee/chastnyj/otslezhivanie_posylki?barcode=';
1758 +
1759 + //LT
1760 + $trackingURLS["omniva"]["lt"]["lt"] = 'https://www.omniva.lt/verslo/siuntos_sekimas?barcode=';
1761 + $trackingURLS["omniva"]["lt"]["en"] = 'https://www.omniva.lt/business/track_and_trace?barcode=';
1762 +
1763 + //LV
1764 + $trackingURLS["omniva"]["lv"]["lv"] = 'https://www.omniva.lv/privats/sutijuma_atrasanas_vieta?barcode=';
1765 + $trackingURLS["omniva"]["lv"]["en"] = 'https://www.omniva.lv/private/track_and_trace?barcode=';
1766 + $trackingURLS["omniva"]["lv"]["ru"] = 'https://www.omniva.lv/chastnyj/mestonahozhdenie_posylki?barcode=';
1767 +
1768 + /* DPD */
1769 + //EE
1770 + $trackingURLS["dpd"]["ee"]["ee"] = 'https://tracking.dpd.de/status/et_EE/parcel/';
1771 + $trackingURLS["dpd"]["ee"]["en"] = 'https://tracking.dpd.de/status/en_EE/parcel/';
1772 +
1773 + //LT
1774 + $trackingURLS["dpd"]["lt"]["lt"] = 'https://tracking.dpd.de/status/lt_LT/parcel/';
1775 + $trackingURLS["dpd"]["lt"]["en"] = 'https://tracking.dpd.de/status/en_LT/parcel/';
1776 +
1777 + //LV
1778 + $trackingURLS["dpd"]["lv"]["lv"] = 'https://tracking.dpd.de/status/lv_LV/parcel/';
1779 + $trackingURLS["dpd"]["lv"]["en"] = 'https://tracking.dpd.de/status/en_LV/parcel/';
1780 +
1781 + //carrier exists
1782 + if (isset($trackingURLS[$carrier])) {
1783 + //country exists
1784 + if (isset($trackingURLS[$carrier][$tld])) {
1785 + if (isset($trackingURLS[$carrier][$tld][$userLang])) {
1786 + return $trackingURLS[$carrier][$tld][$userLang];
1787 + } else {
1788 + //selected language not found, return carrier tld default language.
1789 + return current($trackingURLS[$carrier][$tld]);
1790 + }
1791 + } else {
1792 + //selected tld not found, return carrier default tld + language
1793 + $defaultCarrierTLD = current($trackingURLS[$carrier]);
1794 + return current($defaultCarrierTLD);
1795 + }
1796 + }
1797 +
1798 + //not even carrier was found. Return nothing
1799 + return "";
1800 + }
1801 +
1035 1802 function mk_admin_render_shop_order_columns( $column ) {
1036 1803 global $post, $woocommerce, $the_order;
1037 - if (empty($the_order) || $the_order->id != $post->ID) {
1804 + if (!$the_order) return;
1805 + $the_order_id = $the_order->get_id();
1806 + if (empty($the_order) || $the_order_id != $post->ID) {
1038 1807 $the_order = wc_get_order($post->ID);
1039 1808 }
1040 1809 if ($column === 'shipping_address') {
1041 - $machine_id = get_post_meta($the_order->id, '_parcel_machine', true);
1042 - $shipment_id = get_post_meta($the_order->id, '_parcel_machine_shipment_id', true);
1043 - $shipment_id_error = get_post_meta($the_order->id, '_parcel_machine_error', true);
1810 + $machine_id = get_post_meta($the_order_id, '_parcel_machine', true);
1811 + $shipment_id = get_post_meta($the_order_id, '_parcel_machine_shipment_id', true);
1812 + $shipment_id_error = get_post_meta($the_order_id, '_parcel_machine_error', true);
1044 1813 if ($machine_id && !$shipment_id) {
1045 1814 //echo __('Shipment ID not generated for delivery', 'wc_makecommerce_domain');
1046 1815 }
1047 1816 if ($shipment_id) {
1048 - echo __('Shipment tracking code', 'wc_makecommerce_domain') . ': ' . $shipment_id;
1817 + echo __('Shipment tracking code', 'wc_makecommerce_domain') . ': ' . $shipment_id . '<br/>';
1818 + echo '<span class="has_shipment_id"></span>';
1049 1819 }
1050 1820 else if ($shipment_id_error) echo '<span style="color: red;">'.__('Package shipment generation error:', 'wc_makecommerce_domain') . '</span><br/>' .$shipment_id_error;
1051 1821 }
1822 +
1052 1823 }
1053 - function mk_add_email_customer_details_fields($fields, $sent_to_admin, $order) {
1054 - $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1055 - if (empty($machine_id)) return $fields;
1056 - list($carrier, $machine) = explode('||', $machine_id);
1057 - if (empty($machine)) return $fields;
1058 - $machine = mk_get_machine($carrier, (int)$machine);
1059 - if (!$machine) return $fields;
1060 - $fields[] = array('label' => __('Parcel machine', 'wc_makecommerce_domain').' ('.$carrier.')', 'value' => $machine['name'].' - '.$machine['address']);
1824 +
1825 + function mk_add_email_customer_details_fields($fields, $sent_to_admin, $order)
1826 + {
1827 + //this only applies for Polylang. In WPML it will break
1828 + if (function_exists('pll_languages_list')) {
1829 + $orderLang = get_locale();
1830 +
1831 + if ($orderLang != "") {
1832 + //Workaround for broken Polylang functionality:
1833 + //switch to a different than current language first. Otherwise hooks for polylang dont seem to work...
1834 + if (substr($orderLang, 0 ,2) == "en") {
1835 + switch_to_locale("et");
1836 + do_action('wpml_switch_language', "et");
1837 + } else {
1838 + switch_to_locale("en_GB");
1839 + do_action('wpml_switch_language', "en_GB");
1840 + }
1841 +
1842 + //switch back to original language
1843 + switch_to_locale($orderLang);
1844 + do_action('wpml_switch_language', $orderLang);
1845 + load_plugin_textdomain('wc_makecommerce_domain', false, dirname(plugin_basename(__FILE__)) . '/languages/');
1846 + }
1847 + }
1848 +
1849 + $machine_id = get_post_meta($order->get_id(), '_parcel_machine', true);
1850 +
1851 + $machine = "";
1852 + $carrier = "";
1853 +
1854 + $machineIdArr = explode('||', $machine_id);
1855 + if (count($machineIdArr) == 2) {
1856 + $carrier = $machineIdArr[0];
1857 + $machine = $machineIdArr[1];
1858 + } else if (count($machineIdArr) == 1) {
1859 + $carrier = $machineIdArr[0];
1860 + }
1861 +
1862 + $machine = mk_get_machine($carrier, $machine);
1863 +
1864 + if ($machine != "" && $carrier != "") {
1865 + //add parcel machine name/location
1866 + $fields[] = array('label' => __('Parcel machine', 'wc_makecommerce_domain').' ('.ucfirst($carrier).')', 'value' => $machine['name'].' - '.$machine['address']);
1867 + }
1868 +
1869 + $shipment_id_error = get_post_meta($order->get_id(), '_parcel_machine_error', true);
1870 +
1871 + //currently the only delivery time option exists for smartpost courier shipping method.
1872 + $showDeliveryTime = false;
1873 + foreach ($order->get_shipping_methods() as $shippingMethod) {
1874 + if ($shippingMethod["method_id"] == "courier_smartpost") {
1875 + $showDeliveryTime = true;
1876 + }
1877 + }
1878 +
1879 + $delivery_time = get_post_meta($order->get_id(), '_delivery_time', true);
1880 + if (!$shipment_id_error && $showDeliveryTime) {
1881 + if ($delivery_time === '1') {
1882 + $fields[] = array('label' => __('Delivery time', 'wc_makecommerce_domain'), 'value' => __('Any time', 'wc_makecommerce_domain'));
1883 + }
1884 +
1885 + if ($delivery_time === '2') {
1886 + $fields[] = array('label' => __('Delivery time', 'wc_makecommerce_domain'), 'value' => "09:00..17:00");
1887 + }
1888 +
1889 + if ($delivery_time === '3') {
1890 + $fields[] = array('label' => __('Delivery time', 'wc_makecommerce_domain'), 'value' => "17:00..21:00");
1891 + }
1892 + }
1893 +
1894 + //add tracking information (if possible)
1895 + $shipment_id = get_post_meta($order->get_id(), "_parcel_machine_shipment_id", true);
1896 +
1897 + if ($shipment_id) {
1898 + $trackinglink = '<p><strong>'.__('Shipment tracking code', 'wc_makecommerce_domain').'</strong>';
1899 +
1900 + $href = get_tracking_link($carrier, $order->get_id());
1901 + if ($href != "") {
1902 + $href .= $shipment_id;
1903 + $fields[] = array('label' => __('Shipment tracking info', 'wc_makecommerce_domain').' ('.$carrier.')', 'value' => "<a target='_blank' title='".__('Shipment tracking code', 'wc_makecommerce_domain')."' href='".$href."'>".$href."</a>");
1904 + } else {
1905 + $fields[] = array('label' => __('Shipment tracking info', 'wc_makecommerce_domain'), 'value' => $shipment_id);
1906 + }
1907 + }
1908 +
1061 1909 return $fields;
1062 1910 }
1911 +
1063 1912 function mk_add_order_customer_details_fields($order) {
1064 - $machine_id = get_post_meta($order->id, '_parcel_machine', true);
1913 + $machine_id = get_post_meta($order->get_id(), '_parcel_machine', true);
1065 1914 if (empty($machine_id)) return;
1066 1915 list($carrier, $machine) = explode('||', $machine_id);
1067 1916 if (empty($machine)) return;
1068 - $machine = mk_get_machine($carrier, (int)$machine);
1917 + $machine = mk_get_machine($carrier, $machine);
1069 1918 if (!$machine) return;
1070 1919 echo '<tr>';
1071 - echo '<th>'.__('Parcel machine', 'wc_makecommerce_domain').' ('.$carrier.') </th>';
1920 + echo '<th>'.__('Parcel machine', 'wc_makecommerce_domain').' ('.ucfirst($carrier).') </th>';
1072 1921 echo '<td>'.$machine['name'].'<br/>'.$machine['address'].'</td>';
1073 1922 echo '</tr>';
1074 1923 }
1075 1924 function mk_admin_product_option_fields($fields) {
@@ -1096,8 +1945,14 @@
1096 1945 $no_shipping_cost = isset($_POST['_no_shipping_cost']) ? 'yes' : 'no';
1097 1946 update_post_meta($post_id, '_no_shipping_cost', $no_shipping_cost);
1098 1947 }
1099 1948
1949 +
1950 +
1951 +
1952 +
1953 +
1954 +
1100 1955 add_action('woocommerce_product_options_shipping', 'mk_admin_product_option_fields');
1101 1956 add_action('woocommerce_process_product_meta', 'mk_admin_product_fields_save');
1102 1957 add_action('woocommerce_admin_order_data_after_shipping_address', 'mk_admin_parcelmachine_order_meta', 10, 1);
1103 1958 add_action('manage_shop_order_posts_custom_column', 'mk_admin_render_shop_order_columns', 3);
@@ -1104,8 +1959,9 @@
1104 1959 add_filter('woocommerce_email_customer_details_fields', 'mk_add_email_customer_details_fields', 10, 3 );
1105 1960 add_action('woocommerce_order_details_after_customer_details', 'mk_add_order_customer_details_fields');
1106 1961 add_action('woocommerce_order_status_processing', 'mk_get_shipment_ids');
1107 1962
1963 +
1108 1964
1109 1965
1110 1966 // ===== MK API communication related stuff
1111 1967
@@ -1117,12 +1973,23 @@
1117 1973 $shipping_request = array('credentials' => array(), 'orders' => array());
1118 1974 $shipping_classes_map = array(
1119 1975 'parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva',
1120 1976 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_Smartpost',
1121 - 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva'
1977 + 'parcelmachine_dpd' => 'WC_ParcelMachine_Shipping_Method_DPD',
1978 + 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva',
1979 + 'courier_smartpost' => 'WC_Courier_Shipping_Method_Smartpost'
1122 1980 );
1123 1981 foreach ($post_ids as $post_id) {
1982 + $oldId = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
1983 + if (strlen($oldId)>6) continue;
1124 1984 $order = new WC_Order($post_id);
1985 +
1986 + //check if the post state is even paid, if not, ignore this post...
1987 + //this could perhaps be in a way better location. Why is the function even called when post state is set to failed.
1988 + if ((string)$order->get_status() !== "completed" && (string)$order->get_status() !== "processing") {
1989 + continue;
1990 + }
1991 +
1125 1992 $shipping_methods = $order->get_shipping_methods();
1126 1993 if (empty($shipping_methods)) { continue; }
1127 1994 foreach ($shipping_methods as $shipping_method) {
1128 1995 $shipping_id = explode(':', $shipping_method['method_id']);
@@ -1144,13 +2011,28 @@
1144 2011
1145 2012 if (empty($shipping_request['credentials'][$carrier_uc])) {
1146 2013 $api_user = $transport_class->settings['service_user'];
1147 2014 $api_password = $transport_class->settings['service_password'];
1148 - if (!$api_user || !$api_password) {
2015 +
2016 + //change carrier_uc if it has been set in the interface.
2017 + if (isset($transport_class->settings['service_carrier'])) {
2018 + $carrier_uc = $transport_class->settings['service_carrier'];
2019 + }
2020 +
2021 + $use_mk_contract = false;
2022 + if ( isset($transport_class->settings['use_mk_contract']) ) {
2023 + $use_mk_contract = $transport_class->settings['use_mk_contract'];
2024 + }
2025 +
2026 + if ( (!$api_user || !$api_password) and (!$use_mk_contract) ) {
1149 2027 add_action('admin_notices', 'mk_admin_error');
1150 2028 continue;
1151 2029 }
1152 - $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password);
2030 +
2031 + // if $use_mk_contract is enabled, do not add credentials array to the request
2032 + if (!$use_mk_contract) {
2033 + $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password);
2034 + }
1153 2035 }
1154 2036 $sender = array(
1155 2037 'name' => !empty($transport_class->settings['shop_name']) ? $transport_class->settings['shop_name'] : '',
1156 2038 'phone' => !empty($transport_class->settings['shop_phone']) ? $transport_class->settings['shop_phone'] : '',
@@ -1159,13 +2041,19 @@
1159 2041 'city' => !empty($transport_class->settings['shop_address_city']) ? $transport_class->settings['shop_address_city'] : '',
1160 2042 'street' => !empty($transport_class->settings['shop_address_street']) ? $transport_class->settings['shop_address_street'] : '',
1161 2043 'postalCode' => !empty($transport_class->settings['shop_postal_code']) ? $transport_class->settings['shop_postal_code'] : ''
1162 2044 );
1163 - $recipient_name = $order->shipping_first_name && $order->shipping_last_name ? $order->shipping_first_name . ' ' . $order->shipping_last_name : $order->billing_first_name . ' ' . $order->billing_last_name;
2045 + $s_first_name = method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : $order->shipping_first_name;
2046 + $s_last_name = method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : $order->shipping_last_name;
2047 + $b_first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name;
2048 + $b_last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name;
2049 + $b_phone = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone;
2050 + $b_email = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email;
2051 + $recipient_name = $s_first_name && $s_last_name ? $s_first_name . ' ' . $s_last_name : $b_first_name . ' ' . $b_last_name;
1164 2052 $sr_order = array(
1165 2053 'carrier' => $carrier_uc,
1166 - 'orderId' => $order->id,
1167 - 'recipient' => array('name' => $recipient_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email),
2054 + 'orderId' => $order->get_id(),
2055 + 'recipient' => array('name' => $recipient_name, 'phone' => $b_phone, 'email' => $b_email),
1168 2056 'sender' => $sender,
1169 2057 'destination' => array(),
1170 2058 );
1171 2059 $m_service_type = null;
@@ -1177,16 +2065,29 @@
1177 2065
1178 2066 if (!empty($machine_id)) {
1179 2067 $sr_order['destination']['destinationId'] = $machine_id;
1180 2068 }
1181 - if ($transport_class->carrier === 'omniva' && $transport_class->type === 'cou') {
2069 + if ($transport_class->type === 'cou') {
2070 + $s_postcode = method_exists($order, 'get_shipping_postcode') ? $order->get_shipping_postcode() : $order->shipping_postcode;
2071 + $s_country = method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : $order->shipping_country;
2072 + $s_state = method_exists($order, 'get_shipping_state') ? $order->get_shipping_state() : $order->shipping_state;
2073 + $s_city = method_exists($order, 'get_shipping_city') ? $order->get_shipping_city() : $order->shipping_city;
2074 + $s_address_1 = method_exists($order, 'get_shipping_address_1') ? $order->get_shipping_address_1() : $order->shipping_address_1;
2075 + $s_address_2 = method_exists($order, 'get_shipping_address_2') ? $order->get_shipping_address_2() : $order->shipping_address_2;
1182 2076 $sr_order['destination'] = array(
1183 - 'postalCode' => $order->shipping_postcode,
1184 - 'country' => $order->shipping_country,
1185 - 'county' => $order->shipping_state,
1186 - 'city' => $order->shipping_city,
1187 - 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2
2077 + 'postalCode' => $s_postcode,
2078 + 'country' => $s_country,
2079 + 'county' => $s_state,
2080 + 'city' => $s_city,
2081 + 'street' => $s_address_1 . ' ' . $s_address_2
1188 2082 );
2083 +
2084 + if ($shipping_class == "courier_smartpost") {
2085 + $delivery_time = get_post_meta($post_id, '_delivery_time', true);
2086 + if ($delivery_time) {
2087 + $sr_order['destination']['timeWindow'] = $delivery_time;
2088 + }
2089 + }
1189 2090 }
1190 2091
1191 2092 if ($m_service_type) {
1192 2093 $sr_order['services'] = array('serviceType' => $m_service_type);
@@ -1194,22 +2095,26 @@
1194 2095
1195 2096 $shipping_request['orders'][] = $sr_order;
1196 2097 }
1197 2098 }
2099 +
1198 2100 $shipping_request['credentials'] = array_values($shipping_request['credentials']);
1199 2101 if (empty($shipping_request['orders'])) {
1200 2102 return;
1201 2103 }
2104 +
1202 2105 $MK = mk_get_api();
1203 2106 if (!$MK) {
1204 2107 return;
1205 2108 }
2109 +
1206 2110 try {
1207 2111 $response = $MK->createShipments($shipping_request);
1208 2112 } catch (Exception $e) {
1209 - echo $e->getMessage();
1210 - exit();
2113 + error_log('Error while creating shipment ['.$e->getMessage().']');
2114 + return;
1211 2115 }
2116 +
1212 2117 $shipments = !empty($response->shipments) ? $response->shipments : $response;
1213 2118 $manifest = !empty($response->manifests) ? $response->manifests[0] : false;
1214 2119
1215 2120 foreach ($shipments as $order) {
@@ -1217,11 +2122,13 @@
1217 2122 update_post_meta((int)$order->orderId, '_parcel_machine_manifest', sanitize_text_field($manifest));
1218 2123 }
1219 2124 if (!empty($order->orderId) && !empty($order->shipmentId)) {
1220 2125 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->shipmentId));
2126 + update_post_meta((int)$order->orderId, '_tracking_number', sanitize_text_field($order->shipmentId)); //default value used by other plugins, such as WooCommerce PDF Invoices and so on
1221 2127 delete_post_meta((int)$order->orderId, '_parcel_machine_error');
1222 2128 } else if (!empty($order->orderId) && !empty($order->barCode)) {
1223 2129 update_post_meta((int)$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field($order->barCode));
2130 + update_post_meta((int)$order->orderId, '_tracking_number', sanitize_text_field($order->barCode)); //default value used by other plugins, such as WooCommerce PDF Invoices and so on
1224 2131 delete_post_meta((int)$order->orderId, '_parcel_machine_error');
1225 2132 } else if (!empty($order->orderId) && !empty($order->errorMessage)) {
1226 2133 update_post_meta((int)$order->orderId, '_parcel_machine_error', sanitize_text_field($order->errorMessage));
1227 2134 }
@@ -1227,9 +2134,10 @@
1227 2134 }
1228 2135 }
1229 2136 }
1230 2137
1231 - function mk_get_labels($post_ids, $ajax = false) {
2138 + function mk_get_labels($post_ids, $ajax = false)
2139 + {
1232 2140 if (!is_array($post_ids)) {
1233 2141 $post_ids = array($post_ids);
1234 2142 }
1235 2143 transport_add_method();
@@ -1236,13 +2144,19 @@
1236 2144 $shipping_request = array('credentials' => array(), 'orders' => array(), 'printFormat' => 'A4');
1237 2145 $shipping_classes_map = array(
1238 2146 'parcelmachine_omniva' => 'WC_ParcelMachine_Shipping_Method_Omniva',
1239 2147 'parcelmachine_smartpost' => 'WC_ParcelMachine_Shipping_Method_Smartpost',
1240 - 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva'
2148 + 'parcelmachine_dpd' => 'WC_ParcelMachine_Shipping_Method_DPD',
2149 + 'courier_omniva' => 'WC_Courier_Shipping_Method_Omniva',
2150 + 'courier_smartpost' => 'WC_Courier_Shipping_Method_Smartpost'
1241 2151 );
2152 + $missing_ids = array();
1242 2153 foreach ($post_ids as $post_id) {
1243 2154 $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
1244 - if (!$shipment_id) { continue; }
2155 + if (!$shipment_id) {
2156 + $missing_ids[] = $post_id;
2157 + continue;
2158 + }
1245 2159 $order = new WC_Order($post_id);
1246 2160 $shipping_methods = $order->get_shipping_methods();
1247 2161 if (empty($shipping_methods)) { continue; }
1248 2162 foreach ($shipping_methods as $shipping_method) {
@@ -1264,27 +2178,50 @@
1264 2178 }
1265 2179 if (empty($shipping_request['credentials'][$carrier_uc])) {
1266 2180 $api_user = $transport_class->settings['service_user'];
1267 2181 $api_password = $transport_class->settings['service_password'];
1268 - if (!$api_user || !$api_password) {
2182 +
2183 + $use_mk_contract = false;
2184 + if ( isset($transport_class->settings['use_mk_contract']) ) {
2185 + $use_mk_contract = $transport_class->settings['use_mk_contract'];
2186 + }
2187 +
2188 + //change carrier_uc if it has been set in the interface.
2189 + if (isset($transport_class->settings['service_carrier'])) {
2190 + $carrier_uc = $transport_class->settings['service_carrier'];
2191 + }
2192 +
2193 + if ( (!$api_user || !$api_password) and (!$use_mk_contract) ) {
1269 2194 add_action('admin_notices', 'mk_admin_error');
1270 2195 continue;
1271 2196 }
1272 - $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password);
2197 + if (!$use_mk_contract) {
2198 + $shipping_request['credentials'][$carrier_uc] = array('carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password);
2199 + }
1273 2200 }
1274 2201 $sender = array(
1275 2202 'name' => $transport_class->settings['shop_name'],
1276 2203 'phone' => $transport_class->settings['shop_phone'],
1277 2204 'email' => $transport_class->settings['shop_email'],
1278 - 'country' => $transport_class->settings['shop_address_country'],
1279 - 'city' => $transport_class->settings['shop_address_city'],
1280 - 'street' => $transport_class->settings['shop_address_street'],
1281 - 'postalCode' => $transport_class->settings['shop_postal_code']
2205 + 'postalCode' => !empty($transport_class->settings['shop_postal_code']) ? $transport_class->settings['shop_postal_code'] : '',
2206 + 'country' => !empty($transport_class->settings['shop_address_country']) ? $transport_class->settings['shop_address_country'] : '',
2207 + 'city' => !empty($transport_class->settings['shop_address_city']) ? $transport_class->settings['shop_address_city'] : '',
2208 + 'street' => !empty($transport_class->settings['shop_address_street']) ? $transport_class->settings['shop_address_street'] : ''
1282 2209 );
2210 + $s_first_name = method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : $order->shipping_first_name;
2211 + $s_last_name = method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : $order->shipping_last_name;
2212 + $recipient = trim($s_first_name . ' ' . $s_last_name);
2213 + if (empty($recipient)) {
2214 + $b_first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name;
2215 + $b_last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name;
2216 + $recipient = trim($b_first_name . ' ' . $b_last_name);
2217 + }
2218 + $b_phone = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone;
2219 + $b_email = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email;
1283 2220 $sr_order = array(
1284 2221 'carrier' => $carrier_uc,
1285 - 'orderId' => $order->id,
1286 - 'recipient' => array('name' => $order->shipping_first_name . ' ' . $order->shipping_last_name, 'phone' => $order->billing_phone, 'email' => $order->billing_email),
2222 + 'orderId' => $order->get_id(),
2223 + 'recipient' => array('name' => $recipient, 'phone' => $b_phone, 'email' => $b_email),
1287 2224 'sender' => $sender,
1288 2225 'shipmentId' => $shipment_id
1289 2226 );
1290 2227 $m_service_type = null;
@@ -1296,9 +2233,9 @@
1296 2233
1297 2234 if (!empty($machine_id)) {
1298 2235 $sr_order['destination']['destinationId'] = $machine_id;
1299 2236 }
1300 - if ($transport_class->carrier === 'omniva' && $transport_class->type === 'cou') {
2237 + if ($transport_class->type === 'cou') {
1301 2238 $sr_order['destination'] = array(
1302 2239 'postalCode' => $order->shipping_postcode,
1303 2240 'country' => $order->shipping_country,
1304 2241 'county' => $order->shipping_state,
@@ -1304,8 +2241,15 @@
1304 2241 'county' => $order->shipping_state,
1305 2242 'city' => $order->shipping_city,
1306 2243 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2
1307 2244 );
2245 +
2246 + if ($shipping_class == "courier_smartpost") {
2247 + $delivery_time = get_post_meta($post_id, '_delivery_time', true);
2248 + if ($delivery_time) {
2249 + $sr_order['destination']['timeWindow'] = $delivery_time;
2250 + }
2251 + }
1308 2252 }
1309 2253
1310 2254 if ($m_service_type) {
1311 2255 $sr_order['services'] = array('serviceType' => $m_service_type);
@@ -1333,9 +2277,11 @@
1333 2277 }
1334 2278 if ($ajax) {
1335 2279 return $response->labelUrl;
1336 2280 }
1337 - $sendback = add_query_arg(array('post_type' => 'shop_order', 'mk_pdf' => urlencode($response->labelUrl)), '');
2281 + $mk_err = false;
2282 + 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)); }
2283 + $sendback = add_query_arg(array('post_type' => 'shop_order', 'mk_pdf' => urlencode($response->labelUrl), 'mk_err' => urlencode($mk_err)), '');
1338 2284 wp_redirect(esc_url_raw($sendback));
1339 2285 exit();
1340 2286 }
1341 2287
@@ -1346,11 +2292,11 @@
1346 2292 $MK = mk_get_api();
1347 2293 if(!$MK) {
1348 2294 return array();
1349 2295 }
1350 - $data = $MK->getDestinations(array('type' => 'APT'));
1351 - update_option('mk_machines_cache', $data);
1352 - update_option('mk_machines_expires', time()+3*60*60);
2296 + $data = $MK->getDestinations(array('type' => 'APT,PUP'));
2297 + update_option('mk_machines_cache', $data, 'no');
2298 + update_option('mk_machines_expires', time()+3*60*60, 'no');
1353 2299 }
1354 2300
1355 2301 $machines = array();
1356 2302 if (!$data || empty($data)) {
@@ -1356,9 +2302,9 @@
1356 2302 if (!$data || empty($data)) {
1357 2303 return array();
1358 2304 }
1359 2305 foreach ($data as $machine) {
1360 - if ((!$country || $machine->country === $country) && $machine->type === 'APT' && ($carrier === '*' || strtolower($carrier) === strtolower($machine->carrier))) {
2306 + if ((!$country || $machine->country === $country) && ($machine->type === 'APT' || $machine->type === 'PUP') && ($carrier === '*' || strtolower($carrier) === strtolower($machine->carrier))) {
1361 2307 $machines[] = array(
1362 2308 'carrier' => strtolower($machine->carrier),
1363 2309 'id' => $machine->id,
1364 2310 'name' => $machine->name,
@@ -1386,8 +2332,10 @@
1386 2332 return false;
1387 2333 }
1388 2334
1389 2335 function print_parcel_machine_label_action( $post_id ) {
2336 + $shipment_id = get_post_meta($post_id, '_parcel_machine_shipment_id', true);
2337 + if (!$shipment_id) { return; }
1390 2338 ?>
1391 2339 <li class="wide">
1392 2340 <a id="print_parcel_machine_label" href="#" class="button"><?php _e('Print parcel label', 'wc_makecommerce_domain');?></a>
1393 2341 <img class="mc_loading" src="<?php echo site_url('/wp-admin/images/loading.gif');?>">
@@ -1419,14 +2367,14 @@
1419 2367 function ajax_print_pml() {
1420 2368 if(!current_user_can('manage_woocommerce') ) {
1421 2369 die();
1422 2370 }
1423 - die(mk_get_labels(intval($_POST['id']), true));
2371 + echo mk_get_labels(array(intval($_POST['id'])), true);
2372 + die();
1424 2373 }
1425 2374 add_action( 'wp_ajax_print_pml', 'ajax_print_pml' );
1426 2375
1427 2376
1428 - // PP: kus sellel õige koht oleks?
1429 2377 function clear_wc_shipping_rates_cache(){
1430 2378 $packages = WC()->cart->get_shipping_packages();
1431 2379 foreach ($packages as $key => $value) {
1432 2380 $shipping_session = "shipping_for_package_$key";
@@ -1436,9 +2384,8 @@
1436 2384 add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');
1437 2385
1438 2386
1439 2387
1440 - // PP: mis see on? kus ta failis olla võiks?
1441 2388 function api_javascript_ui( $data ) {
1442 2389 ?>
1443 2390 <script type="text/javascript">
1444 2391 jQuery(document).ready(function($) {
@@ -1463,5 +2410,8 @@
1463 2410 });
1464 2411 </script>
1465 2412 <?php
1466 2413 }
2414 +
2415 +
2416 +
1467 2417