PluginProbe
MakeCommerce for WooCommerce / 4.0.1
MakeCommerce for WooCommerce v4.0.1
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / shipping / order.php

order.php in MakeCommerce for WooCommerce 4.0.1, at shipping/order.php

498 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce\Shipping;
4
5 /**
6 * All functionality that has to do with shipping and orders
7 *
8 * @since 3.0.0
9 */
10
11 class Order extends \MakeCommerce\Shipping {
12
13 private $loader;
14
15 /**
16 * Constructs Order class, defines hooks
17 *
18 * @since 3.0.0
19 */
20 public function __construct( \MakeCommerce\Loader $loader ) {
21
22 $this->loader = $loader;
23
24 $this->define_hooks();
25 }
26
27 /**
28 * Define all wordpress hooks
29 *
30 * @since 3.0.0
31 */
32 public function define_hooks() {
33 $this->loader->add_filter( 'woocommerce_order_details_after_customer_details', $this, 'parcel_machine_details' );
34 $this->loader->add_filter( 'woocommerce_admin_order_data_after_shipping_address', $this, 'parcel_machine_changing', 10, 1 );
35 $this->loader->add_filter( 'manage_shop_order_posts_custom_column', $this, 'shipping_method_order_view', 3 );
36 $this->loader->add_filter( 'woocommerce_email_customer_details_fields', $this, 'shipping_email_details', 10, 3 );
37 $this->loader->add_filter( 'restrict_manage_posts', $this, 'filter_orders' );
38
39 // HPOS Add shipping method filtering selectbox to Woo orders page as well
40 $this->loader->add_filter( 'woocommerce_order_list_table_restrict_manage_orders', $this, 'hpos_filter_orders' );
41 // HPOS Add shipping method filtering functionality
42 $this->loader->add_filter( 'woocommerce_order_query_args', $this, 'order_filter_by_shipping_method' );
43 // HPOS Add bulk actions
44 $this->loader->add_filter( 'bulk_actions-woocommerce_page_wc-orders', $this, 'mc_bulk_actions' );
45 }
46
47 /**
48 * Add parcel machine information to order view (Customer)
49 *
50 * @since 3.0.0
51 */
52 public function parcel_machine_details( $order ) {
53
54 $machine_id = $order->get_meta( '_parcel_machine', true );
55
56 if ( empty( $machine_id ) ) {
57 return;
58 }
59
60 list( $carrier, $machine ) = explode( '||', $machine_id );
61
62 if ( empty( $machine ) ) {
63 return;
64 }
65
66 $machine = self::mk_get_machine( $carrier, $machine );
67
68 if ( !$machine ) {
69 return;
70 }
71
72 if ( $carrier == 'lp_express_lt' ) {
73 $carrier = "LP Express";
74 }
75
76 echo '
77 <tr>
78 <th>'.__( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).') </th>
79 <td>'.$machine['name'].'<br/>'.$machine['address'].'</td>
80 </tr>
81 ';
82 }
83
84 /**
85 * Allow changing parcel machine in admin and show data in order view
86 *
87 * @since 3.0.0
88 */
89 public function parcel_machine_changing( $order ) {
90
91 $machine_id = $order->get_meta( '_parcel_machine', true );
92 $carrier = '';
93
94 if ( !empty( $machine_id ) ) {
95
96 list( $carrier, $machine ) = explode( '||', $machine_id );
97 }
98
99 if ( !empty( $machine ) ) {
100
101 $machine = self::mk_get_machine( $carrier, $machine );
102
103 if ( $machine ) {
104
105 echo '
106 <div class="edit_address">
107 <p class="form-field form-field-wide _mk_machine_id" style="padding-bottom: 10px !important;">
108 <label for="_mk_machine_id">'.__( 'Parcel machine', 'wc_makecommerce_domain' ).'</label>
109 <select id="_mk_machine_id" name="_mk_machine_id" class="wc-enhanced-select select">
110 ';
111
112 $machines = self::mk_get_machines( $carrier, method_exists( $order, 'get_shipping_country' ) ? $order->get_shipping_country() : $order->shipping_country , [] );
113
114 echo Method\ParcelMachine::create_parcelmachine_html( $machines, 'no', $machine['id'] );
115
116 echo '
117 </select>
118 </p>
119 </div>
120 ';
121
122 // Add the parcel size templates to LP Express orders
123 if ( $carrier === 'lp_express_lt' ) {
124
125 echo '
126 <div class="edit_address">
127 <p class="form-field form-field-wide _mk_template_id" style="padding-bottom: 10px !important;">
128 <label for="_mk_template_id">'.__( 'LP Express template', 'wc_makecommerce_domain' ).'</label>
129 <select id="_mk_template_id" name="_mk_template_id" class="wc-enhanced-select select">
130 ';
131
132 // Get all the presets from API
133 $presetArray = \MakeCommerce\Shipping\Method\ParcelMachine\LPExpress::get_lp_express_presets();
134 // Default setting
135 $default = get_option( 'mk_lpexpress_template' );
136 // If order has been edited and a template has been chosen
137 if ( !empty( $order->get_meta( '_mk_parcel_template', true ) ) ) {
138 $template = $order->get_meta( '_mk_parcel_template', true );
139 } else {
140 // Otherwise use the default preset
141 $template = $default;
142 }
143 $chosenTemplate = $presetArray[$template];
144
145 $output = '';
146
147 // Add all the presets as html
148 foreach ( $presetArray as $key => $label ) {
149 $output .= '<option value="'. $key .'" '.( $template == $key ? ' selected="selected"' : '' ).'>'.$label.'</option>';
150 }
151
152 echo $output .'
153 </select>
154 </p>
155 </div>
156 ';
157
158
159 // Show the chosen template in the order view
160 echo '
161 <div class="address">
162 <p>LP Express</strong><br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>
163 <p>Chosen template for LP Express: <strong>'.$chosenTemplate.'</strong></p>';
164 } else {
165 echo '
166 <div class="address">
167 <p>'.ucfirst( $carrier ).'</strong><br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>';
168 }
169 }
170 } else {
171 //in case of a courier, wrap the time and trackinglink in address div as well
172 //to hide it during order editing.
173 //can be an empty div in case of other shipping methods
174 echo '<div class="address">';
175 }
176
177 $shipment_id = $order->get_meta( '_parcel_machine_shipment_id', true );
178 $shipment_id_error = $order->get_meta( '_parcel_machine_error', true );
179 $shipment_manifest = $order->get_meta( '_parcel_machine_manifest', true );
180
181 $shippingMethod["method_title"] = '';
182
183 //currently the only delivery time option exists for smartpost courier shipping method.
184 //only one shipping method, get it easily from array with foreach
185 foreach ( $order->get_shipping_methods() as $shippingMethod) {
186
187 if ( $shippingMethod["method_id"] == "courier_smartpost" && !$shipment_id_error ) {
188 $delivery_time = $order->get_meta( '_delivery_time', true );
189
190 switch ( $delivery_time ) {
191 case '1':
192 echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - '.__( 'Any time', 'wc_makecommerce_domain' ).'</p>';
193 break;
194 case '2':
195 echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - 09:00..17:00</p>';
196 break;
197 case '3':
198 echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - 17:00..21:00</p>';
199 break;
200 }
201 }
202 }
203
204 if ( $shipment_id ) {
205 $trackinglink = '<p><strong>'.__( 'Shipment tracking code', 'wc_makecommerce_domain' ).'</strong>';
206
207 if ( $carrier == "" ) {
208 $carrier = $this->get_order_shipment_carrier( $order );
209 }
210
211 if ( $carrier === 'lp_express_lt' ) {
212 $shipment_id = $order->get_meta( '_parcel_machine_shipment_barcode', true );
213 $carrier = "lpexpress";
214 }
215
216 $theLink = $this->get_tracking_link( $carrier, $order, $shipment_id, true );
217
218 if ( $carrier != "" && $theLink != "" ) {
219 $trackinglink .= ' ('. ucfirst( $shippingMethod["method_title"] ) .'): <br/> <a target="_blank" href="' . $theLink . '">' . $shipment_id . '</a>';
220 } else {
221 $trackinglink .= ': <br/>'.$shipment_id;
222 }
223
224 echo $trackinglink.'</p>';
225 }
226
227 if ( $shipment_manifest ) {
228 echo '<p><strong>'.__( 'Shipments pickup manifest' ).':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>';
229 }
230
231 if ( $shipment_id_error ) {
232 echo '<p><strong style="color: red;">'.__( 'Shipment registration error' ).':</strong><br/>' . $shipment_id_error . '</small></p>';
233 }
234
235 echo '</div>';
236 }
237
238 /**
239 * Add shipping information on order list view
240 *
241 * @since 3.0.0
242 */
243 public function shipping_method_order_view( $column ) {
244
245 global $post, $woocommerce, $the_order;
246
247 if ( !$the_order ) {
248 return;
249 }
250
251 $the_order_id = $the_order->get_id();
252
253 if ( empty( $the_order ) || $the_order_id != $post->ID ) {
254
255 $the_order = wc_get_order( $post->ID );
256 }
257
258 if ( $column === 'shipping_address' ) {
259
260 $shipment_id = $the_order->get_meta( '_parcel_machine_shipment_id', true );
261 $shipment_id_error = $the_order->get_meta( '_parcel_machine_error', true );
262 $identifier = $the_order->get_meta( '_lp_express_cart_identifier', true );
263 $barcode = $the_order->get_meta( '_parcel_machine_shipment_barcode', true );
264
265 // LP Express
266 if ( !empty( $identifier ) && !empty( $barcode ) ) {
267 $shipment_id = $barcode;
268 }
269
270 if ( $shipment_id ) {
271
272 echo __( 'Shipment tracking code', 'wc_makecommerce_domain' ) . ': ' . $shipment_id . '<br/>';
273 echo '<span class="has_shipment_id"></span>';
274 } else if ( $shipment_id_error ) {
275 echo '<span style="color: red;">'.__( 'Package shipment generation error:', 'wc_makecommerce_domain' ) . '</span><br/>' .$shipment_id_error;
276 }
277 }
278 }
279
280 /**
281 * Fires before the Filter button on the Posts and Pages list tables.
282 * Allows filtering of orders by shipping method
283 *
284 * @since 3.0.0
285 */
286 public function filter_orders() {
287
288 global $typenow;
289
290 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
291
292 $selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false;
293 $methods = WC()->shipping->load_shipping_methods();
294
295 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
296 echo '<option value="">'.__( '-- filter by shipping method', 'wc_makecommerce_domain' ) . '</option>';
297
298 foreach ( $methods as $method ) {
299 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->get_method_title().'</option>';
300 }
301
302 echo '</select>';
303 }
304 }
305
306 /**
307 * Fires before the Filter button on the orders page
308 * Allows filtering of orders by shipping method
309 *
310 * @since 3.4.0
311 */
312 public function hpos_filter_orders() {
313
314 if ( $_GET['page'] == 'wc-orders' ) {
315
316 $selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false;
317 $methods = WC()->shipping->load_shipping_methods();
318
319 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
320 echo '<option value="">'.__( '-- filter by shipping method', 'wc_makecommerce_domain' ) . '</option>';
321
322 foreach ( $methods as $method ) {
323 // Can not filter by methods which are not provided by MK
324 if ( !$method instanceof \MakeCommerce\Shipping\Method ) continue;
325 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->get_method_title().'</option>';
326 }
327
328 echo '</select>';
329 }
330 }
331
332 /**
333 * Returns order shipment carrier
334 *
335 * @since 3.0.0
336 */
337 private function get_order_shipment_carrier( $order ) {
338
339 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
340 $carrier = explode( "_", $shippingMethod["method_id"] );
341
342 $carrier = $carrier[1];
343 }
344
345 return $carrier;
346 }
347
348 /**
349 * Add shipment data to email
350 *
351 * @since 3.0.0
352 */
353 public function shipping_email_details( $fields, $sent_to_admin, $order ) {
354 // No mc metadata without calling the wc_get_order again
355 $order = wc_get_order( $order->get_id() );
356
357 //check if makecommerce shipping has been used
358 //add tracking information (if possible)
359 $shipment_id = $order->get_meta( "_parcel_machine_shipment_id", true );
360
361 $machine_id = $order->get_meta( '_parcel_machine', true );
362
363 $machine = "";
364 $carrier = "";
365
366 $machineIdArr = explode( '||', $machine_id );
367
368 if ( count( $machineIdArr ) == 2 ) {
369
370 $carrier = $machineIdArr[0];
371 $machine = $machineIdArr[1];
372 } else if ( count( $machineIdArr ) == 1 ) {
373
374 $carrier = $machineIdArr[0];
375 }
376
377 $machine = self::mk_get_machine( $carrier, $machine );
378
379 //do something about the language only if we even have something to add to the email
380 if ( $shipment_id || ( $machine != "" && $carrier != "" ) ) {
381
382 //this only needs to be done for polylang. Don't do it for wpml, that won't work.
383 if ( \MakeCommerce\i18n::is_polylang_active() ) {
384
385 $orderLang = get_locale();
386
387 if ( $orderLang != "" ) {
388
389 if ( substr( $orderLang, 0 ,2 ) == "en" ) {
390
391 switch_to_locale( "et" );
392 do_action( 'wpml_switch_language', "et" );
393 } else {
394
395 switch_to_locale( "en_GB" );
396 do_action( 'wpml_switch_language', "en_GB" );
397 }
398
399 switch_to_locale( $orderLang );
400 do_action( 'wpml_switch_language', $orderLang );
401 }
402
403 load_plugin_textdomain( 'wc_makecommerce_domain', false, '/makecommerce/languages/' );
404 }
405 }
406
407 if ( $machine != "" && $carrier != "" ) {
408 //add parcel machine name/location
409 if ( strtolower( $carrier ) == 'lp_express_lt') {
410 $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' (LP Express)', 'value' => $machine['name'].' - '.$machine['address']);
411 } else {
412 $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).')', 'value' => $machine['name'].' - '.$machine['address']);
413 }
414 }
415
416 $shipment_id_error = $order->get_meta( '_parcel_machine_error', true );
417
418 //currently the only delivery time option exists for smartpost courier shipping method.
419 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
420
421 if ( $shippingMethod["method_id"] == "courier_smartpost" ) {
422 $delivery_time = $order->get_meta( '_delivery_time', true );
423
424 if ( !$shipment_id_error ) {
425
426 if ( $delivery_time === '1' ) {
427
428 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => __( 'Any time', 'wc_makecommerce_domain' ) );
429 }
430
431 if ( $delivery_time === '2' ) {
432
433 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "09:00..17:00" );
434 }
435
436 if ( $delivery_time === '3' ) {
437
438 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "17:00..21:00" );
439 }
440 }
441 }
442 }
443
444 //add tracking information (if possible)
445 if ( $shipment_id && strtolower( $carrier ) !== "lp_express_lt" ) {
446
447 if ( $carrier == "" ) {
448 $carrier = $this->get_order_shipment_carrier( $order );
449 }
450
451 $href = $this->get_tracking_link( $carrier, $order, $shipment_id );
452
453 if ( $href != "" ) {
454 $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>" );
455 } else {
456 $fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ), 'value' => $shipment_id );
457 }
458 }
459
460 return $fields;
461 }
462
463 /**
464 * Adds bulk actions to orders page without JS
465 * Works with HPOS enabled
466 *
467 * @since 3.4.0
468 */
469 public function mc_bulk_actions( $actions ) {
470 $actions['parcel_machine_register_orders'] = __( 'Register parcel machine shipments', 'wc_makecommerce_domain' );
471 $actions['parcel_machine_print_labels'] = __( 'Print parcel machine labels', 'wc_makecommerce_domain' );
472 return $actions;
473 }
474
475 /**
476 * Filters orders by _mc_shipping_method meta key
477 * Relies on hpos_filter_orders()
478 * Works with HPOS enabled
479 *
480 * @since 3.4.0
481 */
482 public function order_filter_by_shipping_method( $args ) {
483
484 if ( is_admin() && !empty( $_REQUEST['_shipping_method'] ) && $_GET['page'] == 'wc-orders' ) {
485 $shipping_method = sanitize_text_field( $_REQUEST['_shipping_method'] );
486
487 $args['meta_query'] = [
488 [
489 'key' => '_mc_shipping_method',
490 'value' => $shipping_method
491 ]
492 ];
493 }
494
495 return $args;
496 }
497 }
498