PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.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
makecommerce / shipping / order.php

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

404 lines 12.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 echo '
67 <tr>
68 <th>'.__( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).') </th>
69 <td>'.$machine['name'].'<br/>'.$machine['address'].'</td>
70 </tr>
71 ';
72 }
73
74 /**
75 * Allow changing parcel machine in admin and show data in order view
76 *
77 * @since 3.0.0
78 */
79 public function parcel_machine_changing( $order ) {
80
81 echo '
82 </div>
83
84 <div class="order_data_column" style="float: right;">
85 ';
86
87 $order_id = $order->get_id();
88 $machine_id = get_post_meta( $order_id, '_parcel_machine', true );
89
90 if ( !empty( $machine_id ) ) {
91
92 list( $carrier, $machine ) = explode( '||', $machine_id );
93
94 if ( !empty( $machine ) ) {
95
96 $machine = self::mk_get_machine( $carrier, $machine );
97
98 if ( $machine ) {
99
100 echo '
101 <h3>'.__( 'Selected parcel machine', 'wc_makecommerce_domain' ).'<a href="#" class="edit_address">'.__( 'Edit' ).'</a></h3>
102 <div class="edit_address">
103 <p class="form-field form-field-wide _mk_machine_id">
104 <label for="_mk_machine_id">'.__( 'Parcel machine', 'wc_makecommerce_domain' ).'</label>
105 <select id="_mk_machine_id" name="_mk_machine_id" class="wc-enhanced-select select">
106 ';
107
108 $res = '';
109 $machines = self::mk_get_machines( $carrier, method_exists( $order, 'get_shipping_country' ) ? $order->get_shipping_country() : $order->shipping_country );
110
111 $res .= '<option value="">'.__( '-- select parcel machine --', 'wc_makecommerce_domain' ).'</option>';
112
113 $pcity = false;
114 foreach ( $machines as $smachine ) {
115
116 $city = strtolower( $smachine['city'] );
117
118 if ( $city !== $pcity ) {
119
120 if ( $pcity ) {
121 $res .= '</optgroup>';
122 }
123
124 $res .= '<optgroup label="'.$smachine['city'].'">';
125 }
126
127 $mname = $smachine['name'];
128 $res .= '<option value="'.esc_attr( $smachine['carrier'].'||'.$smachine['id'] ).'" '.( $machine['id'] === $smachine['id'] ? ' selected="selected"' : '' ).'>'.$mname.'</option>';
129 $pcity = $city;
130 }
131
132 if ( $pcity ) {
133 $res .= '</optgroup>';
134 }
135
136 echo $res;
137
138 echo '
139 </select>
140 </p>
141 </div>
142
143 <div class="address">
144 <p>'.ucfirst($carrier).'</strong><br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p>
145 </div>
146 ';
147 }
148 }
149 }
150
151 if ( empty( $carrier ) ) {
152 $carrier = '';
153 }
154
155 $shipment_id = get_post_meta( $order_id, '_parcel_machine_shipment_id', true );
156 $shipment_id_error = get_post_meta( $order_id, '_parcel_machine_error', true );
157 $shipment_manifest = get_post_meta( $order_id, '_parcel_machine_manifest', true );
158
159 //currently the only delivery time option exists for smartpost courier shipping method.
160 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
161
162 if ( $shippingMethod["method_id"] == "courier_smartpost" ) {
163
164 $delivery_time = get_post_meta( $order_id, '_delivery_time', true );
165
166 if ( !$shipment_id_error ) {
167
168 if ( $delivery_time === '1' ) {
169
170 echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - '.__( 'Any time', 'wc_makecommerce_domain' ).'</p>';
171 }
172
173 if ( $delivery_time === '2' ) {
174
175 echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - 09:00..17:00</p>';
176 }
177
178 if ( $delivery_time === '3' ) {
179
180 echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - 17:00..21:00</p>';
181 }
182 }
183 }
184 }
185
186 if ( $shipment_id ) {
187 $trackinglink = '<p><strong>'.__( 'Shipment tracking code', 'wc_makecommerce_domain' ).'</strong>';
188
189 //check if its omniva courier
190 if ( $carrier == "" ) {
191 $carrier = $this->get_order_shipment_carrier( $order );
192 }
193
194 $theLink = $this->get_tracking_link( $carrier, $order_id, true );
195
196 if ( $carrier != "" && $theLink != "" ) {
197
198 $trackinglink .= ' ('. ucfirst( $carrier ) .'): <br/> <a target="_blank" href="' . $theLink . $shipment_id . '">' . $shipment_id . '</a>';
199 } else {
200 $trackinglink .= ': <br/>'.$shipment_id;
201 }
202
203 echo $trackinglink.'</p>';
204 }
205
206 if ( $shipment_manifest ) {
207 echo '<p><strong>'.__( 'Shipments pickup manifest' ).':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>';
208 }
209
210 if ( $shipment_id_error ) {
211 echo '<p><strong style="color: red;">'.__( 'Shipment registration error' ).':</strong><br/>' . $shipment_id_error . '</small></p>';
212 }
213
214 echo '</div>';
215 }
216
217 /**
218 * Add shipping information on order list view
219 *
220 * @since 3.0.0
221 */
222 public function shipping_method_order_view( $column ) {
223
224 global $post, $woocommerce, $the_order;
225
226 if ( !$the_order ) {
227 return;
228 }
229
230 $the_order_id = $the_order->get_id();
231
232 if ( empty( $the_order ) || $the_order_id != $post->ID ) {
233
234 $the_order = wc_get_order( $post->ID );
235 }
236
237 if ( $column === 'shipping_address' ) {
238
239 $machine_id = get_post_meta( $the_order_id, '_parcel_machine', true );
240 $shipment_id = get_post_meta( $the_order_id, '_parcel_machine_shipment_id', true );
241 $shipment_id_error = get_post_meta( $the_order_id, '_parcel_machine_error', true );
242
243 if ( $shipment_id ) {
244
245 echo __( 'Shipment tracking code', 'wc_makecommerce_domain' ) . ': ' . $shipment_id . '<br/>';
246 echo '<span class="has_shipment_id"></span>';
247 } else if ( $shipment_id_error ) {
248 echo '<span style="color: red;">'.__( 'Package shipment generation error:', 'wc_makecommerce_domain' ) . '</span><br/>' .$shipment_id_error;
249 }
250 }
251 }
252
253 /**
254 * Fires before the Filter button on the Posts and Pages list tables.
255 * Allows filtering of orders by shipping method
256 *
257 * @since 3.0.0
258 */
259 public function filter_orders() {
260
261 global $typenow;
262
263 if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
264
265 $selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false;
266 $methods = WC()->shipping->load_shipping_methods();
267
268 echo '<select name="_shipping_method" id="shipping_type" class="enhanced">';
269 echo '<option value="">'.__( '-- filter by shipping method', 'wc_makecommerce_domain' ) . '</option>';
270
271 foreach ( $methods as $method ) {
272
273 echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>';
274 }
275
276 echo '</select>';
277 }
278 }
279
280 private function get_order_shipment_carrier( $order ) {
281
282 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
283 $carrier = explode( "_", $shippingMethod["method_id"] );
284
285 $carrier = $carrier[1];
286 }
287
288 return $carrier;
289 }
290
291 /**
292 * Add shipment data to email
293 *
294 * @since 3.0.0
295 */
296 public function shipping_email_details( $fields, $sent_to_admin, $order ) {
297
298 //check if makecommerce shipping has been used
299 //add tracking information (if possible)
300 $shipment_id = get_post_meta( $order->get_id(), "_parcel_machine_shipment_id", true );
301
302 $machine_id = get_post_meta( $order->get_id(), '_parcel_machine', true );
303
304 $machine = "";
305 $carrier = "";
306
307 $machineIdArr = explode( '||', $machine_id );
308
309 if ( count( $machineIdArr ) == 2 ) {
310
311 $carrier = $machineIdArr[0];
312 $machine = $machineIdArr[1];
313 } else if ( count( $machineIdArr ) == 1 ) {
314
315 $carrier = $machineIdArr[0];
316 }
317
318 $machine = self::mk_get_machine( $carrier, $machine );
319
320 //do something about the language only if we even have something to add to the email
321 if ( $shipment_id || ( $machine != "" && $carrier != "" ) ) {
322
323 //this only needs to be done for polylang. Don't do it for wpml, that won't work.
324 if ( \MakeCommerce\i18n::is_polylang_active() ) {
325
326 $orderLang = get_locale();
327
328 if ( $orderLang != "" ) {
329
330 if ( substr( $orderLang, 0 ,2 ) == "en" ) {
331
332 switch_to_locale( "et" );
333 do_action( 'wpml_switch_language', "et" );
334 } else {
335
336 switch_to_locale( "en_GB" );
337 do_action( 'wpml_switch_language', "en_GB" );
338 }
339
340 switch_to_locale( $orderLang );
341 do_action( 'wpml_switch_language', $orderLang );
342 }
343
344 load_plugin_textdomain( 'wc_makecommerce_domain', false, '/makecommerce/languages/' );
345 }
346 }
347
348 if ( $machine != "" && $carrier != "" ) {
349 //add parcel machine name/location
350 $fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).')', 'value' => $machine['name'].' - '.$machine['address']);
351 }
352
353 $shipment_id_error = get_post_meta( $order->get_id(), '_parcel_machine_error', true );
354
355 //currently the only delivery time option exists for smartpost courier shipping method.
356 foreach ( $order->get_shipping_methods() as $shippingMethod ) {
357
358 if ( $shippingMethod["method_id"] == "courier_smartpost" ) {
359
360 $delivery_time = get_post_meta( $order->get_id(), '_delivery_time', true );
361
362 if ( !$shipment_id_error ) {
363
364 if ( $delivery_time === '1' ) {
365
366 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => __( 'Any time', 'wc_makecommerce_domain' ) );
367 }
368
369 if ( $delivery_time === '2' ) {
370
371 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "09:00..17:00" );
372 }
373
374 if ( $delivery_time === '3' ) {
375
376 $fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "17:00..21:00" );
377 }
378 }
379 }
380 }
381
382 //add tracking information (if possible)
383 if ( $shipment_id ) {
384
385 $trackinglink = '<p><strong>'.__(' Shipment tracking code', 'wc_makecommerce_domain' ).'</strong>';
386
387 if ( $carrier == "" ) {
388 $carrier = $this->get_order_shipment_carrier( $order );
389 }
390
391 $href = $this->get_tracking_link( $carrier, $order->get_id() );
392
393 if ( $href != "" ) {
394
395 $href .= $shipment_id;
396 $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>" );
397 } else {
398 $fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ), 'value' => $shipment_id );
399 }
400 }
401
402 return $fields;
403 }
404 }