PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.14
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.14
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-product-webhook.php
commercebird / includes / classes / apis Last commit date
class-api-for-cmbird.php 1 year ago class-api-for-exact-webhooks.php 1 year ago class-api-for-product-webhook.php 1 year ago class-api-for-shipping-status.php 1 year ago class-api-for-woo-order.php 1 year ago class-api-for-zoho-inventory.php 1 year ago class-commercebird-list-items-api-controller.php 1 year ago class-commercebird-media-api-controller.php 1 year ago class-commercebird-metadata-controller.php 1 year ago index.php 1 year ago trait-api-permission.php 11 months ago
class-api-for-product-webhook.php
563 lines
1 <?php
2
3 namespace CommerceBird\API;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use CMBIRD_Image_ZI;
10 use CMBIRD_Products_ZI;
11 use CMBIRD_Products_ZI_Export;
12 use WC_Data_Exception;
13 use WC_Product_Variation;
14 use WC_Product_Variable;
15 use WP_REST_Response;
16 use wpdb;
17 use CMBIRD_Common_Functions;
18 use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore;
19
20 class ProductWebhook {
21
22
23 use Api;
24
25 private static string $endpoint = 'zoho-product';
26
27 private $is_tax_enabled;
28
29 public function __construct() {
30 register_rest_route(
31 self::$namespace,
32 self::$endpoint,
33 array(
34 'methods' => 'POST',
35 'callback' => array( $this, 'handle' ),
36 'permission_callback' => '__return_true',
37 )
38 );
39 // Check if WooCommerce taxes are enabled and store the result
40 $this->is_tax_enabled = 'yes' === get_option( 'woocommerce_calc_taxes' );
41 }
42
43 // Method to use the tax check across the class
44 public function is_tax_enabled(): bool {
45 return $this->is_tax_enabled;
46 }
47
48
49 /**
50 * @throws WC_Data_Exception
51 */
52 private function process( array $data ): WP_REST_Response {
53 $response = new WP_REST_Response();
54 $response->set_data( $this->empty_response );
55 $response->set_status( 404 );
56 if ( ! array_key_exists( 'item', $data ) && ! array_key_exists( 'inventory_adjustment', $data ) ) {
57 return $response;
58 }
59
60 // Accounting stock mode check
61 $accounting_stock = get_option( 'cmbird_zoho_enable_accounting_stock_status' );
62 $zi_enable_locationstock = get_option( 'cmbird_zoho_enable_locationstock_status' );
63 $location_id = get_option( 'cmbird_zoho_location_id_status' );
64
65 // variable item sync
66 if ( array_key_exists( 'item', $data ) ) {
67 return $this->process_product_data( $data['item'], $zi_enable_locationstock, $location_id, $accounting_stock );
68 }
69 // inventory_adjustment
70 if ( array_key_exists( 'inventory_adjustment', $data ) ) {
71 return $this->inventory_adjustment( $data['inventory_adjustment'] );
72 }
73
74 return $response;
75 }
76
77 /**
78 * @param $item
79 * @param $zi_enable_locationstock
80 * @param $location_id
81 * @param $accounting_stock
82 *
83 * @return WP_REST_Response
84 * @throws WC_Data_Exception
85 */
86 public function process_product_data( $item, $zi_enable_locationstock, $location_id, $accounting_stock ): WP_REST_Response {
87 // $fd = fopen( __DIR__ . '/process_product_data.txt', 'a+' );
88
89 // clean orphaned data from the database
90 $common_class = new CMBIRD_Common_Functions();
91 $common_class->clear_orphan_data();
92
93 global $wpdb;
94 $item_id = $item['item_id'];
95 $item_name = $item['name'];
96 $item_price = $item['rate'];
97 $item_sku = $item['sku'];
98 $item_description = $item['description'];
99 $item_status = $item['status'] === 'active' ? 'publish' : 'draft';
100 $item_brand = $item['brand'];
101 $category_id = $item['category_id'];
102 $custom_fields = $item['custom_fields'];
103 $item_image = $item['image_name'];
104 // Stock mode check
105 $locations = $item['locations'];
106 if ( $zi_enable_locationstock ) {
107 foreach ( $locations as $location ) {
108 if ( $location['location_id'] === $location_id ) {
109 if ( $accounting_stock ) {
110 $item_stock = $location['location_available_for_sale_stock'];
111 } else {
112 $item_stock = $location['location_actual_available_for_sale_stock'];
113 }
114 }
115 }
116 } elseif ( $accounting_stock && ! $zi_enable_locationstock ) {
117 $item_stock = $item['available_for_sale_stock'];
118 } else {
119 $item_stock = $item['actual_available_for_sale_stock'];
120 }
121 if ( isset( $item['group_name'] ) ) {
122 $group_name = $item['group_name'];
123 } else {
124 $group_name = '';
125 }
126 $item_category = $item['category_name'];
127 if ( isset( $item['group_id'] ) ) {
128 $groupid = $item['group_id'];
129 } else {
130 $groupid = '';
131 }
132
133 // Item package details
134 $details = $item['package_details'];
135 $weight = floatval( $details['weight'] );
136 $length = floatval( $details['length'] );
137 $width = floatval( $details['width'] );
138 $height = floatval( $details['height'] );
139
140 // fwrite($fd, PHP_EOL . '$groupid : ' . $groupid);
141 if ( ! empty( $groupid ) ) {
142 // fwrite($fd, PHP_EOL . 'Inside grouped items');
143 // find parent variable product
144 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key='zi_item_id' AND meta_value=%s", $groupid ) );
145 $group_id = $row->post_id;
146
147 if ( ! empty( $group_id ) ) {
148 $existing_parent_product = wc_get_product( $group_id );
149 $zi_disable_itemdescription_sync = get_option( 'cmbird_zoho_disable_description_sync_status' );
150 if ( ! empty( $item_description ) && ! $zi_disable_itemdescription_sync ) {
151 // fwrite($fd, PHP_EOL . 'Item description update : ' . $item_description);
152 $existing_parent_product->set_short_description( $item_description );
153 }
154 // Update the name of the variable product if allowed
155 $zi_disable_itemname_sync = get_option( 'cmbird_zoho_disable_name_sync_status' );
156 if ( ! $zi_disable_itemname_sync ) {
157 $existing_parent_product->set_name( $item['group_name'] );
158 $slug = sanitize_title( $item['group_name'] );
159 $existing_parent_product->set_slug( $slug );
160 }
161 // Brand update if taxonomy product_brand exists
162 if ( ! empty( $item_brand ) && taxonomy_exists( 'product_brand' ) ) {
163 wp_set_object_terms( $groupid, $item_brand, 'product_brand' );
164 } elseif ( ! empty( $item_brand ) && taxonomy_exists( 'product_brand' ) ) {
165 wp_set_object_terms( $groupid, $item_brand, 'product_brand' );
166 }
167 // Update the custom fields if the custom fields are not empty
168 $cmbird_product_zi = new CMBIRD_Products_ZI();
169 if ( ! empty( $custom_fields ) ) {
170 $cmbird_product_zi->sync_item_custom_fields( $custom_fields, $groupid );
171 }
172
173 // Create or Update the Attributes
174 // turn $item into array
175 $gp_arr = (array) $item;
176 $attr_created = $cmbird_product_zi->sync_attributes_of_group( $gp_arr, $group_id );
177 if ( ! empty( $group_id ) && $attr_created ) {
178 // Create the variations
179 $cmbird_product_zi->import_variable_product_variations( $item, $group_id );
180 }
181
182 // set the product status of the variable parent product
183 // $existing_parent_product->set_status( $item_status );
184 $existing_parent_product->save();
185 } else {
186 // Add in scheduler to create the Variable Product
187 $last_synced_page = get_option( 'cmbird_group_item_sync_page_cat_id_' . $category_id );
188 if ( ! intval( $last_synced_page ) ) {
189 $last_synced_page = 1;
190 }
191 $data = array(
192 'page' => $last_synced_page,
193 'category' => $category_id,
194 );
195 $existing_schedule = as_has_scheduled_action( 'import_group_items_cron', $data );
196 // Schedule the action if it doesn't exist.
197 if ( ! $existing_schedule ) {
198 as_schedule_single_action( time(), 'import_group_items_cron', $data );
199 }
200 }
201
202 $row_item = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key='zi_item_id' AND meta_value=%s", $item_id ) );
203 $variation_id = $row_item->post_id;
204 if ( $variation_id ) {
205 // updating existing variations
206 $variation = new WC_Product_Variation( $variation_id );
207 // Prices
208 if ( ! empty( $item_price ) ) {
209 $variation->set_price( $item_price );
210 }
211 $variation->set_regular_price( $item_price );
212 // Stock
213 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
214 if ( ! empty( $item_stock ) && ! $zi_disable_stock_sync ) {
215 fwrite($fd, PHP_EOL . 'Stock is here:'. $item_stock );
216 $variation->set_stock_quantity( $item_stock );
217 $variation->set_manage_stock( true );
218 // $variation->set_stock_status('');
219 } else {
220 // fwrite($fd, PHP_EOL . 'Available Stock : false');
221 $variation->set_manage_stock( false );
222 }
223 // featured image
224 $zi_disable_itemimage_sync = get_option( 'cmbird_zoho_disable_image_sync_status' );
225 if ( ! empty( $item_image ) && ! $zi_disable_itemimage_sync ) {
226 // fwrite($fd, PHP_EOL . 'Sync Image' );
227 $image_class = new CMBIRD_Image_ZI();
228 $image_class->cmbird_zi_get_image( $item_id, $item_name, $variation_id, $item_image );
229 }
230 // Disable or enable the variation based on the item_status
231 $variation->set_status( $item_status );
232 // Update Purchase price
233 $variation->update_meta_data( '_cost_price', $item['purchase_rate'] );
234
235 // Map taxes while syncing product from zoho.
236 if ( $item['tax_id'] && ! $this->is_tax_enabled() ) {
237 $zi_common_class = new CMBIRD_Common_Functions();
238 $woo_tax_class = $zi_common_class->get_tax_class_by_percentage( $item['tax_percentage'] );
239 $variation->set_tax_status( 'taxable' );
240 $variation->set_tax_class( $woo_tax_class );
241 }
242 // weight & dimensions
243 $variation->set_weight( $weight );
244 $variation->set_length( $length );
245 $variation->set_width( $width );
246 $variation->set_height( $height );
247
248 $variation->save(); // Save the data
249 } elseif ( 'publish' === $item_status ) {
250 $attribute_name11 = $item['attribute_option_name1'];
251 $attribute_name12 = $item['attribute_option_name2'];
252 $attribute_name13 = $item['attribute_option_name3'];
253 // Prepare the variation data
254 $attribute_arr = array();
255 if ( ! empty( $attribute_name11 ) ) {
256 $sanitized_name1 = wc_sanitize_taxonomy_name( $item['attribute_name1'] );
257 $attribute_arr[ $sanitized_name1 ] = $attribute_name11;
258 }
259 if ( ! empty( $attribute_name12 ) ) {
260 $sanitized_name2 = wc_sanitize_taxonomy_name( $item['attribute_name2'] );
261 $attribute_arr[ $sanitized_name2 ] = $attribute_name12;
262 }
263 if ( ! empty( $attribute_name13 ) ) {
264 $sanitized_name3 = wc_sanitize_taxonomy_name( $item['attribute_name3'] );
265 $attribute_arr[ $sanitized_name3 ] = $attribute_name13;
266 }
267
268 // here actually create new variation because sku not found
269 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
270 $variation = new WC_Product_Variation();
271 $variation->set_parent_id( $group_id );
272 $variation->set_status( 'publish' );
273 $variation->set_regular_price( $item_price );
274 $variation->set_sku( $item_sku );
275 $variation->set_weight( $weight );
276 $variation->set_length( $length );
277 $variation->set_width( $width );
278 $variation->set_height( $height );
279 if ( ! $zi_disable_stock_sync ) {
280 $variation->set_stock_quantity( $item_stock );
281 $variation->set_manage_stock( true );
282 $variation->set_stock_status( '' );
283 } else {
284 $variation->set_manage_stock( false );
285 }
286 // Map taxes while syncing product from zoho.
287 if ( $item['tax_id'] && ! $this->is_tax_enabled() ) {
288 $zi_common_class = new CMBIRD_Common_Functions();
289 $woo_tax_class = $zi_common_class->get_tax_class_by_percentage( $item['tax_percentage'] );
290 $variation->set_tax_status( 'taxable' );
291 $variation->set_tax_class( $woo_tax_class );
292 }
293 $variation->add_meta_data( 'zi_item_id', $item->item_id );
294 $variation_id = $variation->save();
295
296 // Get the variation attributes with correct attribute values
297 foreach ( $attribute_arr as $attribute => $term_name ) {
298 $taxonomy = $attribute;
299 // If taxonomy doesn't exists we create it
300 if ( ! taxonomy_exists( $taxonomy ) ) {
301 register_taxonomy(
302 $taxonomy,
303 'product_variation',
304 array(
305 'hierarchical' => false,
306 'label' => ucfirst( $attribute ),
307 'query_var' => true,
308 'rewrite' => array( 'slug' => sanitize_title( $attribute ) ),
309 ),
310 );
311 }
312
313 // Check if the Term name exist and if not we create it.
314 if ( ! term_exists( $term_name, $taxonomy ) ) {
315 wp_insert_term( $term_name, $taxonomy );
316 }
317
318 $term_slug = get_term_by( 'name', $term_name, $taxonomy )->slug;
319 // Get the post Terms names from the parent variable product.
320 $post_term_names = wp_get_post_terms( $group_id, $taxonomy, array( 'fields' => 'names' ) );
321 // Check if the post term exist and if not we set it in the parent variable product.
322 if ( ! in_array( $term_name, $post_term_names, true ) ) {
323 wp_set_post_terms( $group_id, $term_name, $taxonomy, true );
324 }
325 // Set/save the attribute data in the product variation
326 update_post_meta( $variation_id, 'attribute_' . $taxonomy, $term_slug );
327 }
328
329 // featured image
330 $zi_disable_itemimage_sync = get_option( 'cmbird_zoho_disable_image_sync_status' );
331 if ( ! empty( $item_image ) && ! $zi_disable_itemimage_sync ) {
332 $image_class = new CMBIRD_Image_ZI();
333 $image_class->cmbird_zi_get_image( $item_id, $item_name, $variation_id, $item_image );
334 }
335
336 update_post_meta( $variation_id, 'zi_item_id', $item_id );
337 // Sync variation data with parent variable product
338 WC_Product_Variable::sync( $group_id );
339 // Regenerate lookup table for attributes
340 $lookup_data_store = new LookupDataStore();
341 $lookup_data_store->create_data_for_product( $group_id );
342 // End group item add process
343 unset( $attribute_arr );
344 }
345 wc_delete_product_transients( $group_id ); // Clear/refresh cache
346 // end of grouped item creation
347 } else {
348 // fwrite($fd, PHP_EOL . 'Inside simple items');
349 // fwrite($fd, PHP_EOL . 'Item description Simple : ' . $item_description);
350 $row_item = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = 'zi_item_id' AND meta_value = %s", $item_id ) );
351 $mapped_product_id = $row_item->post_id;
352 // simple product
353 // fwrite($fd, PHP_EOL . 'Before Match check');
354 $pdt_id = '';
355 if ( ! empty( $mapped_product_id ) && null !== $mapped_product_id ) {
356 $product_found = wc_get_product( $mapped_product_id );
357 if ( ! $product_found ) {
358 // remove all postmeta of that product id.
359 $wpdb->delete( $wpdb->postmeta, array( 'post_id' => $mapped_product_id ) );
360 } else {
361 $pdt_id = $mapped_product_id;
362 }
363 } elseif ( empty( $item['is_combo_product'] ) ) {
364 // fwrite($fd, PHP_EOL . 'Inside create product');
365
366 // Check if Category is selected before creating simple item
367 if ( 'publish' === $item_status ) {
368 $opt_category = get_option( 'cmbird_zoho_item_category' );
369 $opt_category = maybe_unserialize( $opt_category );
370 $category_id = $item['category_id'];
371 if ( $opt_category ) {
372 if ( in_array( $category_id, $opt_category, true ) ) {
373 $product_class = new CMBIRD_Products_ZI_Export();
374 $pdt_id = $product_class->cmbird_zi_product_to_woocommerce( $item, $item_stock );
375 }
376 }
377 }
378 // fwrite($fd, PHP_EOL . 'After adding it : ' . $pdt_id);
379 }
380
381 // If there is product id then update metadata.
382 if ( ! empty( $pdt_id ) ) {
383 $simple_product = wc_get_product( $pdt_id );
384 // update the name if its allowed
385 $zi_disable_itemname_sync = get_option( 'cmbird_zoho_disable_name_sync_status' );
386 if ( ! $zi_disable_itemname_sync ) {
387 $simple_product->set_name( $item_name );
388 $slug = sanitize_title( $item_name );
389 $simple_product->set_slug( $slug );
390 }
391 // update the zi_item_id using the product instance
392 $simple_product->update_meta_data( 'zi_item_id', $item_id );
393 // update the status using set_status()
394 $simple_product->set_status( $item_status );
395 // Update the product SKU
396 $simple_product->set_sku( $item_sku );
397 // price
398 $sale_price = $simple_product->get_sale_price();
399 $simple_product->set_regular_price( $item_price );
400 if ( empty( $sale_price ) ) {
401 $simple_product->set_price( $item_price );
402 }
403 // Update Purchase price
404 $simple_product->update_meta_data( '_cost_price', $item['purchase_rate'] );
405 // description
406 $zi_disable_itemdescription_sync = get_option( 'cmbird_zoho_disable_description_sync_status' );
407 if ( ! empty( $item_description ) && ! $zi_disable_itemdescription_sync ) {
408 $simple_product->set_short_description( $item_description );
409 }
410 // Brand update if taxonomy product_brand(s) exists
411 if ( ! empty( $item_brand ) && taxonomy_exists( 'product_brand' ) ) {
412 wp_set_object_terms( $pdt_id, $item_brand, 'product_brand' );
413 } elseif ( ! empty( $item_brand ) && taxonomy_exists( 'product_brand' ) ) {
414 wp_set_object_terms( $pdt_id, $item_brand, 'product_brand' );
415 }
416 // stock
417 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
418 if ( ! $zi_disable_stock_sync ) {
419 // fwrite( $fd, PHP_EOL . 'Inside1' );
420 if ( 'NULL' !== gettype( $item_stock ) ) {
421 // fwrite( $fd, PHP_EOL . 'Inside1.1' );
422 // Set manage stock to yes
423 $simple_product->set_manage_stock( true );
424 // Update stock for simple product
425 $simple_product->set_stock_quantity( number_format( $item_stock, 0, '.', '' ) );
426 if ( $item_stock > 0 ) {
427 // fwrite( $fd, PHP_EOL . 'Inside2' );
428 // Update stock status
429 $simple_product->set_stock_status( 'instock' );
430 wp_set_post_terms( $pdt_id, 'instock', 'product_visibility', true );
431 } else {
432 // fwrite($fd, PHP_EOL . 'Inside3');
433 $stock_status = $simple_product->backorders_allowed() ? 'onbackorder' : 'outofstock';
434 $simple_product->set_stock_status( $stock_status );
435 wp_set_post_terms( $pdt_id, $stock_status, 'product_visibility', true );
436 }
437 }
438 }
439 // fwrite($fd, PHP_EOL . 'After stock');
440 // Update weight & dimensions of simple product
441 $simple_product->set_weight( $weight );
442 $simple_product->set_length( $length );
443 $simple_product->set_width( $width );
444 $simple_product->set_height( $height );
445
446 // featured image
447 $zi_disable_itemimage_sync = get_option( 'cmbird_zoho_disable_image_sync_status' );
448 if ( ! empty( $item_image ) && ! $zi_disable_itemimage_sync ) {
449 $image_class = new CMBIRD_Image_ZI();
450 $image_class->cmbird_zi_get_image( $item_id, $item_name, $pdt_id, $item_image );
451 }
452
453 // category
454 if ( ! empty( $item_category ) && empty( $group_name ) ) {
455 $term = get_term_by( 'name', $item_category, 'product_cat' );
456 $term_id = $term->term_id;
457 if ( empty( $term_id ) ) {
458 $term = wp_insert_term(
459 $item_category,
460 'product_cat',
461 array(
462 'parent' => 0,
463 )
464 );
465 $term_id = $term->term_id;
466 }
467 // Remove "uncategorized" category if assigned
468 $uncategorized_term = get_term_by( 'slug', 'uncategorized', 'product_cat' );
469 if ( $uncategorized_term && has_term( $uncategorized_term->term_id, 'product_cat', $pdt_id ) ) {
470 wp_remove_object_terms( $pdt_id, $uncategorized_term->term_id, 'product_cat' );
471 }
472 if ( ! is_wp_error( $term_id ) && isset( $term->term_id ) ) {
473 $existing_terms = wp_get_object_terms( $pdt_id, 'product_cat' );
474 if ( $existing_terms && count( $existing_terms ) > 0 ) {
475 $import_class = new CMBIRD_Products_ZI();
476 $is_term_exist = $import_class->zi_check_terms_exists( $existing_terms, $term_id );
477 if ( ! $is_term_exist ) {
478 $simple_product->update_meta_data( 'zi_category_id', $item['category_id'] );
479 wp_add_object_terms( $pdt_id, $term_id, 'product_cat' );
480 }
481 } else {
482 $simple_product->update_meta_data( 'zi_category_id', $item['category_id'] );
483 wp_set_object_terms( $pdt_id, $term_id, 'product_cat' );
484 }
485 }
486 }
487
488 // Update the custom fields if the custom fields are not empty
489 if ( ! empty( $custom_fields ) ) {
490 $import_class = new CMBIRD_Products_ZI();
491 $import_class->sync_item_custom_fields( $custom_fields, $pdt_id );
492 }
493
494 // Map taxes while syncing product from zoho.
495 if ( $item['tax_id'] && ! $this->is_tax_enabled() ) {
496 $zi_common_class = new CMBIRD_Common_Functions();
497 $woo_tax_class = $zi_common_class->get_tax_class_by_percentage( $item['tax_percentage'] );
498 $simple_product->set_tax_status( 'taxable' );
499 $simple_product->set_tax_class( $woo_tax_class );
500 }
501 $simple_product->save();
502 wc_delete_product_transients( $pdt_id );
503 }
504 }
505 $response = new WP_REST_Response();
506 $response->set_data( 'success on variable product' );
507 $response->set_status( 200 );
508
509 // fclose( $fd ); // close logfile.
510
511 return $response;
512 }
513
514 /**
515 * @param $inventory_adjustment
516 * @param wpdb $wpdb
517 *
518 * @return WP_REST_Response
519 */
520 public function inventory_adjustment( $inventory_adjustment ): WP_REST_Response {
521 global $wpdb;
522 $item = $inventory_adjustment;
523 $line_items = $item['line_items'];
524 // get first item from line items array
525 $item_id = $line_items[0]['item_id'];
526 $adjusted_stock = $line_items[0]['quantity_adjusted'];
527
528 $row_item = $wpdb->get_row(
529 $wpdb->prepare(
530 "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = 'zi_item_id' AND meta_value = %s",
531 $item_id
532 )
533 );
534 $mapped_product_id = $row_item->post_id;
535
536 if ( ! empty( $mapped_product_id ) ) {
537 // stock
538 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
539 $product = wc_get_product( $mapped_product_id );
540 // Check if the product is in stock
541 if ( ! $zi_disable_stock_sync ) {
542 if ( $product->is_in_stock() ) {
543 // Get stock quantity
544 $stock_quantity = $product->get_stock_quantity();
545 $new_stock = $stock_quantity + $adjusted_stock;
546 $product->set_stock_quantity( $new_stock );
547 } else {
548 $product->set_stock_quantity( $adjusted_stock );
549 $product->set_stock_status( 'instock' );
550 $product->set_manage_stock( true );
551 }
552 $product->save();
553 }
554 }
555
556 $response = new WP_REST_Response();
557 $response->set_data( 'Inventory Adjustment successful' );
558 $response->set_status( 200 );
559
560 return $response;
561 }
562 }
563