PluginProbe
PostNL for WooCommerce / 3.1.4
PostNL for WooCommerce v3.1.4
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / class-wcmp-admin.php

class-wcmp-admin.php in PostNL for WooCommerce 3.1.4, at includes/class-wcmp-admin.php

603 lines 24.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use WPO\WC\PostNL\Compatibility\WC_Core as WCX;
4 use WPO\WC\PostNL\Compatibility\Order as WCX_Order;
5 use WPO\WC\PostNL\Compatibility\Product as WCX_Product;
6
7 if ( ! defined('ABSPATH')) exit; // Exit if accessed directly
8
9 if ( ! class_exists('WooCommerce_PostNL_Admin')) :
10
11 /**
12 * Admin options, buttons & data
13 */
14 class WooCommerce_PostNL_Admin {
15
16 function __construct() {
17 add_action('woocommerce_admin_order_actions_end', array($this, 'order_list_shipment_options'), 9999);
18 add_action('admin_footer', array($this, 'bulk_actions'));
19
20 add_action('admin_footer', array($this, 'offset_dialog'));
21 add_action('woocommerce_admin_order_actions_end', array($this, 'admin_wc_actions'), 20);
22 add_action('add_meta_boxes_shop_order', array($this, 'shop_order_metabox'));
23 add_action('woocommerce_admin_order_data_after_shipping_address', array($this, 'single_order_shipment_options'));
24
25 add_action('wp_ajax_wcmp_save_shipment_options', array($this, 'save_shipment_options_ajax'));
26 add_action('wp_ajax_wcmp_get_shipment_summary_status', array($this, 'order_list_ajax_get_shipment_summary'));
27
28 // HS code in product shipping options tab
29 add_action('woocommerce_product_options_shipping', array($this, 'product_hs_code_field'));
30 add_action('woocommerce_process_product_meta', array($this, 'product_hs_code_field_save'));
31 }
32
33 public function order_list_shipment_options($order, $hide = true) {
34 $shipping_country = WCX_Order::get_prop($order, 'shipping_country');
35 if ( ! WooCommerce_PostNL()->export->is_postnl_destination($shipping_country)) {
36 return;
37 }
38 $order_id = WCX_Order::get_id($order);
39 $shipment_options = WooCommerce_PostNL()->export->get_options($order);
40 $postnl_options_extra = WCX_Order::get_meta($order, '_postnl_shipment_options_extra');
41 $package_types = WooCommerce_PostNL()->export->get_package_types();
42 $recipient = WooCommerce_PostNL()->export->get_recipient($order);
43
44 // get shipment data (exclude concepts = true)
45 $consignments = $this->get_order_shipments($order, true);
46
47 $style = $hide ? 'style="display:none"' : '';
48 // if we have shipments, then we show status & link to Track & Trace, settings under i
49 if ( ! empty($consignments)) {
50 // only use last shipment
51 $last_shipment = array_pop($consignments);
52 $last_shipment_id = $last_shipment['shipment_id'];
53 ?>
54 <div class="wcmp_shipment_summary" <?php echo $style; ?>>
55 <?php $this->show_order_delivery_options($order); ?>
56 <a class="wcmp_show_shipment_summary"><span class="encircle wcmp_show_shipment_summary">i</span></a>
57 <div class="wcmp_shipment_summary_list" data-loaded="" data-shipment_id="<?php echo $last_shipment_id; ?>" data-order_id="<?php echo $order_id; ?>" style="display: none;">
58 <img src="<?php echo WooCommerce_PostNL()->plugin_url() . '/assets/img/wpspin_light.gif'; ?>" class="wcmp_spinner" />
59 </div>
60 </div>
61 <?php
62 } else {
63 ?>
64 <div class="wcmp_shipment_options" <?php echo $style; ?>>
65 <?php $this->show_order_delivery_options($order); ?>
66 </div>
67 <?php
68 }
69
70 ?>
71 <div class="wcmp_shipment_options" <?php echo $style; ?>>
72 <?php printf('<a href="#" class="wcmp_show_shipment_options"><span class="wcpm_package_type">%s</span> &#x25BE;</a>', $package_types[$shipment_options['package_type']]); ?>
73 <div class="wcmp_shipment_options_form" style="display: none;">
74 <?php include('views/wcmp-order-shipment-options.php'); ?>
75 </div>
76 </div>
77 <?php
78 }
79
80 /**
81 * Get shipment status + Track & Trace link via AJAX
82 */
83 public function order_list_ajax_get_shipment_summary() {
84 check_ajax_referer('wc_postnl', 'security');
85 extract($_POST); // order_id, shipment_id
86 $order = wc_get_order($order_id);
87 $shipment = WooCommerce_PostNL()->export->get_shipment_data($shipment_id, $order);
88 if ( ! empty($shipment['tracktrace'])) {
89 $order_has_shipment = true;
90 $tracktrace_url = $this->get_tracktrace_url($order_id, $shipment['tracktrace']);
91 }
92 $package_types = WooCommerce_PostNL()->export->get_package_types();
93
94 include('views/wcmp-order-shipment-summary.php');
95 die();
96 }
97
98 public function order_list_return_shipment_options($order, $hide = true) {
99 $shipping_country = WCX_Order::get_prop($order, 'shipping_country');
100 if ($shipping_country != 'NL' && ! WooCommerce_PostNL()->export->is_eu_country($shipping_country)) {
101 return;
102 }
103
104 $style = $hide ? 'style="display:none"' : '';
105 ?>
106 <div class="wcmp_shipment_options_form return_shipment" <?php echo $style; ?>>
107 <?php include('views/wcmp-order-return-shipment-options.php'); ?>
108 </div>
109 <?php
110 }
111
112 /**
113 * Add export option to bulk action drop down menu
114 * Using Javascript until WordPress core fixes: http://core.trac.wordpress.org/ticket/16031
115 * @access public
116 * @return void
117 */
118 public function bulk_actions() {
119 global $post_type;
120 $bulk_actions = array(
121 'wcmp_export' => __('PostNL: Prepare shipment', 'woocommerce-postnl'),
122 'wcmp_print' => __('PostNL: Print', 'woocommerce-postnl'),
123 'wcmp_export_print' => __('PostNL: Prepare & print shipment', 'woocommerce-postnl'),
124 );
125
126 if ('shop_order' == $post_type) {
127 ?>
128 <script type="text/javascript">
129 jQuery(document).ready(function() {
130 <?php foreach ($bulk_actions as $action => $title) { ?>
131 jQuery('<option>').val('<?php echo $action; ?>').html('<?php echo esc_attr($title); ?>').appendTo("select[name='action'], select[name='action2']");
132 <?php } ?>
133 });
134 </script>
135 <img src="<?php echo WooCommerce_PostNL()->plugin_url() . '/assets/img/wpspin_light.gif'; ?>" class="wcmp_bulk_spinner waiting" style="display:none;" />
136 <?php
137 }
138 }
139
140 /**
141 * Show dialog to choose print position (offset)
142 * @access public
143 * @return void
144 */
145 public function offset_dialog() {
146 global $post_type;
147 if ('shop_order' == $post_type) {
148 ?>
149 <div id="wcpostnl_offset_dialog" style="display:none;">
150 <?php _e('Labels to skip', 'woocommerce-postnl'); ?>:
151 <input type="text" size="2" class="wc_postnl_offset">
152 <img src="<?php echo WooCommerce_PostNL()->plugin_url() . '/assets/img/print-offset-icon.png'; ?>" id="wcpostnl-offset-icon" style="vertical-align: middle;">
153 <button class="button" style="display:none; margin-top: 4px"><?php _e('Print', 'woocommerce-postnl'); ?></button>
154 </div>
155 <?php
156 }
157 }
158
159 /**
160 * Add print actions to the orders listing
161 * Support wc > 3.3.0
162 * Call the function admin_order_actions for the same settings
163 *
164 * @param $order
165 */
166 public function admin_wc_actions($order) {
167 return $this->admin_order_actions($order);
168 }
169
170 /**
171 * Add print actions to the orders listing
172 */
173 public function admin_order_actions($order) {
174 if (empty($order)) {
175 return;
176 }
177
178 $shipping_country = WCX_Order::get_prop($order, 'shipping_country');
179 if ( ! WooCommerce_PostNL()->export->is_postnl_destination($shipping_country)) {
180 return;
181 }
182
183 $order_id = WCX_Order::get_id($order);
184
185 $listing_actions = array(
186 'add_shipment' => array(
187 'url' => wp_nonce_url(admin_url('admin-ajax.php?action=wc_postnl&request=add_shipment&order_ids=' . $order_id), 'wc_postnl'),
188 'img' => WooCommerce_PostNL()->plugin_url() . '/assets/img/postnl-up.png',
189 'alt' => esc_attr__('Export to PostNL', 'woocommerce-postnl'),
190 ),
191 'get_labels' => array(
192 'url' => wp_nonce_url(admin_url('admin-ajax.php?action=wc_postnl&request=get_labels&order_ids=' . $order_id), 'wc_postnl'),
193 'img' => WooCommerce_PostNL()->plugin_url() . '/assets/img/postnl-pdf.png',
194 'alt' => esc_attr__('Print PostNL label', 'woocommerce-postnl'),
195 ),
196 );
197
198 $consignments = $this->get_order_shipments($order);
199
200 if (empty($consignments)) {
201 unset($listing_actions['get_labels']);
202 }
203
204 $processed_shipments = $this->get_order_shipments($order, true);
205 if (empty($processed_shipments) || $shipping_country != 'NL') {
206 unset($listing_actions['add_return']);
207 }
208
209 $target = (isset(WooCommerce_PostNL()->general_settings['download_display'])
210 && WooCommerce_PostNL()->general_settings['download_display'] == 'display') ? 'target="_blank"'
211 : '';
212 $nonce = wp_create_nonce('wc_postnl');
213 foreach ($listing_actions as $action => $data) {
214 printf(
215 '<a href="%1$s" class="button tips postnl %2$s" alt="%3$s" data-tip="%3$s" data-order-id="%4$s" data-request="%2$s" data-nonce="%5$s" %6$s>',
216 $data['url'],
217 $action,
218 $data['alt'],
219 $order_id,
220 $nonce,
221 $target
222 );
223 ?>
224 <img src="<?php echo $data['img']; ?>" alt="<?php echo $data['alt']; ?>" style="width:17px; margin: 5px 3px; pointer-events: none;" class="wcmp_button_img"> </a>
225 <?php
226 }
227 ?>
228 <img src="<?php echo WooCommerce_PostNL()->plugin_url(
229 ) . '/assets/img/wpspin_light.gif'; ?>" style="width: 17px; margin: 5px 3px;" class="wcmp_spinner waiting" />
230 <?php
231 }
232
233 public function get_order_shipments($order, $exclude_concepts = false) {
234 if (empty($order)) {
235 return;
236 }
237
238 $consignments = WCX_Order::get_meta($order, '_postnl_shipments');
239 // fallback to legacy consignment data (v1.X)
240 if (empty($consignments)) {
241 if ($consignment_id = WCX_Order::get_meta($order, '_postnl_consignment_id')) {
242 $consignments = array(
243 array(
244 'shipment_id' => $consignment_id,
245 'tracktrace' => WCX_Order::get_meta($order, '_postnl_tracktrace'),
246 ),
247 );
248 } else {
249 if ($legacy_consignments = WCX_Order::get_meta($order, '_postnl_consignments')) {
250 $consignments = array();
251 foreach ($legacy_consignments as $consignment) {
252 if (isset($consignment['consignment_id'])) {
253 $consignments[] = array(
254 'shipment_id' => $consignment['consignment_id'],
255 'tracktrace' => $consignment['tracktrace'],
256 );
257 }
258 }
259 }
260 }
261 }
262
263 if (empty($consignments) || ! is_array($consignments)) {
264 return false;
265 }
266
267 if ( ! empty($consignments) && $exclude_concepts) {
268 foreach ($consignments as $key => $consignment) {
269 if (empty($consignment['tracktrace'])) {
270 unset($consignments[$key]);
271 }
272 }
273 }
274
275 return $consignments;
276 }
277
278 public function save_shipment_options_ajax() {
279 check_ajax_referer('wc_postnl', 'security');
280 extract($_POST);
281 parse_str($form_data, $form_data);
282 $order = WCX::get_order($order_id);
283
284 if (isset($form_data['postnl_options'][$order_id])) {
285 $shipment_options = $form_data['postnl_options'][$order_id];
286
287 // convert insurance option
288 if (isset($shipment_options['insured'])) {
289 unset($shipment_options['insured']);
290 $shipment_options['insurance'] = array(
291 'amount' => (int) $shipment_options['insured_amount'] * 100,
292 'currency' => 'EUR',
293 );
294 unset($shipment_options['insured_amount']);
295 }
296
297 // separate extra options
298 if (isset($shipment_options['extra_options'])) {
299 WCX_Order::update_meta_data(
300 $order,
301 '_postnl_shipment_options_extra',
302 $shipment_options['extra_options']
303 );
304 unset($shipment_options['extra_options']);
305 }
306
307 WCX_Order::update_meta_data($order, '_postnl_shipment_options', $shipment_options);
308 }
309
310 // Quit out
311 die();
312 }
313
314 /**
315 * Add the meta box on the single order page
316 */
317 public function shop_order_metabox() {
318 add_meta_box(
319 'postnl', //$id
320 __('PostNL', 'woocommerce-postnl'), //$title
321 array($this, 'create_box_content'), //$callback
322 'shop_order', //$post_type
323 'side', //$context
324 'default' //$priority
325 );
326 }
327
328 /**
329 * Callback: Create the meta box content on the single order page
330 */
331 public function create_box_content() {
332 global $post_id;
333 // get order
334 $order = WCX::get_order($post_id);
335 if ( ! $order) {
336 return;
337 }
338 $order_id = WCX_Order::get_id($order);
339
340 $shipping_country = WCX_Order::get_prop($order, 'shipping_country');
341 if ( ! WooCommerce_PostNL()->export->is_postnl_destination($shipping_country)) {
342 return;
343 }
344
345 // show buttons and check if WooCommerce > 3.3.0 is used and select the correct function and class
346 if (version_compare(WOOCOMMERCE_VERSION, '3.3.0', '>=')) {
347 echo '<div class="single_wc_actions">';
348 $this->admin_wc_actions($order, false);
349 } else {
350 echo '<div class="single_order_actions">';
351 $this->admin_order_actions($order, false);
352 }
353 echo '</div>';
354
355 $consignments = $this->get_order_shipments($order);
356 // show shipments if available
357 if ( ! empty($consignments)) {
358 ?>
359 <table class="tracktrace_status">
360 <thead>
361 <tr>
362 <th>&nbsp;</th>
363 <th><?php _e('Track & Trace', 'woocommerce-postnl'); ?></th>
364 <th><?php _e('Status', 'woocommerce-postnl'); ?></th>
365 </tr>
366 </thead>
367 <tbody>
368 <?php
369 $action = 'get_labels';
370 $target = (isset(WooCommerce_PostNL()->general_settings['download_display'])
371 && WooCommerce_PostNL()->general_settings['download_display'] == 'display')
372 ? 'target="_blank"'
373 : '';
374 $nonce = wp_create_nonce('wc_postnl');
375 $label_button_text = esc_attr__('Print PostNL label', 'woocommerce-postnl');
376 foreach ($consignments as $shipment_id => $shipment):
377 $shipment = WooCommerce_PostNL()->export->get_shipment_data($shipment_id, $order);
378 $label_url = wp_nonce_url(admin_url('admin-ajax.php?action=wc_postnl&request=get_labels&shipment_ids=' . $shipment_id),'wc_postnl');
379 if (isset($shipment['tracktrace'])) {
380 $tracktrace_url = $this->get_tracktrace_url($order_id, $shipment['tracktrace']);
381 $tracktrace_link = sprintf('<a href="%s">%s</a>', $tracktrace_url, $shipment['tracktrace']);
382 } else {
383 if (isset($shipment['shipment']) && isset($shipment['shipment']['options'])) {
384 $tracktrace_link = '(' . WooCommerce_PostNL()->export->get_package_name( $shipment['shipment']['options']['package_type']) . ')';
385 } else {
386 $tracktrace_link = '(Unknown)';
387 }
388 }
389 $status = isset($shipment['status']) ? $shipment['status'] : '-';
390 ?>
391 <tr>
392 <td class="wcmp-create-label">
393 <?php
394 printf(
395 '<a href="%1$s" class="button tips postnl %2$s" alt="%3$s" data-tip="%3$s" data-order-id="%4$s" data-request="%2$s" data-nonce="%5$s" %6$s>',
396 $label_url,
397 $action,
398 $label_button_text,
399 $order_id,
400 $nonce,
401 $target
402 );
403 printf(
404 '<img class="wcmp_button_img" src="%1$s" alt="%2$s" width="16" />',
405 WooCommerce_PostNL()->plugin_url() . "/assets/img/postnl-pdf.png",
406 $label_button_text
407 );
408 printf("</a>");
409 ?>
410 </td>
411 <td class="wcmp-tracktrace"><?php echo $tracktrace_link; ?></td>
412 <td class="wcmp-status"><?php echo $status; ?></td>
413 </tr>
414 <?php endforeach ?>
415 </tbody>
416 </table>
417 <?php
418 }
419 }
420
421 public function single_order_shipment_options($order) {
422 $shipping_country = WCX_Order::get_prop($order, 'shipping_country');
423 if ( ! WooCommerce_PostNL()->export->is_postnl_destination($shipping_country)) {
424 return;
425 }
426
427 echo '<div style="clear:both;"><strong>' . __('PostNL shipment:', 'woocommerce-postnl') . '</strong><br/>';
428 $this->order_list_shipment_options($order, false);
429 echo '</div>';
430 }
431
432 public function show_order_delivery_options($order) {
433 $delivery_options = WCX_Order::get_meta($order, '_postnl_delivery_options');
434
435 if ( ! empty($delivery_options) && is_array($delivery_options)) {
436 extract($delivery_options);
437 }
438
439 echo '<div class="delivery-options">';
440 if ( ! empty($date)
441 && ! (isset(WooCommerce_PostNL()->checkout_settings['deliverydays_window'])
442 && WooCommerce_PostNL()->checkout_settings['deliverydays_window'] == 0)) {
443 $formatted_date = date_i18n(
444 apply_filters('wcpostnl_delivery_date_format', wc_date_format()),
445 strtotime($date)
446 );
447 if ( ! empty($time)) {
448 $time = array_shift($time); // take first element in time array
449 if (isset($time['price_comment'])) {
450 switch($time['price_comment']) {
451 case 'morning':
452 $time_title = __('Morning delivery', 'woocommerce-postnl');
453 break;
454 case 'standard':
455 // $time_title = __( 'Standard delivery', 'woocommerce-postnl' );
456 break;
457 case 'evening':
458 case 'avond':
459 $time_title = __('Evening delivery', 'woocommerce-postnl');
460 break;
461 }
462 }
463 $time_title = ! empty($time_title) ? "({$time_title})" : '';
464 }
465
466 printf(
467 '<div class="delivery-date"><strong>%s: </strong>%s %s</div>',
468 __('Delivery date', 'woocommerce-postnl'),
469 $formatted_date,
470 $time_title
471 );
472 }
473
474 if ($pickup = WooCommerce_PostNL()->export->is_pickup($order, $delivery_options)) {
475 switch($pickup['price_comment']) {
476 case 'retail':
477 $title = __('PostNL Pickup', 'woocommerce-postnl');
478 break;
479 case 'retailexpress':
480 $title = __('PostNL Pickup Express', 'woocommerce-postnl');
481 break;
482 }
483
484 echo "<div class='pickup-location'><strong>{$title}: </strong>{$pickup['location']}, {$pickup['street']} {$pickup['number']}, {$pickup['postal_code']} {$pickup['city']}</div>";
485 }
486 echo '</div>';
487 }
488
489 public function get_tracktrace_url($order_id, $tracktrace) {
490 if (empty($order_id)) {
491 return;
492 }
493
494 $order = WCX::get_order($order_id);
495 $country = WCX_Order::get_prop($order, 'shipping_country');
496 $postcode = preg_replace('/\s+/', '', WCX_Order::get_prop($order, 'shipping_postcode'));
497
498 // set url for NL or foreign orders
499 if ($country == 'NL') {
500 // use billing postcode for pickup/pakjegemak
501 if (WooCommerce_PostNL()->export->is_pickup($order)) {
502 $postcode = preg_replace('/\s+/', '', WCX_Order::get_prop($order, 'billing_postcode'));
503 }
504
505 // $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Inbox/Search?lang=nl&B=%s&P=%s', $tracktrace, $postcode);
506 $tracktrace_url = sprintf(
507 'https://mijnpakket.postnl.nl/Claim?Barcode=%s&Postalcode=%s',
508 $tracktrace,
509 $postcode
510 );
511 } else {
512 $tracktrace_url = sprintf(
513 'https://www.internationalparceltracking.com/Main.aspx#/track/%s/%s/%s',
514 $tracktrace,
515 $country,
516 $postcode
517 );
518 }
519
520 return $tracktrace_url;
521 }
522
523 public function get_tracktrace_links($order_id) {
524 if ($consignments = $this->get_tracktrace_shipments($order_id)) {
525 foreach ($consignments as $key => $consignment) {
526 $tracktrace_links[] = $consignment['tracktrace_link'];
527 }
528
529 return $tracktrace_links;
530 } else {
531 return false;
532 }
533 }
534
535 public function get_tracktrace_shipments($order_id) {
536 $order = WCX::get_order($order_id);
537 $shipments = $this->get_order_shipments($order, true);
538
539 if (empty($shipments)) {
540 return false;
541 }
542
543 foreach ($shipments as $shipment_id => $shipment) {
544 // skip concepts, letters & mailbox packages
545 if (empty($shipment['tracktrace'])) {
546 unset($shipments[$shipment_id]);
547 continue;
548 }
549 // add links & urls
550 $shipments[$shipment_id]['tracktrace_url'] = $tracktrace_url = $this->get_tracktrace_url(
551 $order_id,
552 $shipment['tracktrace']
553 );
554 $shipments[$shipment_id]['tracktrace_link'] = sprintf(
555 '<a href="%s">%s</a>',
556 $tracktrace_url,
557 $shipment['tracktrace']
558 );
559 }
560
561 if (empty($shipments)) {
562 return false;
563 }
564
565 return $shipments;
566 }
567
568 public function product_hs_code_field() {
569 echo '<div class="options_group">';
570 woocommerce_wp_text_input(
571 array(
572 'id' => '_postnl_hs_code',
573 'label' => __('HS Code', 'woocommerce-postnl'),
574 'description' => sprintf(
575 __('HS Codes are used for PostNL world shipments, you can find the appropriate code on the %ssite of the Dutch Customs%s.', 'woocommerce-postnl'),
576 '<a href="http://tarief.douane.nl/arctictariff-public-web/#!/home" target="_blank">',
577 '</a>'
578 )
579 )
580 );
581 echo '</div>';
582 }
583
584 public function product_hs_code_field_save($post_id) {
585 // check if hs code is passed and not an array (=variation hs code)
586 if (isset($_POST['_postnl_hs_code']) && ! is_array($_POST['_postnl_hs_code'])) {
587 $product = wc_get_product($post_id);
588 $hs_code = $_POST['_postnl_hs_code'];
589 if ( ! empty($hs_code)) {
590 WCX_Product::update_meta_data($product, '_postnl_hs_code', esc_attr($hs_code));
591 } else {
592 if (isset($_POST['_postnl_hs_code']) && empty($hs_code)) {
593 WCX_Product::delete_meta_data($product, '_postnl_hs_code');
594 }
595 }
596 }
597 }
598 }
599
600 endif; // class_exists
601
602 return new WooCommerce_PostNL_Admin();
603