| 1 |
<?php |
| 2 |
|
| 3 |
class Routeapp_Jetpack extends Routeapp_WooCommerce_Common_Tracking_Provider implements Routeapp_WooCommerce_Tracking_Provider { |
| 4 |
|
| 5 |
private $routeapp_api_client; |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
} |
| 9 |
|
| 10 |
public function is_active() { |
| 11 |
return in_array( 'jetpack/jetpack.php', (array) get_option( 'active_plugins', array() )); |
| 12 |
} |
| 13 |
|
| 14 |
public static function get_route_public_instance(){ |
| 15 |
global $routeapp_public; |
| 16 |
return $routeapp_public; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_shipping_provider_name($order_id) |
| 20 |
{ |
| 21 |
$couriers_array = array(); |
| 22 |
$tracking_items = get_post_meta( $order_id, 'wc_connect_labels', true ); |
| 23 |
foreach ( $tracking_items as $tracking_item ) { |
| 24 |
$couriers_array[$tracking_item['tracking']] = strtolower($tracking_item['carrier_id']); |
| 25 |
} |
| 26 |
return $couriers_array; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_shipping_info($order_id) |
| 30 |
{ |
| 31 |
$shipping_info = []; |
| 32 |
$tracking_data = get_post_meta( $order_id, 'wc_connect_labels', true ); |
| 33 |
if (!$tracking_data) return false; |
| 34 |
$product_ids = $this->get_order_products($order_id); |
| 35 |
foreach ($tracking_data as $track_item) { |
| 36 |
if ($track_item['tracking']!='' && $track_item['status'] == 'PURCHASED') { |
| 37 |
$shipping_info[] = array( |
| 38 |
'source_order_id' => $order_id, |
| 39 |
'source_product_ids' => $product_ids, |
| 40 |
'courier_id' => $track_item['carrier_id'], |
| 41 |
'tracking_number' => $track_item['tracking'] |
| 42 |
); |
| 43 |
} |
| 44 |
} |
| 45 |
return $shipping_info; |
| 46 |
} |
| 47 |
|
| 48 |
public function update($order_id, $routeapp) { |
| 49 |
$tracking_data = get_post_meta( $order_id, 'wc_connect_labels', true ); |
| 50 |
if (!$tracking_data) return; |
| 51 |
|
| 52 |
$order = wc_get_order( $order_id ); |
| 53 |
$product_ids = $this->get_order_products($order_id); |
| 54 |
|
| 55 |
$tracking_numbers = array(); |
| 56 |
foreach ($tracking_data as $track_item) { |
| 57 |
//skip this function until the processing is done on credit card side |
| 58 |
if ($track_item['status'] == 'PURCHASE_IN_PROGRESS') return false; |
| 59 |
if ($track_item['tracking']!='' && $track_item['status'] == 'PURCHASED') { |
| 60 |
$track_data = array( |
| 61 |
'tracking_number' => $track_item['tracking'], |
| 62 |
'courier_id' => $track_item['carrier_id'], |
| 63 |
); |
| 64 |
$tracking_numbers[] = $track_data; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$existing_tracking_numbers = (array) explode( self::SEPARATOR_PIPE, |
| 69 |
get_post_meta($order_id, 'routeapp_shipment_tracking_number', true )); |
| 70 |
|
| 71 |
if (count($tracking_numbers)>0) { |
| 72 |
$tracking_numbers_array = array(); |
| 73 |
foreach ($tracking_numbers as $tracking_number){ |
| 74 |
$tracking_numbers_array[] = $tracking_number['tracking_number']; |
| 75 |
if (in_array($tracking_number, $existing_tracking_numbers)) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$shipmentResponse = $routeapp->routeapp_api_client->get_shipment($tracking_number['tracking_number'], $order_id); |
| 80 |
|
| 81 |
if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
$params = array( |
| 86 |
'source_order_id' => $order_id, |
| 87 |
'source_product_ids' => $product_ids, |
| 88 |
'courier_id' => $tracking_number['courier_id'] |
| 89 |
); |
| 90 |
|
| 91 |
$extraData = array(); |
| 92 |
$response = $routeapp->routeapp_api_client->create_shipment($tracking_number['tracking_number'], $params); |
| 93 |
$extraData['endpoint'] = 'shipments'; |
| 94 |
|
| 95 |
try{ |
| 96 |
if ( is_wp_error( $response ) ) { |
| 97 |
throw new Exception($response->get_error_message()); |
| 98 |
} |
| 99 |
} catch (Exception $exception) { |
| 100 |
$routeapp_public = self::get_route_public_instance(); |
| 101 |
$params['tracking_id'] = $tracking_number['tracking_number']; |
| 102 |
$extraData = array( |
| 103 |
'params' => $params, |
| 104 |
'method' => 'POST' |
| 105 |
); |
| 106 |
$routeapp_public->routeapp_log($exception, $extraData); |
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
if ($existing_tracking_numbers[0]!='') { |
| 111 |
foreach ($existing_tracking_numbers as $existing_tracking_number) { |
| 112 |
if (!in_array($existing_tracking_number, $tracking_numbers_array)) { |
| 113 |
$routeapp->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array); |
| 119 |
$this->add_custom_post_meta($order_id, $tracking_numbers); |
| 120 |
}else { |
| 121 |
/* If user removed the Tracking Code and updated to a new one */ |
| 122 |
if ($existing_tracking_numbers[0]!='') { |
| 123 |
foreach ($existing_tracking_numbers as $existing_tracking_number) { |
| 124 |
$routeapp->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function parse_order_notes($order_id, $route_app) |
| 131 |
{ |
| 132 |
return; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|