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