PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.5.1
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.5.1
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 9 months ago class-api-for-exact-webhooks.php 9 months ago class-api-for-product-webhook.php 9 months ago class-api-for-shipping-status.php 9 months ago class-api-for-woo-order.php 9 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 9 months ago index.php 1 year ago trait-api-permission.php 9 months ago
class-api-for-shipping-status.php
142 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
77 return $response;
78 }
79
80 /* Getting Packages if empty in response */
81 if ( empty( $packages ) && ! empty( $post_id ) ) {
82 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
83 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
84 $package_url = $zoho_inventory_url . 'inventory/v1/packages?organization_id=' . $zoho_inventory_oid;
85 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
86 $json = $execute_curl_call_handle->execute_curl_call_get( $package_url );
87 if ( 0 === (int) $json->code ) {
88 $all_packages = $json->packages;
89 foreach ( $all_packages as $packs ) {
90 $order_id = $packs->salesorder_id;
91 if ( trim( $order_id ) === trim( $salesorder_id ) ) {
92 $tracking_number = $packs->tracking_number;
93 if ( empty( $ship_status ) ) {
94 $ship_status = trim( $packs->status );
95 }
96 $order->update_meta_data( 'zi_tracking_number', $tracking_number );
97 }
98 }
99 }
100 } elseif ( ! empty( $packages ) && ! empty( $post_id ) ) {
101 foreach ( $packages as $package ) {
102 /* getting all ship and trace data from package */
103 $tracking_number = $package['tracking_number'];
104 $carrier = $package['carrier'];
105 $status = $package['status'];
106 if ( empty( $ship_status ) ) {
107 $ship_status = trim( $status );
108 }
109 $order->update_meta_data( 'zi_tracking_number', $tracking_number );
110 $order->update_meta_data( 'zi_shipping_carrier', $carrier );
111 }
112 } else {
113 $error = 'Post id not available for this ' . $salesorder_id . ' sales order';
114 $response->set_data( $error );
115 $response->set_status( 400 );
116
117 return $response;
118
119 }
120
121 // process shipped status.
122 if ( ! empty( $ship_status ) && $post_id ) {
123 $order_statuses = array_map( 'strtolower', $order_statuses );
124 if ( in_array( $ship_status, $order_statuses ) ) {
125 $ship_status = remove_accents( $ship_status );
126 $order->update_meta_data( 'zi_shipping_status', $ship_status );
127 $order->update_status( $ship_status );
128 } else {
129 $order->update_meta_data( 'zi_shipping_status', $ship_status );
130 }
131 }
132 $order->save();
133 $response->set_data( 'Success add tracking id and update shipped status' );
134 $response->set_status( 200 );
135
136 return $response;
137 }
138
139 return $response;
140 }
141 }
142