| 1 |
<?php |
| 2 |
|
| 3 |
class Routeapp_Mimo_Shipment_Tracking extends Routeapp_WooCommerce_Common_Tracking_Provider implements Routeapp_WooCommerce_Tracking_Provider { |
| 4 |
|
| 5 |
private $routeapp_api_client; |
| 6 |
|
| 7 |
private $provider_list; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
$this->_get_provider_list(); |
| 11 |
} |
| 12 |
|
| 13 |
public function is_active() { |
| 14 |
return in_array( 'mimo-woocommerce-order-tracking/mimo-woocommerce-order-tracking.php', (array) get_option( 'active_plugins', array() )); |
| 15 |
} |
| 16 |
|
| 17 |
public static function get_route_public_instance(){ |
| 18 |
global $routeapp_public; |
| 19 |
return $routeapp_public; |
| 20 |
} |
| 21 |
|
| 22 |
private function _get_provider_list() { |
| 23 |
$this->provider_list = get_option( 'mimo_provider_list' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_shipping_provider_name($order_id) |
| 27 |
{ |
| 28 |
$courier_id = ''; |
| 29 |
$tracking_provider_id = $this->get_meta_data( $order_id , 'mimo_tracking_provider_id'); |
| 30 |
foreach ($this->provider_list as $provider) { |
| 31 |
if ($tracking_provider_id==$provider['id']) { |
| 32 |
$courier_id = str_replace(' ', '-', strtolower($provider['name'])); |
| 33 |
} |
| 34 |
} |
| 35 |
return $courier_id; |
| 36 |
} |
| 37 |
|
| 38 |
public function get_shipping_info($order_id) |
| 39 |
{ |
| 40 |
$tracking_number = $this->get_meta_data( $order_id, 'mimo_tracking_number' ); |
| 41 |
if (!$tracking_number) return false; |
| 42 |
$courier_id = $this->get_shipping_provider_name($order_id); |
| 43 |
$product_ids = $this->get_order_products($order_id); |
| 44 |
return array( |
| 45 |
'source_order_id' => $order_id, |
| 46 |
'source_product_ids' => $product_ids, |
| 47 |
'courier_id' => $courier_id, |
| 48 |
'tracking_number' => $tracking_number |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function update($order_id, $routeapp) { |
| 53 |
$tracking_number = $this->get_meta_data( $order_id, 'mimo_tracking_number'); |
| 54 |
if (!$tracking_number) return; |
| 55 |
|
| 56 |
$tracking_provider_id = $this->get_meta_data( $order_id , 'mimo_tracking_provider_id'); |
| 57 |
$tracking_provider_name = ''; |
| 58 |
foreach ($this->provider_list as $provider) { |
| 59 |
if ($tracking_provider_id==$provider['id']) { |
| 60 |
$tracking_provider_name = str_replace(' ', '-', strtolower($provider['name'])); |
| 61 |
} |
| 62 |
} |
| 63 |
$product_ids = $this->get_order_products($order_id); |
| 64 |
$route_tracking_number = $this->get_meta_data($order_id, 'routeapp_shipment_tracking_number'); |
| 65 |
|
| 66 |
if (!empty($route_tracking_number) && $tracking_number !== $route_tracking_number) { |
| 67 |
$routeapp->routeapp_cancel_tracking_order( $order_id, $route_tracking_number, $product_ids ); |
| 68 |
} |
| 69 |
|
| 70 |
$shipmentResponse = $routeapp->routeapp_api_client->get_shipment($tracking_number, $order_id); |
| 71 |
|
| 72 |
if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$courier_id = $tracking_provider_name; |
| 77 |
$params = array( |
| 78 |
'source_order_id' => $order_id, |
| 79 |
'source_product_ids' => $product_ids, |
| 80 |
'courier_id' => $courier_id |
| 81 |
); |
| 82 |
|
| 83 |
$extraData = array(); |
| 84 |
$response = $routeapp->routeapp_api_client->create_shipment($tracking_number, $params); |
| 85 |
$extraData['endpoint'] = 'shipments'; |
| 86 |
|
| 87 |
try{ |
| 88 |
if ( is_wp_error( $response ) ) { |
| 89 |
throw new Exception($response->get_error_message()); |
| 90 |
} |
| 91 |
} catch (Exception $exception) { |
| 92 |
$routeapp_public = self::get_route_public_instance(); |
| 93 |
$params['tracking_id'] = $tracking_number; |
| 94 |
$extraData = array( |
| 95 |
'params' => $params, |
| 96 |
'method' => 'POST' |
| 97 |
); |
| 98 |
$routeapp_public->routeapp_log($exception, $extraData); |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
$this->add_custom_post_meta($order_id, $tracking_number, $courier_id); |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
public function parse_order_notes($order_id, $route_app) |
| 108 |
{ |
| 109 |
return; |
| 110 |
} |
| 111 |
} |
| 112 |
|