PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.7.0
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.7.0
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-woo-order.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 7 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-woo-order.php
258 lines
1 <?php
2
3 namespace CommerceBird\API;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use CommerceBird\Admin\Traits\LogWriter;
10 use WP_REST_Response;
11
12 class CreateOrderWebhook {
13
14 use Api;
15 use LogWriter;
16
17 private static string $endpoint = 'create-woo-order';
18
19
20 public function __construct() {
21 register_rest_route(
22 self::$namespace,
23 self::$endpoint,
24 array(
25 'methods' => 'POST',
26 'callback' => array( $this, 'handle' ),
27 'permission_callback' => array( $this, 'permission_check' ),
28 )
29 );
30 }
31
32 /**
33 * @param $address
34 *
35 * @return array
36 */
37 public function format_address( $address ): array {
38 if ( array_key_exists( 'attention', $address ) ) {
39 $names = explode( ' ', $address['attention'] );
40 $first_name = $names[0] ?? '';
41 $last_name = $names[1] ?? '';
42 }
43 return array(
44 'first_name' => $first_name ?? '',
45 'last_name' => $last_name ?? '',
46 'address_1' => $address['address'] ?? '',
47 'address_2' => $address['street2'] ?? '',
48 'city' => $address['city'] ?? '',
49 'state' => $address['state_code'] ?? '',
50 'postcode' => $address['zip'] ?? '',
51 'country' => $address['country_code'] ?? '',
52 'phone' => $address['phone'] ?? '',
53 );
54 }
55
56 private function process( array $order_data ): WP_REST_Response {
57 $response = new WP_REST_Response();
58
59 if ( empty( $order_data ) || empty( $order_data['salesorder'] ) ) {
60 $response->set_data( $this->empty_response );
61 $response->set_status( 404 );
62 return $response;
63 }
64
65 $allowed_keys = array(
66 'salesorder_id',
67 'salesorder_number',
68 'customer_id',
69 'billing_address',
70 'shipping_address',
71 'delivery_method',
72 'currency_code',
73 'line_items',
74 'contact_person_details',
75 'shipping_charges',
76 'order_status',
77 'paid_status',
78 'discount',
79 'discount_total',
80 'notes',
81 );
82
83 $order_data = array_intersect_key( $order_data['salesorder'], array_flip( $allowed_keys ) );
84 $billing_address = $this->format_address( $order_data['billing_address'] );
85 $shipping_address = $this->format_address( $order_data['shipping_address'] );
86 if ( isset( $order_data['contact_person_details'][0]['email'] ) ) {
87 $customer_data = $order_data['contact_person_details'][0];
88 $customer_mail = $customer_data['email'];
89 $customer = get_user_by( 'email', $customer_mail );
90 if ( empty( $customer ) ) {
91 $customer_id = wc_create_new_customer( $customer_mail );
92 $customer = get_user_by( 'id', $customer_id );
93 }
94 }
95
96 if ( empty( $order_data['line_items'] ) ) {
97 // translators: 1: order number, 2: Store name.
98 $message = sprintf( __( 'Zoho order # %1$s could not be created in your store %2$s because of missing line items.', 'commercebird' ), $order_data['salesorder_number'], get_bloginfo( 'name' ) );
99 $common_class = new \CMBIRD_Common_Functions();
100 $common_class->send_email( __( 'Zoho Order Sync', 'commercebird' ), $message );
101 $response->set_status( 500 );
102 $response->set_data( $message );
103 return $response;
104 }
105
106 $line_items = $this->get_items( $order_data['line_items'] );
107
108 if ( ! empty( $line_items['not_found'] ) ) {
109 // translators: 1: order number, 2: Store name, 3: missing items.
110 $message = sprintf( __( 'Zoho order # %1$s could not be created in your store %2$s because of missing items: %3$s', 'commercebird' ), $order_data['salesorder_number'], get_bloginfo( 'name' ), implode( ', ', $line_items['not_found'] ) );
111 $common_class = new \CMBIRD_Common_Functions();
112 $common_class->send_email( __( 'Zoho Order Sync', 'commercebird' ), $message );
113 $response->set_status( 500 );
114 $response->set_data( $message );
115 return $response;
116 }
117 // create shipping object.
118 $shipping = new \WC_Order_Item_Shipping();
119 $shipping->set_method_title( $order_data['delivery_method'] );
120 $shipping->set_total( $order_data['shipping_charges']['item_total'] );
121 $id = $this->get_order_id( $order_data['salesorder_id'] );
122 $existing_order = wc_get_order( $id );
123 if ( ! empty( $existing_order ) ) {
124 $existing_order->set_address( $shipping_address, 'shipping' );
125
126 // Get existing order items.
127 $order_items = array_column( $order_data['line_items'], 'quantity', 'sku' );
128 $existing_order->remove_order_items( 'line_item' );
129 foreach ( $order_items as $sku => $quantity ) {
130 $product_id = wc_get_product_id_by_sku( $sku );
131 if ( empty( $product_id ) ) {
132 continue;
133 }
134 $product = wc_get_product( $product_id );
135 if ( empty( $product_id ) ) {
136 continue;
137 }
138 $existing_order->add_product( $product, $quantity );
139 }
140 // Save the changes to the order.
141 $existing_order->set_customer_note( isset( $order_data['notes'] ) ? $order_data['notes'] : '' );
142 $existing_order->update_status( $this->map_status( $order_data['paid_status'] ) );
143 $existing_order->calculate_totals();
144 $existing_order->save();
145 wc_delete_shop_order_transients( $existing_order );
146 $response->set_data(
147 array(
148 'id' => $existing_order->get_id(),
149 'items' => array_map(
150 function ( $item ) {
151 return array(
152 'product_id' => $item->get_product_id(),
153 'quantity' => $item->get_quantity(),
154 'price' => $item->get_total(),
155 );
156 },
157 $existing_order->get_items()
158 ),
159 'total' => $existing_order->get_total(),
160 )
161 );
162 } else {
163 $order = wc_create_order();
164 if ( $customer ) {
165 $order->set_customer_id( $customer->ID );
166 }
167 foreach ( $order_data['line_items'] as $order_data_item ) {
168 $product_id = wc_get_product_id_by_sku( $order_data_item['sku'] );
169 if ( empty( $product_id ) ) {
170 continue;
171 }
172 $product = wc_get_product( $product_id );
173 if ( empty( $product_id ) ) {
174 continue;
175 }
176 $order->add_product( $product, $order_data_item['quantity'] );
177 }
178 $order->set_address( $billing_address, 'billing' );
179 $order->set_address( $shipping_address, 'shipping' );
180 $order->set_currency( $order_data['currency_code'] );
181 $order->set_status( $this->map_status( $order_data['paid_status'] ) );
182 $order->set_discount_total( $order_data['discount'] );
183 $order->add_item( $shipping );
184 $order->calculate_totals();
185 $order->set_shipping_tax( $order_data['shipping_charges']['tax_total_fcy'] ?? 0 );
186 $order->set_customer_note( $order_data['notes'] );
187 $order->save();
188 $order->update_meta_data( 'zi_salesorder_id', $order_data['salesorder_id'] );
189 $order->save();
190 $response->set_data(
191 array(
192 'id' => $order->get_id(),
193 'items' => array_map(
194 function ( $item ) {
195 return array(
196 'product_id' => $item->get_product_id(),
197 'quantity' => $item->get_quantity(),
198 'price' => $item->get_total(),
199 );
200 },
201 $order->get_items()
202 ),
203 'total' => $order->get_total(),
204 )
205 );
206 }
207
208 return $response;
209 }
210
211
212 private function map_status( $status ): string {
213 switch ( $status ) {
214 case 'paid':
215 return 'wc-completed';
216 default:
217 return 'wc-pending';
218 }
219 }
220
221
222 private function get_items( $line_items ): array {
223 $meta_ids = array_column( $line_items, 'sku' );
224 $product_ids = array();
225 foreach ( $line_items as $item ) {
226 $product_id = wc_get_product_id_by_sku( $item['sku'] );
227 if ( $product_id ) {
228 $product_ids[] = $product_id;
229 }
230 }
231 if ( count( $product_ids ) !== count( $meta_ids ) ) {
232 return array( 'not_found' => array_diff( $meta_ids, array_keys( $product_ids ) ) );
233 }
234
235 return $product_ids;
236 }
237
238 private function get_order_id( $zi_id ): int {
239 // Define your meta query arguments.
240 $args = array(
241 'meta_query' => array(
242 array(
243 'key' => 'zi_salesorder_id',
244 'value' => $zi_id,
245 'compare' => '=',
246 ),
247 ),
248 );
249
250 // Get orders based on meta query.
251 $orders = wc_get_orders( $args );
252 if ( count( $orders ) > 0 ) {
253 return $orders[0]->get_id();
254 }
255 return 0;
256 }
257 }
258