| 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 |
if ( is_wp_error( $response ) ) { |
| 105 |
throw new Exception($response->get_error_message()); |
| 106 |
} |
| 107 |
} catch (Exception $exception) { |
| 108 |
$routeapp_public = self::get_route_public_instance(); |
| 109 |
$params['tracking_id'] = $tracking_number; |
| 110 |
$extraData = array( |
| 111 |
'params' => $params, |
| 112 |
'method' => 'POST' |
| 113 |
); |
| 114 |
if ($routeapp_public) { |
| 115 |
$routeapp_public->routeapp_log($exception, $extraData); |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
if ($existing_tracking_numbers[0]!='' && !empty(array_filter($tracking_numbers_array))) { |
| 122 |
foreach ($existing_tracking_numbers as $existing_tracking_number) { |
| 123 |
if (!in_array($existing_tracking_number, $tracking_numbers_array)) { |
| 124 |
$route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
$tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array); |
| 129 |
if (!empty($tracking_number)) { |
| 130 |
$this->add_custom_post_meta($order_id, $tracking_numbers); |
| 131 |
} |
| 132 |
return true; |
| 133 |
}else { |
| 134 |
/* If user removed the order note from Woo */ |
| 135 |
if ($existing_tracking_numbers[0]!='') { |
| 136 |
foreach ($existing_tracking_numbers as $existing_tracking_number) { |
| 137 |
$route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private function parse_individual_order_note($note) { |
| 144 |
$tracking_data = []; |
| 145 |
if (strpos($note, 'tracking number')) { |
| 146 |
$lines = explode('shipped via', $note); |
| 147 |
$lines[1] = trim($lines[1]); |
| 148 |
$lines = explode('tracking number', $lines[1]); |
| 149 |
$courier_id = explode(' ', $lines[0]); |
| 150 |
$courier_id = $courier_id[0]; |
| 151 |
$tracking_number = $lines[1]; |
| 152 |
$tracking_data['tracking_number']= $this->sanitize_value($tracking_number); |
| 153 |
$tracking_data['courier_id']= $courier_id; |
| 154 |
if (isset($tracking_data['tracking_number']) && isset($tracking_data['courier_id'])) { |
| 155 |
return $tracking_data; |
| 156 |
} |
| 157 |
} |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
private function sanitize_value($value) { |
| 162 |
$value = str_replace(['-(SHIPSTATION)', '(SHIPSTATION)-', '(Shipstation)'], '', $value); |
| 163 |
$value = str_replace('.', '', $value); |
| 164 |
$value = trim($value); |
| 165 |
return $value; |
| 166 |
} |
| 167 |
} |
| 168 |
|