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

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

427 lines 13.6 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 && $carrier !== 'lp_express_lt' ) {
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 $theLink = $this->get_tracking_link( $carrier, $order_id, true );
208 if ( $carrier != "" && $theLink != "" ) {
209 $trackinglink .= ' ('. ucfirst( $shippingMethod["method_title"] ) .'): <br/> <a target="_blank" href="' . $theLink . $shipment_id . '">' . $shipment_id . '</a>';
210 } else {
211 $trackinglink .= ': <br/>'.$shipment_id;
212 }
213
214 echo $trackinglink.'</p>';
215 }
216
217 if ( $shipment_manifest ) {
218 echo '<p><strong>'.__( 'Shipments pickup manifest' ).':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>';
219 }
220
221 if ( $shipment_id_error ) {
222 echo '<p><strong style="color: red;">'.__( 'Shipment registration error' ).':</strong><br/>' . $shipment_id_error . '</small></p>';
223 }
224
225 echo '</div>';
226 }
227
228 /**
229 * Add shipping information on order list view
230 *
231 * @since 3.0.0
232 */
233 public function shipping_method_order_view( $column ) {
234
235 global $post, $woocommerce, $the_order;
236
237 if ( !$the_order ) {
238 return;
239 }
240
241 $the_order_id = $the_order->get_id();
242
243 if ( empty( $the_order ) || $the_order_id != $post->ID ) {
244
245 $the_order = wc_get_order( $post->ID );
246 }
247
248 if ( $column === 'shipping_address' ) {
249
250 $machine_id = get_post_meta( $the_order_id, '_parcel_machine', true );
251 $shipment_id = get_post_meta( $the_order_id, '_parcel_machine_shipment_id', true );
252 $shipment_id_error = get_post_meta( $the_order_id, '_parcel_machine_error', true );
253 $identifier = get_post_meta( $the_order_id, '_lp_express_cart_identifier', true );
254
255 // LP Express does not have tracking, only add if identifier is an empty string
256 if ( $shipment_id && empty( $identifier ) ) {
257
258 echo __( 'Shipment tracking code', 'wc_makecommerce_domain' ) . ': ' . $shipment_id . '<br/>';
259 echo '<span class="has_shipment_id"></span>';
260 } else if ( $shipment_id_error ) {
261 echo '<span style="color: red;">'.__( 'Package shipment generation error:', 'wc_makecommerce_domain' ) . '</span><br/>' .$shipment_id_error;
262 }
263 }
264 }
265
266 /**
267 * Fires before the Filter button on the Posts and Pages list tables.
268 * Allows filtering of orders by shipping method
269 *
270 * @since 3.0.0
271 */
272 public function filter_orders() {
273
274 global $typenow;
275
276 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
277
278 $selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false;
279 $methods = WC()->shipping->load_shipping_methods();
280
281 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
282 echo '<option value="">'.__( '-- filter by shipping method', 'wc_makecommerce_domain' ) . '</option>';
283
284 foreach ( $methods as $method ) {
285
286 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>';
287 }
288
289 echo '</select>';
290 }
291 }
292
293 /**
294 * Returns order shipment carrier
295 *
296 * @since 3.0.0
297 */
298 private function get_order_shipment_carrier( $order ) {
299
300 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
301 $carrier = explode( "_", $shippingMethod["method_id"] );
302
303 $carrier = $carrier[1];
304 }
305
306 return $carrier;
307 }
308
309 /**
310 * Add shipment data to email
311 *
312 * @since 3.0.0
313 */
314 public function shipping_email_details( $fields, $sent_to_admin, $order ) {
315
316 //check if makecommerce shipping has been used
317 //add tracking information (if possible)
318 $shipment_id = get_post_meta( $order->get_id(), "_parcel_machine_shipment_id", true );
319
320 $machine_id = get_post_meta( $order->get_id(), '_parcel_machine', true );
321
322 $machine = "";
323 $carrier = "";
324
325 $machineIdArr = explode( '||', $machine_id );
326
327 if ( count( $machineIdArr ) == 2 ) {
328
329 $carrier = $machineIdArr[0];
330 $machine = $machineIdArr[1];
331 } else if ( count( $machineIdArr ) == 1 ) {
332
333 $carrier = $machineIdArr[0];
334 }
335
336 $machine = self::mk_get_machine( $carrier, $machine );
337
338 //do something about the language only if we even have something to add to the email
339 if ( $shipment_id || ( $machine != "" && $carrier != "" ) ) {
340
341 //this only needs to be done for polylang. Don't do it for wpml, that won't work.
342 if ( \MakeCommerce\i18n::is_polylang_active() ) {
343
344 $orderLang = get_locale();
345
346 if ( $orderLang != "" ) {
347
348 if ( substr( $orderLang, 0 ,2 ) == "en" ) {
349
350 switch_to_locale( "et" );
351 do_action( 'wpml_switch_language', "et" );
352 } else {
353
354 switch_to_locale( "en_GB" );
355 do_action( 'wpml_switch_language', "en_GB" );
356 }
357
358 switch_to_locale( $orderLang );
359 do_action( 'wpml_switch_language', $orderLang );
360 }
361
362 load_plugin_textdomain( 'wc_makecommerce_domain', false, '/makecommerce/languages/' );
363 }
364 }
365
366 if ( $machine != "" && $carrier != "" ) {
367 //add parcel machine name/location
368 if ( strtolower( $carrier ) == 'lp_express_lt') {
369 $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' (LP Express)', 'value' => $machine['name'].' - '.$machine['address']);
370 } else {
371 $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).')', 'value' => $machine['name'].' - '.$machine['address']);
372 }
373 }
374
375 $shipment_id_error = get_post_meta( $order->get_id(), '_parcel_machine_error', true );
376
377 //currently the only delivery time option exists for smartpost courier shipping method.
378 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
379
380 if ( $shippingMethod["method_id"] == "courier_smartpost" ) {
381
382 $delivery_time = get_post_meta( $order->get_id(), '_delivery_time', true );
383
384 if ( !$shipment_id_error ) {
385
386 if ( $delivery_time === '1' ) {
387
388 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => __( 'Any time', 'wc_makecommerce_domain' ) );
389 }
390
391 if ( $delivery_time === '2' ) {
392
393 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "09:00..17:00" );
394 }
395
396 if ( $delivery_time === '3' ) {
397
398 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "17:00..21:00" );
399 }
400 }
401 }
402 }
403
404 //add tracking information (if possible)
405 //no tracking information for lp express
406 if ( $shipment_id && strtolower( $carrier ) !== 'lp_express_lt') {
407
408 $trackinglink = '<p><strong>'.__(' Shipment tracking code', 'wc_makecommerce_domain' ).'</strong>';
409
410 if ( $carrier == "" ) {
411 $carrier = $this->get_order_shipment_carrier( $order );
412 }
413
414 $href = $this->get_tracking_link( $carrier, $order->get_id() );
415
416 if ( $href != "" ) {
417
418 $href .= $shipment_id;
419 $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>" );
420 } else {
421 $fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ), 'value' => $shipment_id );
422 }
423 }
424
425 return $fields;
426 }
427 }