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-shipstation.php

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

166 lines 7.0 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
71 if (!$order_notes && !$existing_tracking_numbers) return;
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 // Skip if tracking number contains HTML tags
81 $has_html = isset($track_data['tracking_number']) &&
82 $track_data['tracking_number'] !== strip_tags($track_data['tracking_number']);
83 if (isset($track_data['tracking_number']) && isset($track_data['courier_id']) && !$has_html) {
84 $tracking_number = $track_data['tracking_number'];
85 $tracking_numbers_array[] = $tracking_number;
86 if (in_array($tracking_number, $existing_tracking_numbers) || empty($tracking_number)) {
87 continue;
88 }
89
90 $shipmentResponse = $route_app->routeapp_api_client->get_shipment($tracking_number, $order_id);
91 if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) {
92 continue;
93 }
94 $params = array(
95 'source_order_id' => $order_id,
96 'source_product_ids' => $product_ids,
97 'courier_id' => $track_data['courier_id']
98 );
99 $extraData = array();
100 $response = $route_app->routeapp_api_client->create_shipment($tracking_number, $params);
101 $extraData['endpoint'] = 'shipments';
102
103 try{
104 self::assert_successful_shipment_create( $response );
105 } catch (Exception $exception) {
106 $routeapp_public = self::get_route_public_instance();
107 $params['tracking_id'] = $tracking_number;
108 $extraData = array(
109 'params' => $params,
110 'method' => 'POST'
111 );
112 if ($routeapp_public) {
113 $routeapp_public->routeapp_log($exception, $extraData);
114 }
115 return false;
116 }
117 }
118 }
119 if ($existing_tracking_numbers[0]!='' && !empty(array_filter($tracking_numbers_array))) {
120 foreach ($existing_tracking_numbers as $existing_tracking_number) {
121 if (!in_array($existing_tracking_number, $tracking_numbers_array)) {
122 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
123 }
124 }
125 }
126 $tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array);
127 if (!empty($tracking_number)) {
128 $this->add_custom_post_meta($order_id, $tracking_numbers);
129 }
130 return true;
131 }else {
132 /* If user removed the order note from Woo */
133 if ($existing_tracking_numbers[0]!='') {
134 foreach ($existing_tracking_numbers as $existing_tracking_number) {
135 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
136 }
137 }
138 }
139 }
140
141 private function parse_individual_order_note($note) {
142 $tracking_data = [];
143 if (strpos($note, 'tracking number')) {
144 $lines = explode('shipped via', $note);
145 $lines[1] = trim($lines[1]);
146 $lines = explode('tracking number', $lines[1]);
147 $courier_id = explode(' ', $lines[0]);
148 $courier_id = $courier_id[0];
149 $tracking_number = $lines[1];
150 $tracking_data['tracking_number']= $this->sanitize_value($tracking_number);
151 $tracking_data['courier_id']= $courier_id;
152 if (isset($tracking_data['tracking_number']) && isset($tracking_data['courier_id'])) {
153 return $tracking_data;
154 }
155 }
156 return false;
157 }
158
159 private function sanitize_value($value) {
160 $value = str_replace(['-(SHIPSTATION)', '(SHIPSTATION)-', '(Shipstation)'], '', $value);
161 $value = str_replace('.', '', $value);
162 $value = trim($value);
163 return $value;
164 }
165 }
166