| 1 |
<?php |
| 2 |
|
| 3 |
class Routeapp_Woo_Orders_Tracking 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 |
|
| 11 |
public function is_active() { |
| 12 |
return in_array( 'woo-orders-tracking/woo-orders-tracking.php', (array) get_option( 'active_plugins', array() )); |
| 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 = array(); |
| 23 |
$order = wc_get_order( $order_id ); |
| 24 |
//get all existing trackings for the items inside this order for this plugin |
| 25 |
foreach ( $order->get_items() as $item_id => $item ) { |
| 26 |
$existing_trackings = json_decode(wc_get_order_item_meta($item_id, '_vi_wot_order_item_tracking_data', true)); |
| 27 |
if (is_array($existing_trackings)) { |
| 28 |
$existing_tracking = end($existing_trackings); |
| 29 |
if (isset( $existing_tracking->tracking_number)) { |
| 30 |
$courier_id = strtolower(str_replace(' ', '-', $existing_tracking->carrier_name)); |
| 31 |
$couriers_array[$existing_tracking->tracking_number] = $courier_id; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
return $couriers_array; |
| 36 |
return $this->get_meta_data( $order_id, 'routeapp_shipment_tracking_provider'); |
| 37 |
} |
| 38 |
|
| 39 |
public function get_shipping_info($order_id) |
| 40 |
{ |
| 41 |
$shipping_info = []; |
| 42 |
$order = wc_get_order( $order_id ); |
| 43 |
//get all existing trackings for the items inside this order for this plugin |
| 44 |
foreach ( $order->get_items() as $item_id => $item ) { |
| 45 |
$existing_trackings = json_decode(wc_get_order_item_meta($item_id, '_vi_wot_order_item_tracking_data', true)); |
| 46 |
if (is_array($existing_trackings)) { |
| 47 |
$existing_tracking = end($existing_trackings); |
| 48 |
if (isset( $existing_tracking->tracking_number)) { |
| 49 |
$courier_id = strtolower(str_replace(' ', '-', $existing_tracking->carrier_name)); |
| 50 |
$shipping_info[] = array( |
| 51 |
'source_order_id' => $order_id, |
| 52 |
'source_product_ids' => array($item->get_product_id()), |
| 53 |
'courier_id' => $courier_id, |
| 54 |
'tracking_number' => $existing_tracking->tracking_number |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
return $shipping_info; |
| 60 |
} |
| 61 |
|
| 62 |
public function update($order_item_id, $routeapp) { |
| 63 |
if (!isset($_POST['tracking_code']) || !isset($_POST['carrier_name'])) return; |
| 64 |
|
| 65 |
if (wc_get_order( $order_item_id )) return; |
| 66 |
|
| 67 |
$tracking_numbers = array(); |
| 68 |
$tracking_number = $_POST['tracking_code']; |
| 69 |
$tracking_numbers[] = $tracking_number; |
| 70 |
$courier_id = strtolower(str_replace(' ', '-', $_POST['carrier_name'])); |
| 71 |
$item = new WC_Order_Item_Product($order_item_id); |
| 72 |
$order = wc_get_order( $item->get_order_id() ); |
| 73 |
|
| 74 |
$bulk_set_tracking_numbers = isset($_POST['action']) && $_POST['action'] == 'wotv_save_track_info_all_item'; |
| 75 |
$parent_id = $bulk_set_tracking_numbers ? $order->get_id() : $order_item_id; |
| 76 |
|
| 77 |
$product_ids = array(); |
| 78 |
if ($bulk_set_tracking_numbers) { |
| 79 |
$product_ids = $this->get_order_products($parent_id); |
| 80 |
} else { |
| 81 |
array_push($product_ids, $item->get_product()->get_id()); |
| 82 |
} |
| 83 |
|
| 84 |
$params = array( |
| 85 |
'source_order_id' => $order->get_id(), |
| 86 |
'source_product_ids' => $product_ids, |
| 87 |
'courier_id' => $courier_id, |
| 88 |
); |
| 89 |
|
| 90 |
//get all existing trackings for the items inside this order for this plugin |
| 91 |
foreach ( $order->get_items() as $item_id => $item ) { |
| 92 |
$existing_trackings = json_decode(wc_get_order_item_meta($item_id, '_vi_wot_order_item_tracking_data', true)); |
| 93 |
if (is_array($existing_trackings)) { |
| 94 |
$existing_tracking = end($existing_trackings); |
| 95 |
if (isset( $existing_tracking->tracking_number)) { |
| 96 |
$tracking_numbers[] = $existing_tracking->tracking_number; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
//get tracking numbers that we have on our plugin |
| 102 |
$existing_tracking_numbers = explode(self::SEPARATOR_PIPE, |
| 103 |
$this->get_meta_data( $order->get_id(), 'routeapp_shipment_tracking_number')); |
| 104 |
|
| 105 |
if (count($tracking_numbers)>0) { |
| 106 |
$tracking_numbers = array_unique($tracking_numbers); |
| 107 |
$tracking_numbers_array = array(); |
| 108 |
foreach ($tracking_numbers as $tracking_number){ |
| 109 |
$tracking_numbers_array[] = $tracking_number; |
| 110 |
if (in_array($tracking_number, $existing_tracking_numbers)) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
$shipmentResponse = $routeapp->routeapp_api_client->get_shipment($tracking_number, $order->get_id()); |
| 114 |
|
| 115 |
if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
$extraData = array(); |
| 119 |
$response = $routeapp->routeapp_api_client->create_shipment($tracking_number, $params); |
| 120 |
$extraData['endpoint'] = 'shipments'; |
| 121 |
|
| 122 |
try{ |
| 123 |
if ( is_wp_error( $response ) ) { |
| 124 |
throw new Exception($response->get_error_message()); |
| 125 |
} |
| 126 |
} catch (Exception $exception) { |
| 127 |
$routeapp_public = self::get_route_public_instance(); |
| 128 |
$params['tracking_id'] = $tracking_number; |
| 129 |
$extraData = array( |
| 130 |
'params' => $params, |
| 131 |
'method' => 'POST' |
| 132 |
); |
| 133 |
$routeapp_public->routeapp_log($exception, $extraData); |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
if ($existing_tracking_numbers[0]!='' && !empty($tracking_numbers_array)) { |
| 138 |
foreach ($existing_tracking_numbers as $existing_tracking_number) { |
| 139 |
if (!in_array($existing_tracking_number, $tracking_numbers_array)) { |
| 140 |
$routeapp->routeapp_cancel_tracking_order( $order->get_id(), $existing_tracking_number, $product_ids ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
$tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array); |
| 145 |
|
| 146 |
if ($tracking_numbers!='') { |
| 147 |
$this->add_custom_post_meta($order->get_id(), $tracking_numbers); |
| 148 |
} |
| 149 |
}else { |
| 150 |
/* If user removed the Tracking Code and updated to a new one */ |
| 151 |
if ($existing_tracking_numbers[0]!='') { |
| 152 |
foreach ($existing_tracking_numbers as $existing_tracking_number) { |
| 153 |
$routeapp->routeapp_cancel_tracking_order( $order->get_id(), $existing_tracking_number, $product_ids ); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public function parse_order_notes($order_id, $route_app) |
| 160 |
{ |
| 161 |
return; |
| 162 |
} |
| 163 |
} |
| 164 |
|