| 1 |
<?php |
| 2 |
|
| 3 |
use Automattic\WooCommerce\Utilities\OrderUtil; |
| 4 |
class Routeapp_WooCommerce_Common_Tracking_Provider { |
| 5 |
|
| 6 |
public function get_order_products($order_id) |
| 7 |
{ |
| 8 |
$order = wc_get_order( $order_id ); |
| 9 |
|
| 10 |
if ( ! $order ) { |
| 11 |
return array(); |
| 12 |
} |
| 13 |
|
| 14 |
return self::get_order_products_from_order( $order ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Resolve source_product_id the same way order-create does via webhook: |
| 19 |
* prefer _routeapp_source_product_id meta, else parent product_id. |
| 20 |
* (woocommerce-service falls back to product_id when meta is missing.) |
| 21 |
* |
| 22 |
* @param WC_Order_Item_Product $order_item |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public static function resolve_source_product_id( $order_item ) |
| 26 |
{ |
| 27 |
$from_meta = $order_item->get_meta( '_routeapp_source_product_id', true ); |
| 28 |
|
| 29 |
// WooCommerce returns an empty string for both missing and explicitly empty |
| 30 |
// single-value meta; treat either case as unset and fall back to product_id. |
| 31 |
if ( $from_meta !== '' && $from_meta !== null && $from_meta !== false ) { |
| 32 |
return (string) $from_meta; |
| 33 |
} |
| 34 |
|
| 35 |
return (string) $order_item->get_product_id(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Collect source product IDs for shipment payloads. |
| 40 |
* Uses the same ID resolution as order create so Route accepts the shipment. |
| 41 |
* |
| 42 |
* @param WC_Order $order |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public static function get_order_products_from_order( $order ) |
| 46 |
{ |
| 47 |
$product_ids = array(); |
| 48 |
|
| 49 |
foreach ( $order->get_items() as $order_item ) { |
| 50 |
$source_product_id = self::resolve_source_product_id( $order_item ); |
| 51 |
if ( $source_product_id === '' || $source_product_id === '0' ) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
$quantity = (int) $order_item->get_quantity(); |
| 56 |
if ( $quantity > 0 ) { |
| 57 |
$product_ids = array_merge( $product_ids, array_fill( 0, $quantity, $source_product_id ) ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return $product_ids; |
| 62 |
} |
| 63 |
|
| 64 |
public function remove_custom_post_meta($order_id) |
| 65 |
{ |
| 66 |
delete_post_meta( $order_id, 'routeapp_shipment_api_called' ); |
| 67 |
delete_post_meta( $order_id, 'routeapp_shipment_tracking_number' ); |
| 68 |
delete_post_meta( $order_id, 'routeapp_shipment_tracking_provider' ); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
public function add_custom_post_meta($order_id, $tracking_number, $courier_id = false) |
| 73 |
{ |
| 74 |
//validate if the tracking number already exists and ignore the update |
| 75 |
$existing_tracking_numbers = $this->get_meta_data( $order_id, 'routeapp_shipment_tracking_number' ); |
| 76 |
if ( $existing_tracking_numbers === $tracking_number ) { |
| 77 |
return true; |
| 78 |
} |
| 79 |
self::update_post_meta_value($order_id, 'routeapp_shipment_api_called', 'success'); |
| 80 |
self::update_post_meta_value( $order_id, 'routeapp_shipment_tracking_number', $tracking_number ); |
| 81 |
if ($courier_id) { |
| 82 |
self::update_post_meta_value($order_id, 'routeapp_shipment_tracking_provider', $courier_id); |
| 83 |
} |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Update post meta value |
| 89 |
* |
| 90 |
* @param $order_id |
| 91 |
* @param $post_meta |
| 92 |
* @param $value |
| 93 |
*/ |
| 94 |
public static function update_post_meta_value( $order_id, $post_meta, $value) { |
| 95 |
|
| 96 |
if ( class_exists('Automattic\WooCommerce\Utilities\OrderUtil') |
| 97 |
&& OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 98 |
$order = wc_get_order($order_id); |
| 99 |
$order->update_meta_data($post_meta, $value ); |
| 100 |
$order->save(); |
| 101 |
} else { |
| 102 |
// Traditional CPT-based orders are in use. |
| 103 |
update_post_meta( $order_id, $post_meta, $value ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* get meta data if custom order table is enabled |
| 109 |
* @param $order_id |
| 110 |
* @param $post_meta |
| 111 |
* @return array|mixed|string |
| 112 |
*/ |
| 113 |
public static function get_meta_data( $order_id, $post_meta ) { |
| 114 |
if ( class_exists('Automattic\WooCommerce\Utilities\OrderUtil') |
| 115 |
&& OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 116 |
$order = wc_get_order($order_id); |
| 117 |
return $order->get_meta($post_meta); |
| 118 |
} else { |
| 119 |
// Traditional CPT-based orders are in use. |
| 120 |
return get_post_meta($order_id, $post_meta, true); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function cancel($order_id, $tracking_number, $product_ids, $routeapp) { |
| 125 |
if ( is_null($order_id) || is_null($tracking_number) ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
if (!empty($tracking_number)) { |
| 129 |
$shipmentResponse = $routeapp->routeapp_api_client->get_shipment($tracking_number, $order_id); |
| 130 |
if (!is_wp_error($shipmentResponse) && isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200) { |
| 131 |
$response = $routeapp->routeapp_api_client->cancel_shipment($tracking_number, array( |
| 132 |
'source_order_id' => $order_id, |
| 133 |
'source_product_ids' => $product_ids |
| 134 |
)); |
| 135 |
if ( is_wp_error( $response ) ) { |
| 136 |
return false; |
| 137 |
} else { |
| 138 |
//$this->remove_custom_post_meta($order_id); |
| 139 |
if($response['response']['code'] === 400) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
return true; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
|