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 / zoho-inventory / class-order-sync.php
commercebird / includes / classes / zoho-inventory Last commit date
class-cmbird-categories-zi.php 7 months ago class-cmbird-image-zi.php 5 months ago class-import-items.php 4 months ago class-import-price-list.php 9 months ago class-multi-currency.php 7 months ago class-order-sync.php 4 months ago class-product.php 4 months ago class-users-contact.php 4 months ago index.php 1 year ago
class-order-sync.php
1202 lines
1 <?php
2 /**
3 * Class for handling Zoho Inventory order sync related functions.
4 */
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class CMBIRD_Order_Sync_ZI {
10
11 /**
12 * Zoho Inventory organization ID.
13 *
14 * @var string
15 */
16 private $zoho_inventory_oid;
17
18 /**
19 * Zoho Inventory API base URL.
20 *
21 * @var string
22 */
23 private $zoho_inventory_url;
24
25 /**
26 * Zoho Inventory domain code (e.g. 'in' for India).
27 *
28 * @var string
29 */
30 private $zoho_inventory_domain;
31
32 /**
33 * Location ID for Zoho Inventory.
34 *
35 * @var string
36 */
37 private $location_id;
38
39 /**
40 * Parent location ID for Zoho Inventory.
41 *
42 * @var string
43 */
44 private $parent_location_id;
45
46 /**
47 * Whether to enable order status sync.
48 *
49 * @var bool
50 */
51 private $enable_order_status;
52
53 /**
54 * Whether to enable auto-numbering for orders.
55 *
56 * @var bool
57 */
58 private $enable_auto_number;
59
60 /**
61 * Prefix for order numbers.
62 *
63 * @var string
64 */
65 private $order_prefix;
66
67 /**
68 * Custom fields for Zoho Inventory sync.
69 *
70 * @var array
71 */
72 private $custom_fields;
73
74 /**
75 * Whether to include tax in calculations.
76 *
77 * @var bool
78 */
79 private $include_tax;
80
81 /**
82 * Initialize the class.
83 */
84 public function __construct() {
85 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
86 if ( ! empty( $zoho_inventory_access_token ) ) {
87 add_action( 'woocommerce_update_order', array( $this, 'salesorder_void' ) );
88 // get options.
89 $this->zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
90 $this->zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
91 $this->zoho_inventory_domain = get_option( 'cmbird_zoho_inventory_domain' );
92 $this->location_id = get_option( 'cmbird_zoho_location_id_status' );
93 $this->parent_location_id = get_option( 'cmbird_zoho_parent_location_id_status' );
94 $this->enable_order_status = get_option( 'cmbird_zoho_enable_order_status_status' );
95 $this->enable_auto_number = get_option( 'cmbird_zoho_enable_auto_number_status' );
96 $this->order_prefix = get_option( 'cmbird_zoho_order_prefix_status' );
97 $this->custom_fields = json_decode( get_option( 'cmbird_wootozoho_custom_fields' ), true );
98 $this->include_tax = get_option( 'woocommerce_prices_include_tax' ) === 'yes';
99 } else {
100 return;
101 }
102 }
103
104 /**
105 * Function to map customer on checkout before placing order
106 *
107 * @param int $order_id Order ID.
108 */
109 public function cmbird_zi_sync_customer_checkout( $order_id ) {
110 // $fd = fopen( __DIR__ . '/cmbird_zi_sync_customer_checkout.txt', 'w+' );
111 $order = wc_get_order( $order_id );
112 $userid = $order->get_user_id();
113 $is_guest_order = empty( $userid );
114 $user_company = $order->get_billing_company();
115 $user_email = $order->get_billing_email();
116
117 // For guest orders, get contact ID from order meta; for regular users, from user meta.
118 if ( $is_guest_order ) {
119 $zi_customer_id = $order->get_meta( 'zi_contact_id', true );
120 } else {
121 $zi_customer_id = get_user_meta( $userid, 'zi_contact_id', true );
122 }
123
124 // Get currency code of the order.
125 if ( $is_guest_order ) {
126 $currency_id = intval( $order->get_meta( 'zi_currency_id', true ) );
127 } else {
128 $currency_id = intval( get_user_meta( $userid, 'zi_currency_id', true ) );
129 }
130 if ( empty( $currency_id ) ) {
131 $currency_code = $order->get_currency();
132 $multi_currency_handle = new CMBIRD_Multicurrency_Zoho();
133 $currency_id = $multi_currency_handle->zoho_currency_data( $currency_code, $userid, $order );
134 }
135
136 if ( $zi_customer_id ) {
137 $zoho_inventory_oid = $this->zoho_inventory_oid;
138 $zoho_inventory_url = $this->zoho_inventory_url;
139 $get_url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id . '/?organization_id=' . $zoho_inventory_oid;
140
141 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
142 $json = $execute_curl_call_handle->execute_curl_call_get( $get_url );
143 // fwrite($fd,PHP_EOL.'customer_json: '.print_r($json, true)); --- IGNORE ---.
144 $code = $json->code;
145 if ( 0 !== $code && '0' !== $code ) {
146 // For guest orders, clear contact data immediately if validation fails.
147 // For registered users, be more cautious - only clear if it's a definitive "not found" error.
148 $should_clear_contact_data = false;
149
150 if ( 1003 === $code || '1003' === $code ) {
151 // For registered users, only clear if it's a specific "contact not found" error (code 1003).
152 // Other errors might be temporary (rate limiting, API issues, etc.).
153 $should_clear_contact_data = true;
154 }
155
156 if ( $should_clear_contact_data ) {
157 // Clear contact data - from order meta for guest orders, user meta for regular users.
158 if ( $is_guest_order ) {
159 $order->delete_meta_data( 'zi_contact_id' );
160 $order->delete_meta_data( 'zi_billing_address_id' );
161 $order->delete_meta_data( 'zi_primary_contact_id' );
162 $order->delete_meta_data( 'zi_shipping_address_id' );
163 $order->delete_meta_data( 'zi_created_time' );
164 $order->delete_meta_data( 'zi_last_modified_time' );
165 $order->save();
166 } else {
167 delete_user_meta( $userid, 'zi_contact_id' );
168 delete_user_meta( $userid, 'zi_billing_address_id' );
169 delete_user_meta( $userid, 'zi_primary_contact_id' );
170 delete_user_meta( $userid, 'zi_shipping_address_id' );
171 delete_user_meta( $userid, 'zi_created_time' );
172 delete_user_meta( $userid, 'zi_last_modified_time' );
173 }
174 $zi_customer_id = '';
175 } else {
176 // save the billing and shipping address IDs if contact exists.
177 foreach ( $json->contact as $key => $value ) {
178 if ( 'billing_address_id' === $key ) {
179 if ( $is_guest_order ) {
180 $order->update_meta_data( 'zi_billing_address_id', $value );
181 $order->save();
182 } else {
183 update_user_meta( $userid, 'zi_billing_address_id', $value );
184 }
185 }
186 if ( 'shipping_address_id' === $key ) {
187 if ( $is_guest_order ) {
188 $order->update_meta_data( 'zi_shipping_address_id', $value );
189 $order->save();
190 } else {
191 update_user_meta( $userid, 'zi_shipping_address_id', $value );
192 }
193 }
194 }
195 }
196 // If we don't clear the contact data, keep the existing $zi_customer_id and proceed.
197 }
198 }
199
200 /**
201 * Syncing customer if its not in Zoho yet.
202 */
203 if ( empty( $zi_customer_id ) ) {
204
205 // First check based on customer email address.
206 $zoho_inventory_oid = $this->zoho_inventory_oid;
207 $zoho_inventory_url = $this->zoho_inventory_url;
208 $url = $zoho_inventory_url . 'inventory/v1/contacts?organization_id=' . $zoho_inventory_oid . '&email=' . $user_email;
209
210 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
211 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
212
213 $code = $json->code;
214 // $message = $json->message;
215 if ( 0 === $code || '0' === $code ) {
216 // fwrite( $fd, PHP_EOL . 'email found: ' . print_r( $json, true ) );
217 if ( empty( $json->contacts ) ) {
218 // Second check based on Company Name.
219 if ( $user_company ) {
220 $company_name = str_replace( ' ', '%20', $user_company );
221 $url = $zoho_inventory_url . 'inventory/v1/contacts?organization_id=' . $zoho_inventory_oid . '&filter_by=Status.Active&search_text=' . $company_name;
222
223 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
224 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
225
226 $code = $json->code;
227 if ( 0 === $code || '0' === $code ) {
228 if ( empty( $json->contacts ) ) {
229 $zi_customer_id = $this->create_contact_for_order( $userid, $order_id, $is_guest_order );
230 } else {
231 foreach ( $json->contacts[0] as $key => $value ) {
232 if ( 'contact_id' === $key ) {
233 $zi_customer_id = $value;
234 // Store contact ID - in order meta for guest orders, user meta for regular users.
235 if ( $is_guest_order ) {
236 $order->update_meta_data( 'zi_contact_id', $zi_customer_id );
237 $order->save();
238 } else {
239 update_user_meta( $userid, 'zi_contact_id', $zi_customer_id );
240 }
241 }
242 }
243 $zi_customer_id = $this->create_contact_person_for_order( $userid, $order_id, $is_guest_order );
244 }
245 } else {
246 // Company search failed, create new contact.
247 $zi_customer_id = $this->create_contact_for_order( $userid, $order_id, $is_guest_order );
248 }
249 } else {
250 $zi_customer_id = $this->create_contact_for_order( $userid, $order_id, $is_guest_order );
251 }
252 } else {
253 // fwrite($fd,PHP_EOL.'Contacts : '.print_r($json->contacts,true));
254 foreach ( $json->contacts[0] as $key => $value ) {
255 if ( 'contact_id' === $key ) {
256 $zi_customer_id = $value;
257 // Store contact ID - in order meta for guest orders, user meta for regular users.
258 if ( $is_guest_order ) {
259 $order->update_meta_data( 'zi_contact_id', $zi_customer_id );
260 $order->save();
261 } else {
262 update_user_meta( $userid, 'zi_contact_id', $zi_customer_id );
263 }
264 }
265 }
266 }
267 } else {
268 // Email search failed, create new contact.
269 $zi_customer_id = $this->create_contact_for_order( $userid, $order_id, $is_guest_order );
270 }
271 // Http request not processed properly.
272 // echo $message;
273 // Final fallback: if still no contact and this is a guest order, always create contact.
274 if ( empty( $zi_customer_id ) && $is_guest_order ) {
275 $zi_customer_id = $this->create_contact_for_order( $userid, $order_id, $is_guest_order );
276 }
277 return $zi_customer_id;
278 } else {
279 $zoho_inventory_oid = $this->zoho_inventory_oid;
280 $zoho_inventory_url = $this->zoho_inventory_url;
281 $get_url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id . '/contactpersons/?organization_id=' . $zoho_inventory_oid;
282
283 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
284 $contactpersons_response = $execute_curl_call_handle->execute_curl_call_get( $get_url );
285
286 // fwrite( $fd, PHP_EOL . 'Contactpersons: ' . print_r($contactpersons_response, true) );
287
288 // first check within contactpersons endpoint and then map it with that contactperson if email-id matches.
289 if ( 0 === $contactpersons_response->code || '0' === $contactpersons_response->code ) {
290 if ( ! empty( $contactpersons_response->contact_persons ) ) {
291 foreach ( $contactpersons_response->contact_persons as $key => $contact_persons ) {
292 $person_email = trim( $contact_persons->email );
293 if ( trim( $user_email ) === $person_email ) {
294 /* Match Contact */
295 $contactid = $contact_persons->contact_person_id;
296 // Store contact person ID - in order meta for guest orders, user meta for regular users.
297 if ( $is_guest_order ) {
298 $order->update_meta_data( 'zi_contactperson_id_' . $key, $contactid );
299 $order->save();
300 } else {
301 update_user_meta( $userid, 'zi_contactperson_id_' . $key, $contactid );
302 }
303 if ( true === $contact_persons->is_primary_contact || 1 === $contact_persons->is_primary_contact ) {
304 if ( ! $is_guest_order ) {
305 $contact_class_handle = new CMBIRD_Contact_ZI();
306 $contact_class_handle->cmbird_contact_update_function( $userid, $order_id );
307 }
308 } elseif ( ! $is_guest_order ) {
309 $contact_class_handle = new CMBIRD_Contact_ZI();
310 $contact_class_handle->cmbird_update_contact_person( $userid, $order_id );
311 }
312 }
313 }
314 } else {
315 $get_url = $zoho_inventory_url . 'inventory/v1/contacts/' . $zi_customer_id . '/?organization_id=' . $zoho_inventory_oid;
316 $contact_res = $execute_curl_call_handle->execute_curl_call_get( $get_url );
317 if ( ( 0 === $contact_res->code || '0' === $contact_res->code ) && ! empty( $contact_res->contact ) ) {
318 // Access the contact object directly (no loop needed - response contains single contact object).
319 if ( trim( $contact_res->contact->email ) === trim( $user_email ) ) {
320 $contact_class_handle = new CMBIRD_Contact_ZI();
321 $contact_class_handle->cmbird_contact_update_function( $userid, $order_id );
322 } else {
323 $contact_class_handle = new CMBIRD_Contact_ZI();
324 $contact_class_handle->cmbird_create_contact_person( $userid );
325 }
326 }
327 }
328 }
329 // fwrite( $fd, PHP_EOL . 'No contactpersons ' );
330 }
331 // fclose( $fd );
332 return $zi_customer_id;
333 }
334
335 /**
336 * Function for admin zoho sync call.
337 *
338 * @param int $order_id Order ID.
339 */
340 public function zi_order_sync( $order_id ) {
341 $order = wc_get_order( $order_id );
342 if ( ! $order ) {
343 return;
344 }
345 // return if there is no zoho inventory access token.
346 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
347 if ( empty( $zoho_inventory_access_token ) ) {
348 return;
349 }
350
351 $current_time = time();
352 $last_time = $order->get_meta( 'zi_last_order_sync_time', true );
353 if ( ! empty( $last_time ) && $current_time - $last_time < 60 ) {
354 return;
355 }
356
357 // Create an action hook to prevent orders getting synced if it contains an array of statuses.
358 $ignored_statuses = apply_filters( 'cmbird_zi_order_sync_ignored_statuses', array( 'cancelled', 'failed', 'draft', 'trash' ) );
359 if ( in_array( $order->get_status(), $ignored_statuses ) ) {
360 return;
361 }
362
363 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id' );
364 $userid = $order->get_user_id();
365 $is_guest_order = empty( $userid );
366
367 // For guest orders, get contact details from order meta instead of user meta.
368 if ( $is_guest_order ) {
369 $customer_id = $order->get_meta( 'zi_contact_id', true );
370 $billing_id = $order->get_meta( 'zi_billing_address_id', true );
371 $shipping_id = $order->get_meta( 'zi_shipping_address_id', true );
372 } else {
373 $customer_id = get_user_meta( $userid, 'zi_contact_id', true );
374 $billing_id = get_user_meta( $userid, 'zi_billing_address_id', true );
375 $shipping_id = get_user_meta( $userid, 'zi_shipping_address_id', true );
376 }
377 // Sync customer if not exists.
378 if ( empty( $customer_id ) ) {
379 $customer_id = $this->cmbird_zi_sync_customer_checkout( $order_id );
380 } elseif ( empty( $billing_id ) || empty( $shipping_id ) ) {
381 // run cmbird_contact_update_function() to update address IDs.
382 $contact_class_handle = new CMBIRD_Contact_ZI();
383 $contact_class_handle->cmbird_contact_update_function( $userid, $order_id );
384 }
385 // 2nd attempt - For guest orders, get address IDs from order meta; for regular users, from user meta.
386 if ( $is_guest_order ) {
387 $billing_id = $order->get_meta( 'zi_billing_address_id', true );
388 $shipping_id = $order->get_meta( 'zi_shipping_address_id', true );
389 } else {
390 $billing_id = get_user_meta( $userid, 'zi_billing_address_id', true );
391 $shipping_id = get_user_meta( $userid, 'zi_shipping_address_id', true );
392 }
393
394 $line_items = $this->build_line_items( $order );
395 if ( empty( $line_items ) ) {
396 return;
397 }
398
399 // Final validation: Ensure customer_id is not 0 or '0' before creating request.
400 if ( empty( $customer_id ) ) {
401 $order->add_order_note( 'Zoho Order Sync Failed: Contact does not exist or could not be created.' );
402 return;
403 }
404
405 $pdt = array(
406 'customer_id' => $customer_id,
407 'date' => $order->get_date_created()->format( 'Y-m-d' ),
408 'line_items' => $line_items,
409 'is_discount_before_tax' => true,
410 'discount_type' => 'item_level',
411 'price_precision' => '2',
412 'notes' => preg_replace( '/[^A-Za-z0-9\-]/', ' ', $order->get_customer_note() ),
413 'billing_address_id' => $billing_id,
414 'shipping_address_id' => $shipping_id,
415 'delivery_method' => $order->get_shipping_method(),
416 'is_inclusive_tax' => $this->include_tax,
417 'shipping_charge' => $order->get_shipping_total(),
418 'order_status' => $this->enable_order_status ? 'draft' : 'confirmed',
419 );
420
421 if ( $this->parent_location_id && $this->location_id ) {
422 $pdt['location_id'] = $this->parent_location_id;
423 }
424
425 $shipping_tax_id = $this->get_shipping_tax_id( $order );
426 if ( $shipping_tax_id ) {
427 $pdt['shipping_charge_tax_id'] = $shipping_tax_id;
428 }
429
430 $fees = $this->get_order_fees( $order );
431 $pdt = array_merge( $pdt, $fees );
432
433 $pdt['custom_fields'] = $this->prepare_custom_fields( $order );
434 $reference = $this->prepare_reference_number( $order );
435
436 if ( $this->enable_auto_number ) {
437 $pdt['reference_number'] = $reference;
438 } else {
439 $pdt['salesorder_number'] = $order->get_id();
440 }
441
442 $userid = $order->get_user_id();
443 // get below data if zoho_inventory_domain is 'in'.
444 if ( 'in' === $this->zoho_inventory_domain ) {
445 $gst_no = get_user_meta( $userid, 'gst_no', true );
446 // if gst_no is empty then get it from order.
447 if ( empty( $gst_no ) ) {
448 $gst_no = $order->get_meta( '_gst_no', true );
449 }
450 if ( $gst_no ) {
451 $pdt['gst_no'] = $gst_no;
452 }
453
454 // Determine GST treatment based on billing company, GST number, and country.
455 $billing_company = $order->get_billing_company();
456 $billing_country = $order->get_billing_country();
457
458 // GST treatment logic - prioritize company + GST combination first.
459 if ( ! empty( $billing_company ) && ! empty( $gst_no ) ) {
460 // Company with GST number.
461 $gst_treatment = 'business_gst';
462 } elseif ( ! empty( $billing_company ) && empty( $gst_no ) && 'IN' === $billing_country ) {
463 // Company without GST number in India.
464 $gst_treatment = 'business_none';
465 } elseif ( 'IN' !== $billing_country ) {
466 // Non-Indian customer (overseas).
467 $gst_treatment = 'overseas';
468 } else {
469 // Individual consumer in India.
470 $gst_treatment = 'consumer';
471 }
472
473 // Allow override from user meta or order meta if exists.
474 $meta_gst_treatment = get_user_meta( $userid, 'gst_treatment', true );
475 if ( empty( $meta_gst_treatment ) ) {
476 $meta_gst_treatment = $order->get_meta( 'gst_treatment', true );
477 }
478 if ( ! empty( $meta_gst_treatment ) ) {
479 $gst_treatment = $meta_gst_treatment;
480 }
481
482 $pdt['gst_treatment'] = $gst_treatment;
483 $pdt['place_of_supply'] = $order->get_billing_state();
484 }
485
486 $response_msg = $zi_sales_order_id ? $this->single_saleorder_zoho_inventory_update( $order_id, $zi_sales_order_id, wp_json_encode( $pdt ) ) : $this->single_saleorder_zoho_inventory( wp_json_encode( $pdt ), $order_id );
487
488 $order->update_meta_data( 'zi_body_request', wp_json_encode( $pdt ) );
489 $order->update_meta_data( 'zi_salesorder_id', $response_msg['zi_salesorder_id'] );
490 $order->update_meta_data( 'zi_last_order_sync_time', $current_time );
491 $order->add_order_note( 'Zoho Order Sync: ' . $response_msg['message'] );
492 $order->save();
493 }
494
495 /**
496 * Build line items for a given order.
497 *
498 * @param WC_Order $order WooCommerce order object.
499 *
500 * @return array Array of line items.
501 */
502 private function build_line_items( $order ) {
503 $items = array();
504
505 foreach ( $order->get_items() as $item_id => $item ) {
506 $product = $item->get_product();
507 if ( ! $product ) {
508 continue;
509 }
510
511 $name = $item->get_name();
512 $meta = $item->get_formatted_meta_data();
513 $desc = '';
514 if ( ! empty( $meta ) ) {
515 foreach ( $meta as $meta_value ) {
516 $desc .= $meta_value->display_key . ': ' . wp_strip_all_tags( $meta_value->display_value ) . ' ';
517 }
518 $desc = sanitize_text_field( trim( $desc ) );
519 }
520
521 $quantity = $item->get_quantity();
522 $subtotal = $item->get_subtotal();
523 $total = $item->get_total();
524 $rate = $subtotal / max( 1, $quantity );
525 $discount = $subtotal > $total ? $subtotal - $total : 0;
526
527 $tax_data = $item->get_taxes();
528 $tax_id = '';
529 $tax_percent = 0;
530
531 // Preserve original tax/discount calculation logic but ensure the line is created
532 // and added even when there are no taxes for the item.
533 if ( ! empty( $tax_data['subtotal'] ) ) {
534 $tax_rate_ids = array_keys( $tax_data['subtotal'] );
535 $tax_count = count( $tax_rate_ids );
536
537 // Check if this is a compound tax (multiple tax rates).
538 if ( $tax_count > 1 ) {
539 // Calculate total tax percentage from all rates.
540 $total_tax_percent = 0;
541 foreach ( $tax_rate_ids as $tax_rate_id ) {
542 $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id );
543 $total_tax_percent += floatval( $tax_rate['tax_rate'] );
544 }
545
546 // Try to find a matching tax group.
547 $tax_id = $this->zi_get_tax_group_id( $total_tax_percent );
548
549 if ( $tax_id ) {
550 // Found a matching tax group, use the total percentage.
551 $tax_percent = $total_tax_percent;
552 } else {
553 // No tax group found, fall back to first tax rate only.
554 $tax_rate_id = $tax_rate_ids[0];
555 $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id );
556 $tax_percent = $tax_rate['tax_rate'];
557
558 // For India domain, use tax name to determine IGST or CGST/SGST.
559 if ( 'in' === $this->zoho_inventory_domain ) {
560 $tax_name = isset( $tax_rate['tax_rate_name'] ) ? $tax_rate['tax_rate_name'] : '';
561 if ( stripos( $tax_name, 'IGST' ) !== false ) {
562 $tax_id = $this->zi_get_igst_tax_id( $tax_percent );
563 } elseif ( stripos( $tax_name, 'SGST' ) !== false || stripos( $tax_name, 'CGST' ) !== false ) {
564 $tax_id = $this->zi_get_tax_id( $tax_percent );
565 } else {
566 // Keep tax_id empty if tax name doesn't indicate IGST or CGST/SGST.
567 $tax_id = '';
568 }
569 } else {
570 // For non-India domains, use regular tax_id.
571 $tax_id = $this->zi_get_tax_id( $tax_percent );
572 }
573 }
574 } else {
575 // Single tax rate - use existing logic.
576 $tax_rate_id = $tax_rate_ids[0];
577 $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id );
578 $tax_percent = $tax_rate['tax_rate'];
579
580 // For India domain, use tax name to determine IGST or CGST/SGST.
581 if ( 'in' === $this->zoho_inventory_domain ) {
582 $tax_name = isset( $tax_rate['tax_rate_name'] ) ? $tax_rate['tax_rate_name'] : '';
583 if ( stripos( $tax_name, 'IGST' ) !== false ) {
584 $tax_id = $this->zi_get_igst_tax_id( $tax_percent );
585 } elseif ( stripos( $tax_name, 'SGST' ) !== false || stripos( $tax_name, 'CGST' ) !== false ) {
586 $tax_id = $this->zi_get_tax_id( $tax_percent );
587 } else {
588 // Keep tax_id empty if tax name doesn't indicate IGST or CGST/SGST.
589 $tax_id = '';
590 }
591 } else {
592 // For non-India domains, use regular tax_id.
593 $tax_id = $this->zi_get_tax_id( $tax_percent );
594 }
595 }
596 }
597
598 // Always build the line item (previous logic accidentally only added items when taxes existed).
599 $line = array(
600 'item_id' => get_post_meta( $product->get_id(), 'zi_item_id', true ),
601 'name' => $name,
602 'description' => $desc,
603 'quantity' => $quantity,
604 'rate' => number_format( $rate, 2, '.', '' ),
605 );
606
607 if ( $discount > 0 ) {
608 $line['discount'] = number_format( $discount, 2, '.', '' );
609 }
610 if ( $tax_id ) {
611 $line['tax_percentage'] = $tax_percent;
612 $line['tax_id'] = $tax_id;
613 }
614 // add location id if set.
615 if ( ! empty( $this->location_id ) ) {
616 $line['location_id'] = $this->location_id;
617 }
618 $items[] = $line;
619 }
620 return $items;
621 }
622
623 /**
624 * Get the Zoho Inventory tax id for the shipping tax.
625 *
626 * @param WC_Order $order The WooCommerce order object.
627 * @return string The Zoho Inventory tax id, or empty string if no tax is applicable.
628 */
629 private function get_shipping_tax_id( $order ) {
630 $shipping_total = $order->get_shipping_total();
631 $shipping_tax = $order->get_shipping_tax();
632 if ( $shipping_total > 0 && $shipping_tax > 0 ) {
633 $percent = ( $shipping_tax / $shipping_total ) * 100;
634 return $this->zi_get_tax_id( round( $percent, 2 ) );
635 }
636 return '';
637 }
638
639 /**
640 * Retrieves the fees associated with an order and formats them for Zoho Inventory.
641 *
642 * @param WC_Order $order The WooCommerce order object.
643 *
644 * @return array Associative array containing the fee amount and description.
645 */
646 private function get_order_fees( $order ) {
647 $fees = $order->get_fees();
648 $data = array();
649 foreach ( $fees as $fee ) {
650 $amount = $fee->get_total();
651 if ( $amount > 0 ) {
652 $data['adjustment'] = $amount;
653 $data['adjustment_description'] = $fee->get_name();
654 }
655 }
656 return $data;
657 }
658
659 /**
660 * Prepare custom fields for the order.
661 *
662 * Iterates over the $custom_fields array and creates an associative array
663 * containing the custom field label and value.
664 *
665 * @param WC_Order $order The WooCommerce order object.
666 * @return array Associative array containing the custom field label and value.
667 */
668 private function prepare_custom_fields( $order ) {
669 $fields = array();
670 if ( is_array( $this->custom_fields ) ) {
671 foreach ( $this->custom_fields as $meta_key => $label ) {
672 $fields[] = array(
673 'label' => $label,
674 'value' => $order->get_meta( $meta_key ),
675 );
676 }
677 }
678 return $fields;
679 }
680
681 /**
682 * Prepare the reference number for the sales order.
683 *
684 * The reference number is used to identify the sales order in Zoho Inventory.
685 * It is generated by combining the order prefix (if set) with the transaction ID
686 * (if available) or the order ID.
687 *
688 * @param WC_Order $order The WooCommerce order object.
689 * @return string The reference number for the sales order.
690 */
691 private function prepare_reference_number( $order ) {
692 $transaction_id = $order->get_transaction_id();
693 if ( empty( $transaction_id ) ) {
694 $transaction_id = $order->get_meta( '_order_number', true );
695 }
696 if ( class_exists( 'WCJ_Order_Numbers' ) || class_exists( 'WC_Seq_Order_Number_Pro' ) ) {
697 return $this->order_prefix . $transaction_id;
698 }
699 if ( ! empty( $this->order_prefix ) ) {
700 return $this->order_prefix . '-' . $order->get_id();
701 }
702 return $order->get_id();
703 }
704
705 /**
706 * Void the sales order if cancelled in WooCommerce.
707 *
708 * @param int $order_id Order ID.
709 */
710 public function salesorder_void( $order_id ) {
711 if ( ! $order_id ) {
712 return;
713 }
714 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
715 if ( empty( $zoho_inventory_access_token ) ) {
716 return;
717 }
718
719 $order = wc_get_order( $order_id );
720 // return if order is already voided.
721 if ( $order->get_meta( 'zi_salesorder_void', true ) ) {
722 return;
723 }
724 $order_status = $order->get_status();
725
726 if ( 'cancelled' === $order_status || 'wc-merged' === $order_status ) {
727 $zi_sales_order_id = $order->get_meta( 'zi_salesorder_id', true );
728 $zoho_inventory_oid = $this->zoho_inventory_oid;
729 $zoho_inventory_url = $this->zoho_inventory_url;
730
731 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id . '/status/void?organization_id=' .
732 $zoho_inventory_oid;
733 $data = '';
734 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
735 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
736
737 $errmsg = $json->message;
738 $code = $json->code;
739 if ( '0' === $code || 0 === $code ) {
740 // Add order meta key "zi_salesorder_void" to true.
741 $order->update_meta_data( 'zi_salesorder_void', true );
742 $order->add_order_note( 'Zoho Order Void: ' . $errmsg );
743 $order->save();
744 return;
745 }
746 } else {
747 return;
748 }
749 }
750
751 /**
752 * Sync order from Woo to Zoho.
753 *
754 * @param string $pdt1 JSON string.
755 * @param int $order_id Order ID.
756 * @return string Error message
757 */
758 public function single_saleorder_zoho_inventory( $pdt1, $order_id ) {
759 // start logging.
760 // $fd = fopen( __DIR__ . '/order-sync-backend.txt', 'w+' );
761
762 $zoho_inventory_oid = $this->zoho_inventory_oid;
763 $zoho_inventory_url = $this->zoho_inventory_url;
764 $data = array(
765 'JSONString' => $pdt1,
766 'organization_id' => $zoho_inventory_oid,
767 );
768
769 // logging.
770 // fwrite($fd, PHP_EOL . 'Data log : ' . print_r($data, true));
771
772 if ( empty( $data['JSONString'] ) ) {
773 return '';
774 }
775 $ignore_auto_no = $this->enable_auto_number ? 'false' : 'true';
776 $url = $zoho_inventory_url . 'inventory/v1/salesorders?ignore_auto_number_generation=' . $ignore_auto_no;
777
778 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
779 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
780
781 // fwrite( $fd, PHP_EOL . 'Data log : ' . print_r( $json, true ) );
782 $response = array();
783 $code = $json->code;
784 $errmsg = $json->message;
785 // fwrite($fd, PHP_EOL . 'Code : ' . $code);
786
787 // Retry once if code is 400.
788 if ( 400 === $code || '400' === $code ) {
789 // Wait a brief moment before retry.
790 sleep( 1 );
791 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $data );
792 $code = $json->code;
793 $errmsg = $json->message;
794 }
795
796 if ( '0' === $code || 0 === $code ) {
797 foreach ( $json->salesorder as $key => $value ) {
798
799 if ( 'salesorder_id' === $key ) {
800 $response['zi_salesorder_id'] = $value;
801 // $order->add_meta_data('zi_salesorder_id', $value, true);
802 }
803 }
804 } else {
805 // send email to admin with the error message.
806 // create message that contains the error message and order id.
807 $message = 'Error in Zoho Inventory Order Sync: ' . $errmsg;
808 // append the link to the order edit page.
809 $message .= '<br><br><a href="' . admin_url( 'admin.php?page=wc-orders&action=edit&id=' . $order_id ) . '">Click here to view the order in WP Admin</a>';
810 $message .= '<br><br>Order ID: ' . $order_id;
811 $message .= '<br><br>Request Body: ' . $pdt1;
812 $class_common = new CMBIRD_Common_Functions();
813 $class_common->send_email( 'Error in Zoho Order Sync', $message );
814 }
815 $response['message'] = $errmsg;
816 // fclose( $fd );
817
818 return $response;
819 }
820
821 /**
822 * Function for updating single sales order.
823 *
824 * @param int $order_id Order ID.
825 * @param string $zi_sales_order_id Zoho Sales Order ID.
826 * @param string $pdt1 JSON string.
827 * @return string Error message
828 */
829 public function single_saleorder_zoho_inventory_update( $order_id, $zi_sales_order_id, $pdt1 ) {
830 // $fd = fopen( __DIR__. '/single_saleorder_update.txt', 'w+' );
831
832 $response = array();
833 $zoho_inventory_oid = $this->zoho_inventory_oid;
834 $zoho_inventory_url = $this->zoho_inventory_url;
835
836 $url = $zoho_inventory_url . 'inventory/v1/salesorders/' . $zi_sales_order_id;
837 $data = array(
838 'JSONString' => $pdt1,
839 'organization_id' => $zoho_inventory_oid,
840 );
841
842 $order = wc_get_order( $order_id );
843 // fwrite($fd, PHP_EOL. print_r($data, true)); //logging response.
844 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
845 $json = $execute_curl_call_handle->execute_curl_call_put( $url, $data );
846
847 // $code = $json->code;
848 $errmsg = $json->message;
849 $response['message'] = $errmsg;
850 $response['zi_salesorder_id'] = $zi_sales_order_id;
851
852 // echo '<pre>'; print_r($errmsg);
853
854 $package_id = $order->get_meta( 'zi_package_id', true );
855
856 if ( ! empty( $package_id ) ) {
857 // fwrite($fd, PHP_EOL. 'inside package exists'); //logging response.
858 foreach ( $json->salesorder as $key => $value ) {
859 if ( 'date' === $key ) {
860 $ship_date = $value;
861 }
862
863 if ( 'line_items' === $key ) {
864 foreach ( $value as $kk => $vv ) {
865 $line_items[] = '{"so_line_item_id": "' . $vv->line_item_id . '","quantity": "' . $vv->quantity . '"}';
866 }
867 $impot = implode( ',', $line_items );
868
869 $json_package = '"date": "' . $ship_date . '","line_items": [' . $impot . ']';
870
871 $url_package = $zoho_inventory_url . 'inventory/v1/packages/' . $package_id;
872 $data3 = array(
873 'JSONString' => '{' . $json_package . '}',
874 'organization_id' => $zoho_inventory_oid,
875 );
876
877 $res_package = $execute_curl_call_handle->execute_curl_call_put( $url_package, $data3 );
878 }
879 }
880 }
881 // fclose( $fd ); //end of logging.
882 return $response;
883 }
884
885 /**
886 * Function to get all Zoho Taxes.
887 *
888 * @param int $percentage Tax percentage.
889 * @return string Tax ID.
890 */
891 private function zi_get_tax_id( $percentage ) {
892 // $fd = fopen( __DIR__ . '/zi_get_tax_id.txt', 'a+' );
893 // get all options that contain zoho_inventory_tax_rate_ in the name using global $wpdb.
894 global $wpdb;
895 $zoho_tax_rates = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'cmbird_zoho_inventory_tax_rate_%'" );
896 $input_tax_percentage = floor( $percentage * 10 ) / 10;
897 // fwrite( $fd, PHP_EOL . 'Input Tax Percentage: ' . $input_tax_percentage );
898 $tax_id = '';
899 // for each zoho_tax_rate, check if the tax percentage matches the input percentage. The percentage at the end of the value e.g. 69497000002395146##BTW@@Hoog Exclusief##tax##21.
900 foreach ( $zoho_tax_rates as $zoho_tax_rate ) {
901 $tax_rate = explode( '##', $zoho_tax_rate->option_value );
902 $tax_percentage = $tax_rate[3];
903 // Round the stored tax percentage to one decimal place for comparison.
904 $stored_tax_percentage = round( $tax_percentage, 1 );
905 // Compare the rounded tax percentages with a tolerance for floating-point precision errors.
906 if ( abs( $stored_tax_percentage - $input_tax_percentage ) < 0.01 ) {
907 $tax_id = $tax_rate[0];
908 break;
909 }
910 }
911 return $tax_id;
912 }
913
914 /**
915 * Get Zoho Inventory tax group ID by total tax percentage.
916 *
917 * This function attempts to find a matching tax group when multiple
918 * WooCommerce tax rates are applied (compound taxes). Tax groups in Zoho
919 * represent compound taxes as a single combined rate.
920 *
921 * @param float $total_percentage Total tax percentage from all WooCommerce tax rates.
922 * @return string Tax group ID if found, empty string otherwise.
923 */
924 private function zi_get_tax_group_id( $total_percentage ) {
925 $tax_groups = get_option( 'cmbird_zi_tax_groups', array() );
926
927 if ( empty( $tax_groups ) || ! is_array( $tax_groups ) ) {
928 return '';
929 }
930
931 foreach ( $tax_groups as $tax_group ) {
932 if ( ! isset( $tax_group['tax_id'] ) || ! isset( $tax_group['tax_percentage'] ) ) {
933 continue;
934 }
935
936 // Allow tolerance of 0.2 for compound tax matching.
937 if ( abs( $tax_group['tax_percentage'] - $total_percentage ) <= 0.5 ) {
938 return $tax_group['tax_id'];
939 }
940 }
941
942 return '';
943 }
944
945 /**
946 * Function to get Zoho Inventory IGST tax ID for inter-state transactions in India.
947 *
948 * @param int $percentage Tax percentage.
949 * @return string IGST Tax ID.
950 */
951 private function zi_get_igst_tax_id( $percentage ) {
952 // Get all options that contain zoho_inventory_tax_rate_ in the name for IGST.
953 global $wpdb;
954 $zoho_tax_rates = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'cmbird_zoho_inventory_tax_rate_%'" );
955 $input_tax_percentage = floor( $percentage * 10 ) / 10;
956 $tax_id = '';
957
958 // For each zoho_tax_rate, check if it's an IGST tax and the percentage matches.
959 foreach ( $zoho_tax_rates as $zoho_tax_rate ) {
960 $tax_rate = explode( '##', $zoho_tax_rate->option_value );
961 if ( count( $tax_rate ) >= 4 ) {
962 $tax_name = $tax_rate[1];
963 $tax_percentage = $tax_rate[3];
964
965 // Check if this is an IGST tax (contains "IGST" in the name).
966 if ( stripos( $tax_name, 'IGST' ) !== false ) {
967 // Round the stored tax percentage to one decimal place for comparison.
968 $stored_tax_percentage = round( $tax_percentage, 1 );
969 // Compare the rounded tax percentages with a tolerance for floating-point precision errors.
970 if ( abs( $stored_tax_percentage - $input_tax_percentage ) < 0.01 ) {
971 $tax_id = $tax_rate[0];
972 break;
973 }
974 }
975 }
976 }
977
978 // If no IGST tax found, fallback to regular tax_id.
979 if ( empty( $tax_id ) ) {
980 $tax_id = $this->zi_get_tax_id( $percentage );
981 }
982
983 return $tax_id;
984 }
985
986 /**
987 * TODO: Get Order Transaction Fees
988 *
989 * @param int $order_id Order ID.
990 * @return float Transaction fees
991 */
992 protected function get_transaction_fees( $order_id ) {
993 $order = wc_get_order( $order_id );
994 $fees = $order->get_meta( '_stripe_fee' );
995 if ( ! empty( $fees ) ) {
996 return $fees;
997 }
998 $fees = $order->get_meta( '_paypal_transaction_fee' );
999 if ( ! empty( $fees ) ) {
1000 return $fees;
1001 }
1002 return 0;
1003 }
1004
1005 /**
1006 * Create contact for order - handles both regular users and guest orders
1007 *
1008 * @param int $userid User ID (0 for guest orders).
1009 * @param int $order_id Order ID.
1010 * @param bool $is_guest_order Whether this is a guest order.
1011 * @return string Contact ID.
1012 */
1013 private function create_contact_for_order( $userid, $order_id, $is_guest_order ) {
1014 if ( $is_guest_order ) {
1015 // For guest orders, create contact using order data and store contact info in order meta.
1016 $order = wc_get_order( $order_id );
1017 return $this->create_guest_contact( $order );
1018 } else {
1019 // For regular users, use existing contact creation function.
1020 $contact_class_handle = new CMBIRD_Contact_ZI();
1021 return $contact_class_handle->cmbird_contact_create_function( $userid );
1022 }
1023 }
1024
1025 /**
1026 * Create contact person for order - handles both regular users and guest orders
1027 *
1028 * @param int $userid User ID (0 for guest orders).
1029 * @param int $order_id Order ID.
1030 * @param bool $is_guest_order Whether this is a guest order.
1031 * @return string Contact ID.
1032 */
1033 private function create_contact_person_for_order( $userid, $order_id, $is_guest_order ) {
1034 if ( $is_guest_order ) {
1035 // For guest orders, create contact person using order data.
1036 $order = wc_get_order( $order_id );
1037 return $this->create_guest_contact_person( $order );
1038 } else {
1039 // For regular users, use existing contact person creation function.
1040 $contact_class_handle = new CMBIRD_Contact_ZI();
1041 return $contact_class_handle->cmbird_create_contact_person( $userid );
1042 }
1043 }
1044
1045 /**
1046 * Create Zoho contact for guest order
1047 *
1048 * @param WC_Order $order Order object.
1049 * @return string Contact ID.
1050 */
1051 private function create_guest_contact( $order ) {
1052 // Billing Details.
1053 $contact_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
1054 $company_name = $order->get_billing_company();
1055 $billing_address = $order->get_billing_address_1();
1056 $billing_address2 = $order->get_billing_address_2();
1057 $billing_city = $order->get_billing_city();
1058 $billing_state = $order->get_billing_state();
1059 $billing_postcode = $order->get_billing_postcode();
1060 $billing_country = $order->get_billing_country();
1061
1062 // Shipping Details.
1063 $shipping_attention = trim( $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name() );
1064 $shipping_address = $order->get_shipping_address_1();
1065 $shipping_address2 = $order->get_shipping_address_2();
1066 $shipping_city = $order->get_shipping_city();
1067 $shipping_state = $order->get_shipping_state();
1068 $shipping_postcode = $order->get_shipping_postcode();
1069 $shipping_country = $order->get_shipping_country();
1070 // If shipping not available assign billing as shipping (same behavior as cmbird_contact_create_function()).
1071 if ( empty( $shipping_address ) ) {
1072 $shipping_address = $billing_address;
1073 }
1074 if ( empty( $shipping_address2 ) ) {
1075 $shipping_address2 = $billing_address2;
1076 }
1077 if ( empty( $shipping_postcode ) ) {
1078 $shipping_postcode = $billing_postcode;
1079 }
1080 if ( empty( $shipping_city ) ) {
1081 $shipping_city = $billing_city;
1082 }
1083 if ( empty( $shipping_country ) ) {
1084 $shipping_country = $billing_country;
1085 }
1086 if ( empty( $shipping_state ) ) {
1087 $shipping_state = $billing_state;
1088 }
1089 if ( '' === $shipping_attention ) {
1090 $shipping_attention = $contact_name;
1091 }
1092 // Customer Details.
1093 $customer_email = $order->get_billing_email();
1094 $customer_phone = $order->get_billing_phone();
1095
1096 // Get currency code.
1097 $currency_code = $order->get_currency();
1098 $multi_currency_handle = new CMBIRD_Multicurrency_Zoho();
1099 $currency_id = $multi_currency_handle->zoho_currency_data( $currency_code, '', $order );
1100
1101 // Build contact data.
1102 $data = array(
1103 'contact_name' => $contact_name,
1104 'company_name' => $company_name,
1105 'contact_type' => 'customer',
1106 'currency_id' => $currency_id,
1107 'contact_persons' => array(
1108 array(
1109 'first_name' => $order->get_billing_first_name(),
1110 'last_name' => $order->get_billing_last_name(),
1111 'email' => $customer_email,
1112 'phone' => $customer_phone,
1113 ),
1114 ),
1115 'billing_address' => array(
1116 'attention' => $contact_name,
1117 'address' => $billing_address,
1118 'street2' => $billing_address2,
1119 'city' => $billing_city,
1120 'state' => $billing_state,
1121 'zip' => $billing_postcode,
1122 'country' => $billing_country,
1123 ),
1124 'shipping_address' => array(
1125 'attention' => $shipping_attention,
1126 'address' => $shipping_address,
1127 'street2' => $shipping_address2,
1128 'city' => $shipping_city,
1129 'state' => $shipping_state,
1130 'zip' => $shipping_postcode,
1131 'country' => $shipping_country,
1132 ),
1133 );
1134 // append place_of_contact if zoho_inventory_domain is 'in'.
1135 if ( 'in' === $this->zoho_inventory_domain ) {
1136 $data['place_of_contact'] = $billing_state;
1137 }
1138
1139 // Create contact in Zoho (data must be JSON string in 'JSONString' key, as in cmbird_contact_create_function()).
1140 $zoho_inventory_oid = $this->zoho_inventory_oid;
1141 $zoho_inventory_url = $this->zoho_inventory_url;
1142 $url = $zoho_inventory_url . 'inventory/v1/contacts';
1143
1144 $post_data = array(
1145 'JSONString' => wp_json_encode( $data ),
1146 'organization_id' => $zoho_inventory_oid,
1147 );
1148
1149 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
1150 $json = $execute_curl_call_handle->execute_curl_call_post( $url, $post_data );
1151
1152 if ( isset( $json->code ) && ( 0 === $json->code || '0' === $json->code ) ) {
1153 $contact_id = $json->contact->contact_id;
1154
1155 // Store contact details in order meta.
1156 $order->update_meta_data( 'zi_contact_id', $contact_id );
1157
1158 if ( isset( $json->contact->billing_address->address_id ) ) {
1159 $order->update_meta_data( 'zi_billing_address_id', $json->contact->billing_address->address_id );
1160 }
1161
1162 if ( isset( $json->contact->shipping_address->address_id ) ) {
1163 $order->update_meta_data( 'zi_shipping_address_id', $json->contact->shipping_address->address_id );
1164 }
1165
1166 if ( isset( $json->contact->primary_contact_id ) ) {
1167 $order->update_meta_data( 'zi_primary_contact_id', $json->contact->primary_contact_id );
1168 }
1169
1170 if ( isset( $json->contact->created_time ) ) {
1171 $order->update_meta_data( 'zi_created_time', $json->contact->created_time );
1172 }
1173
1174 if ( isset( $json->contact->last_modified_time ) ) {
1175 $order->update_meta_data( 'zi_last_modified_time', $json->contact->last_modified_time );
1176 }
1177
1178 if ( isset( $json->contact->currency_id ) ) {
1179 $order->update_meta_data( 'zi_currency_id', $json->contact->currency_id );
1180 }
1181
1182 $order->save();
1183
1184 return $contact_id;
1185 }
1186
1187 return '';
1188 }
1189
1190 /**
1191 * Create Zoho contact person for guest order
1192 *
1193 * @param WC_Order $order Order object.
1194 * @return string Contact ID.
1195 */
1196 private function create_guest_contact_person( $order ) {
1197 // For guest orders, just return the main contact ID since it's already created with contact person.
1198 return $order->get_meta( 'zi_contact_id', true );
1199 }
1200 }
1201 $CMBIRD_Order_Sync_ZI = new CMBIRD_Order_Sync_ZI();
1202