PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.6
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.6
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 1 year ago
class-api-for-product-webhook.php
568 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 ( true === $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 ) {
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 // create object with the $group_id and the $groupid
180 $args = array(
181 'zi_group_id' => $groupid,
182 'group_id' => $group_id,
183 );
184 $cmbird_product_zi->import_variable_product_variations( $args );
185 }
186
187 // set the product status of the variable parent product
188 // $existing_parent_product->set_status( $item_status );
189 $existing_parent_product->save();
190 } else {
191 // Add in scheduler to create the Variable Product
192 $last_synced_page = get_option( 'cmbird_group_item_sync_page_cat_id_' . $category_id );
193 if ( ! intval( $last_synced_page ) ) {
194 $last_synced_page = 1;
195 }
196 $data = array(
197 'page' => $last_synced_page,
198 'category' => $category_id,
199 );
200 $existing_schedule = as_has_scheduled_action( 'import_group_items_cron', $data );
201 // Schedule the action if it doesn't exist.
202 if ( ! $existing_schedule ) {
203 as_schedule_single_action( time(), 'import_group_items_cron', $data );
204 }
205 }
206
207 $row_item = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key='zi_item_id' AND meta_value=%s", $item_id ) );
208 $variation_id = $row_item->post_id;
209 if ( $variation_id ) {
210 // updating existing variations
211 $variation = new WC_Product_Variation( $variation_id );
212 // Prices
213 if ( ! empty( $item_price ) ) {
214 $variation->set_price( $item_price );
215 }
216 $variation->set_regular_price( $item_price );
217 // Stock
218 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
219 if ( ! empty( $item_stock ) && ! $zi_disable_stock_sync ) {
220 // fwrite($fd, PHP_EOL . 'Stock is here:'. $item_stock);
221 $variation->set_stock_quantity( $item_stock );
222 $variation->set_manage_stock( true );
223 // $variation->set_stock_status('');
224 } else {
225 // fwrite($fd, PHP_EOL . 'Available Stock : false');
226 $variation->set_manage_stock( false );
227 }
228 // featured image
229 $zi_disable_itemimage_sync = get_option( 'cmbird_zoho_disable_image_sync_status' );
230 if ( ! empty( $item_image ) && ! $zi_disable_itemimage_sync ) {
231 // fwrite($fd, PHP_EOL . 'Sync Image' );
232 $image_class = new CMBIRD_Image_ZI();
233 $image_class->cmbird_zi_get_image( $item_id, $item_name, $variation_id, $item_image );
234 }
235 // Disable or enable the variation based on the item_status
236 $variation->set_status( $item_status );
237 // Update Purchase price
238 $variation->update_meta_data( '_cost_price', $item['purchase_rate'] );
239
240 // Map taxes while syncing product from zoho.
241 if ( $item['tax_id'] && ! $this->is_tax_enabled() ) {
242 $zi_common_class = new CMBIRD_Common_Functions();
243 $woo_tax_class = $zi_common_class->get_tax_class_by_percentage( $item['tax_percentage'] );
244 $variation->set_tax_status( 'taxable' );
245 $variation->set_tax_class( $woo_tax_class );
246 }
247 // weight & dimensions
248 $variation->set_weight( $weight );
249 $variation->set_length( $length );
250 $variation->set_width( $width );
251 $variation->set_height( $height );
252
253 $variation->save(); // Save the data
254 } elseif ( 'publish' === $item_status ) {
255 $attribute_name11 = $item['attribute_option_name1'];
256 $attribute_name12 = $item['attribute_option_name2'];
257 $attribute_name13 = $item['attribute_option_name3'];
258 // Prepare the variation data
259 $attribute_arr = array();
260 if ( ! empty( $attribute_name11 ) ) {
261 $sanitized_name1 = wc_sanitize_taxonomy_name( $item['attribute_name1'] );
262 $attribute_arr[ $sanitized_name1 ] = $attribute_name11;
263 }
264 if ( ! empty( $attribute_name12 ) ) {
265 $sanitized_name2 = wc_sanitize_taxonomy_name( $item['attribute_name2'] );
266 $attribute_arr[ $sanitized_name2 ] = $attribute_name12;
267 }
268 if ( ! empty( $attribute_name13 ) ) {
269 $sanitized_name3 = wc_sanitize_taxonomy_name( $item['attribute_name3'] );
270 $attribute_arr[ $sanitized_name3 ] = $attribute_name13;
271 }
272
273 // here actually create new variation because sku not found
274 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
275 $variation = new WC_Product_Variation();
276 $variation->set_parent_id( $group_id );
277 $variation->set_status( 'publish' );
278 $variation->set_regular_price( $item_price );
279 $variation->set_sku( $item_sku );
280 $variation->set_weight( $weight );
281 $variation->set_length( $length );
282 $variation->set_width( $width );
283 $variation->set_height( $height );
284 if ( ! $zi_disable_stock_sync ) {
285 $variation->set_stock_quantity( $item_stock );
286 $variation->set_manage_stock( true );
287 $variation->set_stock_status( '' );
288 } else {
289 $variation->set_manage_stock( false );
290 }
291 // Map taxes while syncing product from zoho.
292 if ( $item['tax_id'] && ! $this->is_tax_enabled() ) {
293 $zi_common_class = new CMBIRD_Common_Functions();
294 $woo_tax_class = $zi_common_class->get_tax_class_by_percentage( $item['tax_percentage'] );
295 $variation->set_tax_status( 'taxable' );
296 $variation->set_tax_class( $woo_tax_class );
297 }
298 $variation->add_meta_data( 'zi_item_id', $item->item_id );
299 $variation_id = $variation->save();
300
301 // Get the variation attributes with correct attribute values
302 foreach ( $attribute_arr as $attribute => $term_name ) {
303 $taxonomy = $attribute;
304 // If taxonomy doesn't exists we create it
305 if ( ! taxonomy_exists( $taxonomy ) ) {
306 register_taxonomy(
307 $taxonomy,
308 'product_variation',
309 array(
310 'hierarchical' => false,
311 'label' => ucfirst( $attribute ),
312 'query_var' => true,
313 'rewrite' => array( 'slug' => sanitize_title( $attribute ) ),
314 ),
315 );
316 }
317
318 // Check if the Term name exist and if not we create it.
319 if ( ! term_exists( $term_name, $taxonomy ) ) {
320 wp_insert_term( $term_name, $taxonomy );
321 }
322
323 $term_slug = get_term_by( 'name', $term_name, $taxonomy )->slug;
324 // Get the post Terms names from the parent variable product.
325 $post_term_names = wp_get_post_terms( $group_id, $taxonomy, array( 'fields' => 'names' ) );
326 // Check if the post term exist and if not we set it in the parent variable product.
327 if ( ! in_array( $term_name, $post_term_names, true ) ) {
328 wp_set_post_terms( $group_id, $term_name, $taxonomy, true );
329 }
330 // Set/save the attribute data in the product variation
331 update_post_meta( $variation_id, 'attribute_' . $taxonomy, $term_slug );
332 }
333
334 // featured image
335 $zi_disable_itemimage_sync = get_option( 'cmbird_zoho_disable_image_sync_status' );
336 if ( ! empty( $item_image ) && ! $zi_disable_itemimage_sync ) {
337 $image_class = new CMBIRD_Image_ZI();
338 $image_class->cmbird_zi_get_image( $item_id, $item_name, $variation_id, $item_image );
339 }
340
341 update_post_meta( $variation_id, 'zi_item_id', $item_id );
342 // Sync variation data with parent variable product
343 WC_Product_Variable::sync( $group_id );
344 // Regenerate lookup table for attributes
345 $lookup_data_store = new LookupDataStore();
346 $lookup_data_store->create_data_for_product( $group_id );
347 // End group item add process
348 unset( $attribute_arr );
349 }
350 wc_delete_product_transients( $group_id ); // Clear/refresh cache
351 // end of grouped item creation
352 } else {
353 // fwrite($fd, PHP_EOL . 'Inside simple items');
354 // fwrite($fd, PHP_EOL . 'Item description Simple : ' . $item_description);
355 $row_item = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = 'zi_item_id' AND meta_value = %s", $item_id ) );
356 $mapped_product_id = $row_item->post_id;
357 // simple product
358 // fwrite($fd, PHP_EOL . 'Before Match check');
359 $pdt_id = '';
360 if ( ! empty( $mapped_product_id ) && null !== $mapped_product_id ) {
361 $product_found = wc_get_product( $mapped_product_id );
362 if ( ! $product_found ) {
363 // remove all postmeta of that product id.
364 $wpdb->delete( $wpdb->postmeta, array( 'post_id' => $mapped_product_id ) );
365 } else {
366 $pdt_id = $mapped_product_id;
367 }
368 } elseif ( empty( $item['is_combo_product'] ) ) {
369 // fwrite($fd, PHP_EOL . 'Inside create product');
370
371 // Check if Category is selected before creating simple item
372 if ( 'publish' === $item_status ) {
373 $opt_category = get_option( 'cmbird_zoho_item_category' );
374 $opt_category = maybe_unserialize( $opt_category );
375 $category_id = $item['category_id'];
376 if ( $opt_category ) {
377 if ( in_array( $category_id, $opt_category, true ) ) {
378 $product_class = new CMBIRD_Products_ZI_Export();
379 $pdt_id = $product_class->cmbird_zi_product_to_woocommerce( $item, $item_stock );
380 }
381 }
382 }
383 // fwrite($fd, PHP_EOL . 'After adding it : ' . $pdt_id);
384 }
385
386 // If there is product id then update metadata.
387 if ( ! empty( $pdt_id ) ) {
388 $simple_product = wc_get_product( $pdt_id );
389 // update the name if its allowed
390 $zi_disable_itemname_sync = get_option( 'cmbird_zoho_disable_name_sync_status' );
391 if ( ! $zi_disable_itemname_sync ) {
392 $simple_product->set_name( $item_name );
393 $slug = sanitize_title( $item_name );
394 $simple_product->set_slug( $slug );
395 }
396 // update the zi_item_id using the product instance
397 $simple_product->update_meta_data( 'zi_item_id', $item_id );
398 // update the status using set_status()
399 $simple_product->set_status( $item_status );
400 // Update the product SKU
401 $simple_product->set_sku( $item_sku );
402 // price
403 $sale_price = $simple_product->get_sale_price();
404 $simple_product->set_regular_price( $item_price );
405 if ( empty( $sale_price ) ) {
406 $simple_product->set_price( $item_price );
407 }
408 // Update Purchase price
409 $simple_product->update_meta_data( '_cost_price', $item['purchase_rate'] );
410 // description
411 $zi_disable_itemdescription_sync = get_option( 'cmbird_zoho_disable_description_sync_status' );
412 if ( ! empty( $item_description ) && ! $zi_disable_itemdescription_sync ) {
413 $simple_product->set_short_description( $item_description );
414 }
415 // Brand update if taxonomy product_brand(s) exists
416 if ( ! empty( $item_brand ) && taxonomy_exists( 'product_brand' ) ) {
417 wp_set_object_terms( $pdt_id, $item_brand, 'product_brand' );
418 } elseif ( ! empty( $item_brand ) && taxonomy_exists( 'product_brand' ) ) {
419 wp_set_object_terms( $pdt_id, $item_brand, 'product_brand' );
420 }
421 // stock
422 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
423 if ( ! $zi_disable_stock_sync ) {
424 // fwrite( $fd, PHP_EOL . 'Inside1' );
425 if ( 'NULL' !== gettype( $item_stock ) ) {
426 // fwrite( $fd, PHP_EOL . 'Inside1.1' );
427 // Set manage stock to yes
428 $simple_product->set_manage_stock( true );
429 // Update stock for simple product
430 $simple_product->set_stock_quantity( number_format( $item_stock, 0, '.', '' ) );
431 if ( $item_stock > 0 ) {
432 // fwrite( $fd, PHP_EOL . 'Inside2' );
433 // Update stock status
434 $simple_product->set_stock_status( 'instock' );
435 wp_set_post_terms( $pdt_id, 'instock', 'product_visibility', true );
436 } else {
437 // fwrite($fd, PHP_EOL . 'Inside3');
438 $stock_status = $simple_product->backorders_allowed() ? 'onbackorder' : 'outofstock';
439 $simple_product->set_stock_status( $stock_status );
440 wp_set_post_terms( $pdt_id, $stock_status, 'product_visibility', true );
441 }
442 }
443 }
444 // fwrite($fd, PHP_EOL . 'After stock');
445 // Update weight & dimensions of simple product
446 $simple_product->set_weight( $weight );
447 $simple_product->set_length( $length );
448 $simple_product->set_width( $width );
449 $simple_product->set_height( $height );
450
451 // featured image
452 $zi_disable_itemimage_sync = get_option( 'cmbird_zoho_disable_image_sync_status' );
453 if ( ! empty( $item_image ) && ! $zi_disable_itemimage_sync ) {
454 $image_class = new CMBIRD_Image_ZI();
455 $image_class->cmbird_zi_get_image( $item_id, $item_name, $pdt_id, $item_image );
456 }
457
458 // category
459 if ( ! empty( $item_category ) && empty( $group_name ) ) {
460 $term = get_term_by( 'name', $item_category, 'product_cat' );
461 $term_id = $term->term_id;
462 if ( empty( $term_id ) ) {
463 $term = wp_insert_term(
464 $item_category,
465 'product_cat',
466 array(
467 'parent' => 0,
468 )
469 );
470 $term_id = $term->term_id;
471 }
472 // Remove "uncategorized" category if assigned
473 $uncategorized_term = get_term_by( 'slug', 'uncategorized', 'product_cat' );
474 if ( $uncategorized_term && has_term( $uncategorized_term->term_id, 'product_cat', $pdt_id ) ) {
475 wp_remove_object_terms( $pdt_id, $uncategorized_term->term_id, 'product_cat' );
476 }
477 if ( ! is_wp_error( $term_id ) && isset( $term->term_id ) ) {
478 $existing_terms = wp_get_object_terms( $pdt_id, 'product_cat' );
479 if ( $existing_terms && count( $existing_terms ) > 0 ) {
480 $import_class = new CMBIRD_Products_ZI();
481 $is_term_exist = $import_class->zi_check_terms_exists( $existing_terms, $term_id );
482 if ( ! $is_term_exist ) {
483 $simple_product->update_meta_data( 'zi_category_id', $item['category_id'] );
484 wp_add_object_terms( $pdt_id, $term_id, 'product_cat' );
485 }
486 } else {
487 $simple_product->update_meta_data( 'zi_category_id', $item['category_id'] );
488 wp_set_object_terms( $pdt_id, $term_id, 'product_cat' );
489 }
490 }
491 }
492
493 // Update the custom fields if the custom fields are not empty
494 if ( ! empty( $custom_fields ) ) {
495 $import_class = new CMBIRD_Products_ZI();
496 $import_class->sync_item_custom_fields( $custom_fields, $pdt_id );
497 }
498
499 // Map taxes while syncing product from zoho.
500 if ( $item['tax_id'] && ! $this->is_tax_enabled() ) {
501 $zi_common_class = new CMBIRD_Common_Functions();
502 $woo_tax_class = $zi_common_class->get_tax_class_by_percentage( $item['tax_percentage'] );
503 $simple_product->set_tax_status( 'taxable' );
504 $simple_product->set_tax_class( $woo_tax_class );
505 }
506 $simple_product->save();
507 wc_delete_product_transients( $pdt_id );
508 }
509 }
510 $response = new WP_REST_Response();
511 $response->set_data( 'success on variable product' );
512 $response->set_status( 200 );
513
514 // fclose( $fd ); // close logfile.
515
516 return $response;
517 }
518
519 /**
520 * @param $inventory_adjustment
521 * @param wpdb $wpdb
522 *
523 * @return WP_REST_Response
524 */
525 public function inventory_adjustment( $inventory_adjustment ): WP_REST_Response {
526 global $wpdb;
527 $item = $inventory_adjustment;
528 $line_items = $item['line_items'];
529 // get first item from line items array
530 $item_id = $line_items[0]['item_id'];
531 $adjusted_stock = $line_items[0]['quantity_adjusted'];
532
533 $row_item = $wpdb->get_row(
534 $wpdb->prepare(
535 "SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = 'zi_item_id' AND meta_value = %s",
536 $item_id
537 )
538 );
539 $mapped_product_id = $row_item->post_id;
540
541 if ( ! empty( $mapped_product_id ) ) {
542 // stock
543 $zi_disable_stock_sync = get_option( 'cmbird_zoho_disable_stock_sync_status' );
544 $product = wc_get_product( $mapped_product_id );
545 // Check if the product is in stock
546 if ( ! $zi_disable_stock_sync ) {
547 if ( $product->is_in_stock() ) {
548 // Get stock quantity
549 $stock_quantity = $product->get_stock_quantity();
550 $new_stock = $stock_quantity + $adjusted_stock;
551 $product->set_stock_quantity( $new_stock );
552 } else {
553 $product->set_stock_quantity( $adjusted_stock );
554 $product->set_stock_status( 'instock' );
555 $product->set_manage_stock( true );
556 }
557 $product->save();
558 }
559 }
560
561 $response = new WP_REST_Response();
562 $response->set_data( 'Inventory Adjustment successful' );
563 $response->set_status( 200 );
564
565 return $response;
566 }
567 }
568