PluginProbe
MakeCommerce for WooCommerce / 3.3.1
MakeCommerce for WooCommerce v3.3.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 3.3.1, at shipping/order.php

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