PluginProbe
Route ‑ Shipping Protection / 2.4.5
Route ‑ Shipping Protection v2.4.5
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-shipstation.php

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

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