| 1 |
<?php |
| 2 |
|
| 3 |
class Routeapp_Woocommerce_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( 'woocommerce-orders-tracking/woocommerce-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_data'])) return; |
| 64 |
|
| 65 |
$tracking_data_list = $_POST['tracking_data']; |
| 66 |
$order = wc_get_order($order_item_id); |
| 67 |
if (!$order) return; |
| 68 |
|
| 69 |
$tracking_numbers = []; |
| 70 |
$grouped_tracking_data = []; |
| 71 |
|
| 72 |
foreach ($tracking_data_list as $tracking_data) { |
| 73 |
$tracking_number = $tracking_data['tracking_number'] ?? ''; |
| 74 |
$carrier_slug = $tracking_data['carrier_slug'] ?? ''; |
| 75 |
$item_id = $tracking_data['item_id'] ?? ''; |
| 76 |
|
| 77 |
if (!$tracking_number || !$carrier_slug || !$item_id) continue; |
| 78 |
|
| 79 |
$courier_id = strtolower(str_replace(' ', '-', $carrier_slug)); |
| 80 |
$item = new WC_Order_Item_Product($item_id); |
| 81 |
$product_id = $item->get_product()->get_id(); |
| 82 |
|
| 83 |
$group_key = $courier_id . '|' . $tracking_number; |
| 84 |
|
| 85 |
if (!isset($grouped_tracking_data[$group_key])) { |
| 86 |
$grouped_tracking_data[$group_key] = [ |
| 87 |
'courier_id' => $courier_id, |
| 88 |
'tracking_number' => $tracking_number, |
| 89 |
'product_ids' => [], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
$grouped_tracking_data[$group_key]['product_ids'][] = $product_id; |
| 94 |
} |
| 95 |
|
| 96 |
foreach ($grouped_tracking_data as $group) { |
| 97 |
$tracking_number = $group['tracking_number']; |
| 98 |
$courier_id = $group['courier_id']; |
| 99 |
$product_ids = array_unique($group['product_ids']); |
| 100 |
|
| 101 |
$params = [ |
| 102 |
'source_order_id' => $order->get_id(), |
| 103 |
'source_product_ids' => $product_ids, |
| 104 |
'courier_id' => $courier_id, |
| 105 |
]; |
| 106 |
|
| 107 |
$tracking_numbers[] = $tracking_number; |
| 108 |
|
| 109 |
$existing_tracking_numbers = explode(self::SEPARATOR_PIPE, |
| 110 |
$this->get_meta_data($order->get_id(), 'routeapp_shipment_tracking_number')); |
| 111 |
|
| 112 |
if (in_array($tracking_number, $existing_tracking_numbers)) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$shipmentResponse = $routeapp->routeapp_api_client->get_shipment($tracking_number, $order->get_id()); |
| 117 |
if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
$response = $routeapp->routeapp_api_client->create_shipment($tracking_number, $params); |
| 122 |
|
| 123 |
try { |
| 124 |
if (is_wp_error($response)) { |
| 125 |
throw new Exception($response->get_error_message()); |
| 126 |
} |
| 127 |
} catch (Exception $exception) { |
| 128 |
$routeapp_public = self::get_route_public_instance(); |
| 129 |
$params['tracking_id'] = $tracking_number; |
| 130 |
$routeapp_public->routeapp_log($exception, [ |
| 131 |
'params' => $params, |
| 132 |
'method' => 'POST' |
| 133 |
]); |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$tracking_numbers = implode(self::SEPARATOR_PIPE, array_unique($tracking_numbers)); |
| 140 |
if ($tracking_numbers !== '') { |
| 141 |
$this->add_custom_post_meta($order->get_id(), $tracking_numbers); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public function parse_order_notes($order_id, $route_app) |
| 146 |
{ |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
|