PluginProbe
Route ‑ Shipping Protection / 2.4.18
Route ‑ Shipping Protection v2.4.18
2.4.19 2.4.18 2.4.17 2.4.16 2.4.15 2.4.14 2.4.13 2.4.12 2.4.11 2.4.10 2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.2.4 2.2.5 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 All 153 releases
routeapp / includes / tracking-providers / class-routeapp-shipworks.php

class-routeapp-shipworks.php in Route ‑ Shipping Protection 2.4.18, at includes/tracking-providers/class-routeapp-shipworks.php

158 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class Routeapp_Shipworks extends Routeapp_WooCommerce_Common_Tracking_Provider implements Routeapp_WooCommerce_Tracking_Provider
3 {
4 public function __construct() {
5 }
6
7 public function is_active() {
8 return in_array( 'shipworks-e-commerce-bridge/controler.php', (array) get_option( 'active_plugins', array() ));
9 }
10
11 public static function get_route_public_instance(){
12 global $routeapp_public;
13 return $routeapp_public;
14 }
15
16 public function get_shipping_provider_name($order_id)
17 {
18 $couriers_array = [];
19 $order_notes = wc_get_order_notes([
20 'order_id' => $order_id,
21 'type' => 'system',
22 ]);
23 if (count($order_notes)>0) {
24 foreach ($order_notes as $order_note) {
25 $track_data = $this->parse_individual_order_note($order_note->content);
26 if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) {
27 $couriers_array[$track_data['tracking_number']] = $track_data['courier_id'];
28 }
29 }
30 }
31 return $couriers_array;
32 }
33
34 public function get_shipping_info($order_id)
35 {
36 $shipping_info = [];
37 $order_notes = wc_get_order_notes([
38 'order_id' => $order_id,
39 'type' => 'system',
40 ]);
41 if (count($order_notes)>0) {
42 $product_ids = $this->get_order_products($order_id);
43 foreach ($order_notes as $order_note) {
44 $track_data = $this->parse_individual_order_note($order_note->content);
45 if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) {
46 $shipping_info[] = array(
47 'source_order_id' => $order_id,
48 'source_product_ids' => $product_ids,
49 'courier_id' => $track_data['courier_id'],
50 'tracking_number' => $track_data['tracking_number']
51 );
52 }
53 }
54 }
55 return $shipping_info;
56 }
57
58 public function update($order_id, $api_client)
59 {
60 return;
61 }
62
63 public function parse_order_notes($order_id, $route_app)
64 {
65 $order_notes = wc_get_order_notes([
66 'order_id' => $order_id,
67 'type' => 'system',
68 ]);
69 $existing_tracking_numbers = (array) explode( self::SEPARATOR_PIPE,
70 $this->get_meta_data($order_id, 'routeapp_shipment_tracking_number'));
71 if (!$order_notes && !$existing_tracking_numbers) return false;
72
73 $order = wc_get_order( $order_id );
74 $product_ids = $this->get_order_products($order_id);
75
76 if (count($order_notes)>0) {
77 $tracking_numbers_array = array();
78 foreach ($order_notes as $order_note) {
79 $track_data = $this->parse_individual_order_note($order_note->content);
80 if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) {
81 $tracking_number = $track_data['tracking_number'];
82 $tracking_numbers_array[] = $tracking_number;
83 if (in_array($tracking_number, $existing_tracking_numbers)) {
84 continue;
85 }
86
87 $shipmentResponse = $route_app->routeapp_api_client->get_shipment($tracking_number, $order_id);
88 if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) {
89 continue;
90 }
91 $params = array(
92 'source_order_id' => $order_id,
93 'source_product_ids' => $product_ids,
94 'courier_id' => $track_data['courier_id']
95 );
96 $extraData = array();
97 $response = $route_app->routeapp_api_client->create_shipment($tracking_number, $params);
98 $extraData['endpoint'] = 'shipments';
99
100 try{
101 self::assert_successful_shipment_create( $response );
102 } catch (Exception $exception) {
103 $routeapp_public = self::get_route_public_instance();
104 $params['tracking_id'] = $tracking_number;
105 $extraData = array(
106 'params' => $params,
107 'method' => 'POST'
108 );
109 if ($routeapp_public) {
110 $routeapp_public->routeapp_log($exception, $extraData);
111 }
112 return false;
113 }
114 }
115 }
116 if ($existing_tracking_numbers[0]!='' && !empty($tracking_numbers_array)) {
117 foreach ($existing_tracking_numbers as $existing_tracking_number) {
118 if (!in_array($existing_tracking_number, $tracking_numbers_array)) {
119 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
120 }
121 }
122 }
123 $tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array);
124 if($tracking_numbers!='') {
125 $this->add_custom_post_meta($order_id, $tracking_numbers);
126 }
127 return true;
128 }else {
129 /* If user removed the order note from Woo */
130 if ($existing_tracking_numbers[0]!='') {
131 foreach ($existing_tracking_numbers as $existing_tracking_number) {
132 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
133 }
134 }
135 }
136 }
137
138 private function parse_individual_order_note($note) {
139 $tracking_data = [];
140 if (strpos($note, 'Tracking number') && strpos($note, ' via ')) {
141 //Example text: "Your order 63 was shipped on 01-26-2021 via USPS. Tracking number is 01262021A."
142 //cut the text and keep with the last part:"USPS. Tracking number is 01262021A."
143 $cutNote = explode(' via ', $note);
144 $cutNote = $cutNote[1];
145 //cut text again, to keep with courier id
146 $cutNote = explode('.', $cutNote);
147 $tracking_data['courier_id'] = trim($cutNote[0]);
148 //now the note is "Tracking number is 01262021A"
149 $cutNote = $cutNote[1];
150 //cut text again, to grab tracking number
151 $cutNote = explode('Tracking number is', $cutNote);
152 $tracking_data['tracking_number'] = trim($cutNote[1]);
153 return $tracking_data;
154 }
155 return false;
156 }
157 }
158