PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.7.5
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.7.5
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / apis / class-api-for-shipping-status.php
commercebird / includes / classes / apis Last commit date
class-api-for-cmbird.php 7 months ago class-api-for-exact-webhooks.php 5 months ago class-api-for-product-webhook.php 5 months ago class-api-for-shipping-status.php 9 months ago class-api-for-woo-order.php 4 months ago class-api-for-zoho-inventory.php 9 months ago class-commercebird-list-items-api-controller.php 10 months ago class-commercebird-media-api-controller.php 10 months ago class-commercebird-metadata-controller.php 5 months ago index.php 1 year ago trait-api-permission.php 7 months ago
class-api-for-shipping-status.php
149 lines
1 <?php
2
3 namespace CommerceBird\API;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use CMBIRD_API_Handler_Zoho;
10 use WP_REST_Response;
11 use WP_REST_Server;
12
13 class ShippingWebhook {
14 use Api;
15
16 private static string $endpoint = 'zoho-shipping-status';
17
18
19 public function __construct() {
20 register_rest_route(
21 self::$namespace,
22 self::$endpoint,
23 array(
24 'methods' => WP_REST_Server::CREATABLE,
25 'callback' => array( $this, 'handle' ),
26 'permission_callback' => array( $this, 'permission_check' ),
27 )
28 );
29 }
30
31
32 /**
33 * Process the given order data and update the WooCommerce order
34 * with the provided tracking information and shipped status.
35 *
36 * @param array $order_data The order data to process.
37 *
38 * @return WP_REST_Response The response object containing the result of the operation.
39 */
40 private function process( array $order_data ): WP_REST_Response {
41 $response = new WP_REST_Response();
42 $response->set_data( $this->empty_response );
43 $response->set_status( 400 );
44 if ( ! empty( $order_data['salesorder'] ) ) {
45 $salesorder = $order_data['salesorder'];
46 /* Getting Salesorder id */
47
48 $salesorder_id = $salesorder['salesorder_id'];
49 $formatted_status = trim( $salesorder['shipped_status_formatted'] );
50 $ship_status = strtolower( $formatted_status );
51 $packages = $salesorder['packages'];
52
53 /* Customer Query to get Order Id */
54 $order_statuses = wc_get_order_statuses();
55 // Find orders with the specific meta key and value.
56 $orders = wc_get_orders(
57 array(
58 'meta_key' => 'zi_salesorder_id',
59 'meta_value' => $salesorder_id,
60 'limit' => 1,
61 'return' => 'ids',
62 )
63 );
64 // Loop through the found orders.
65 foreach ( $orders as $orders_id ) {
66 $post_id = $orders_id;
67 }
68 // Get Order Object.
69 $order = wc_get_order( $post_id );
70 // process cancelled zoho orders.
71 if ( 'void' === trim( $salesorder['status'] ) ) {
72 $order->update_status( 'cancelled', 'Order Cancelled from Zoho Inventory' );
73 $order->save();
74 $response->set_data( 'Cancelled Order processed' );
75 $response->set_status( 200 );
76 return $response;
77 }
78 // If status is closed then change woo order to completed.
79 if ( 'closed' === trim( $salesorder['status'] ) ) {
80 $order->update_status( 'completed', 'Order Completed from Zoho Inventory' );
81 $order->save();
82 $response->set_data( 'Completed Order processed' );
83 $response->set_status( 200 );
84 return $response;
85 }
86
87 /* Getting Packages if empty in response */
88 if ( empty( $packages ) && ! empty( $post_id ) ) {
89 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
90 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
91 $package_url = $zoho_inventory_url . 'inventory/v1/packages?organization_id=' . $zoho_inventory_oid;
92 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
93 $json = $execute_curl_call_handle->execute_curl_call_get( $package_url );
94 if ( 0 === (int) $json->code ) {
95 $all_packages = $json->packages;
96 foreach ( $all_packages as $packs ) {
97 $order_id = $packs->salesorder_id;
98 if ( trim( $order_id ) === trim( $salesorder_id ) ) {
99 $tracking_number = $packs->tracking_number;
100 if ( empty( $ship_status ) ) {
101 $ship_status = trim( $packs->status );
102 }
103 $order->update_meta_data( 'zi_tracking_number', $tracking_number );
104 }
105 }
106 }
107 } elseif ( ! empty( $packages ) && ! empty( $post_id ) ) {
108 foreach ( $packages as $package ) {
109 /* getting all ship and trace data from package */
110 $tracking_number = $package['tracking_number'];
111 $carrier = $package['carrier'];
112 $status = $package['status'];
113 if ( empty( $ship_status ) ) {
114 $ship_status = trim( $status );
115 }
116 $order->update_meta_data( 'zi_tracking_number', $tracking_number );
117 $order->update_meta_data( 'zi_shipping_carrier', $carrier );
118 }
119 } else {
120 $error = 'Post id not available for this ' . $salesorder_id . ' sales order';
121 $response->set_data( $error );
122 $response->set_status( 400 );
123
124 return $response;
125
126 }
127
128 // process shipped status.
129 if ( ! empty( $ship_status ) && $post_id ) {
130 $order_statuses = array_map( 'strtolower', $order_statuses );
131 if ( in_array( $ship_status, $order_statuses ) ) {
132 $ship_status = remove_accents( $ship_status );
133 $order->update_meta_data( 'zi_shipping_status', $ship_status );
134 $order->update_status( $ship_status );
135 } else {
136 $order->update_meta_data( 'zi_shipping_status', $ship_status );
137 }
138 }
139 $order->save();
140 $response->set_data( 'Success add tracking id and update shipped status' );
141 $response->set_status( 200 );
142
143 return $response;
144 }
145
146 return $response;
147 }
148 }
149