| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
class Routeapp_ShippingEasyCustom extends Routeapp_WooCommerce_Common_Tracking_Provider implements Routeapp_WooCommerce_Tracking_Provider { |
| 5 |
public function __construct() { |
| 6 |
} |
| 7 |
|
| 8 |
public function is_active() { |
| 9 |
return in_array( 'woocommerce-shippingeasy/woocommerce-shippingeasy.php', (array) get_option( 'active_plugins', array() )); |
| 10 |
} |
| 11 |
|
| 12 |
public static function get_route_public_instance(){ |
| 13 |
global $routeapp_public; |
| 14 |
return $routeapp_public; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_shipping_provider_name($order_id) |
| 18 |
{ |
| 19 |
$couriers_array = []; |
| 20 |
$order_notes = wc_get_order_notes([ |
| 21 |
'order_id' => $order_id, |
| 22 |
'type' => 'system', |
| 23 |
]); |
| 24 |
if (count($order_notes)>0) { |
| 25 |
foreach ($order_notes as $order_note) { |
| 26 |
$track_data = $this->parse_individual_order_note($order_note->content); |
| 27 |
if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) { |
| 28 |
$couriers_array[$track_data['tracking_number']] = $track_data['courier_id']; |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
return $couriers_array; |
| 33 |
} |
| 34 |
|
| 35 |
public function get_shipping_info($order_id) |
| 36 |
{ |
| 37 |
$shipping_info = []; |
| 38 |
$order_notes = wc_get_order_notes([ |
| 39 |
'order_id' => $order_id, |
| 40 |
'type' => 'system', |
| 41 |
]); |
| 42 |
if (count($order_notes)>0) { |
| 43 |
$product_ids = $this->get_order_products($order_id); |
| 44 |
foreach ($order_notes as $order_note) { |
| 45 |
$track_data = $this->parse_individual_order_note($order_note->content); |
| 46 |
if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) { |
| 47 |
$shipping_info[] = array( |
| 48 |
'source_order_id' => $order_id, |
| 49 |
'source_product_ids' => $product_ids, |
| 50 |
'courier_id' => $track_data['courier_id'], |
| 51 |
'tracking_number' => $track_data['tracking_number'] |
| 52 |
); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
return $shipping_info; |
| 57 |
} |
| 58 |
|
| 59 |
public function update($order_id, $api_client) |
| 60 |
{ |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
public function parse_order_notes($order_id, $route_app) |
| 65 |
{ |
| 66 |
$order_notes = wc_get_order_notes([ |
| 67 |
'order_id' => $order_id, |
| 68 |
'type' => 'system', |
| 69 |
]); |
| 70 |
$existing_tracking_numbers = (array) explode( self::SEPARATOR_PIPE, |
| 71 |
$this->get_meta_data($order_id, 'routeapp_shipment_tracking_number')); |
| 72 |
if (!$order_notes && !$existing_tracking_numbers) return; |
| 73 |
|
| 74 |
$order = wc_get_order( $order_id ); |
| 75 |
$product_ids = $this->get_order_products($order_id); |
| 76 |
|
| 77 |
if (count($order_notes)>0) { |
| 78 |
$tracking_numbers_array = array(); |
| 79 |
foreach ($order_notes as $order_note) { |
| 80 |
$track_data = $this->parse_individual_order_note($order_note->content); |
| 81 |
if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) { |
| 82 |
$tracking_number = $track_data['tracking_number']; |
| 83 |
$tracking_numbers_array[] = $tracking_number; |
| 84 |
if (in_array($tracking_number, $existing_tracking_numbers)) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
$shipmentResponse = $route_app->routeapp_api_client->get_shipment($tracking_number, $order_id); |
| 89 |
if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
$params = array( |
| 93 |
'source_order_id' => $order_id, |
| 94 |
'source_product_ids' => $product_ids, |
| 95 |
'courier_id' => $track_data['courier_id'] |
| 96 |
); |
| 97 |
$extraData = array(); |
| 98 |
$response = $route_app->routeapp_api_client->create_shipment($tracking_number, $params); |
| 99 |
$extraData['endpoint'] = 'shipments'; |
| 100 |
|
| 101 |
try{ |
| 102 |
if ( is_wp_error( $response ) ) { |
| 103 |
throw new Exception($response->get_error_message()); |
| 104 |
} |
| 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 |
$routeapp_public->routeapp_log($exception, $extraData); |
| 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 ($tracking_numbers!='') { |
| 126 |
$this->add_custom_post_meta($order_id, $tracking_numbers); |
| 127 |
} |
| 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')) { |
| 141 |
$lines = explode('<br/>', $note); |
| 142 |
foreach ($lines as $line) { |
| 143 |
$line = explode(':', $line); |
| 144 |
switch (trim($line[0])) { |
| 145 |
case 'Shipping Tracking Number': |
| 146 |
$tracking_data['tracking_number']= trim($line[1]); |
| 147 |
break; |
| 148 |
case 'Carrier Key': |
| 149 |
$tracking_data['courier_id']= trim($line[1]); |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
if (isset($tracking_data['tracking_number']) && isset($tracking_data['courier_id'])) { |
| 154 |
return $tracking_data; |
| 155 |
} |
| 156 |
} |
| 157 |
return false; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|