PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / trunk
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). vtrunk
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 / admin / includes / Actions / Sync / ZohoInventorySync.php
commercebird / admin / includes / Actions / Sync Last commit date
ExactOnlineSync.php 1 month ago ZohoCRMSync.php 5 months ago ZohoInventorySync.php 2 months ago index.php 1 year ago
ZohoInventorySync.php
104 lines
1 <?php
2
3 namespace CommerceBird\Admin\Actions\Sync;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Handles synchronization of products between WooCommerce and Zoho Inventory.
11 */
12 class ZohoInventorySync {
13
14 /**
15 * Sync data from Zoho Inventory.
16 *
17 * @param string $type product.
18 * @param array $data to sync from Zoho Inventory.
19 * @param bool $import import or update.
20 * @return mixed
21 */
22 public static function sync( string $type, array $data, bool $import = false ) {
23 if ( empty( $type ) ) {
24 return false;
25 }
26 if ( $import ) {
27 self::import( $type, $data );
28 } else {
29 self::import( $type, $data, true );
30 }
31 }
32
33 /**
34 * Import data from Zoho Inventory using WooCommerce REST API batch endpoint.
35 *
36 * For product data will be like:
37 * {
38 * "item_id": string,
39 * "sku": string,
40 * "name": string,
41 * "description": string,
42 * "rate": float,
43 * "status": string,
44 * "category_name": string,
45 * "brand": string,
46 * "image_document_id": string,
47 * "image_name": string
48 * }
49 *
50 * @param string $type of provided data.
51 * @param array $data of import.
52 * @param bool $update whether to update existing items.
53 * @param string $endpoint API endpoint to use for import.
54 * @return mixed
55 */
56 public static function import( string $type, array $data, bool $update = false, $endpoint = '' ) {
57 $payload = array();
58 // if $update is true, we will use update action.
59 $action = $update ? 'update' : 'create';
60
61 switch ( $type ) {
62 case 'product':
63 $endpoint = ! empty( $endpoint ) ? $endpoint : '/wc/v3/products/batch';
64 $payload = array(
65 $action => $data,
66 );
67 break;
68 case 'variations':
69 // if endpoint is not provided, then return false as we don't know which product to update.
70 $endpoint = ! empty( $endpoint ) ? $endpoint : '';
71 $payload = $data;
72 break;
73 default:
74 return false;
75 }
76
77 if ( empty( $payload[ $action ] ) ) {
78 return false;
79 }
80
81 add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
82 add_filter( 'wc_product_has_unique_sku', '__return_false' );
83 $request = new \WP_REST_Request( 'POST', $endpoint );
84 $request->set_body_params( $payload );
85 $response = rest_do_request( $request );
86 remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );
87 remove_filter( 'wc_product_has_unique_sku', '__return_false' );
88
89 if ( 200 !== $response->get_status() ) {
90 $logger = wc_get_logger();
91 $logger->error(
92 sprintf(
93 'WC REST Batch Response - Status: %d, Message: %s',
94 $response->get_status(),
95 $response->get_data()['message'] ?? wp_json_encode( $response->get_data() )
96 ),
97 array( 'source' => 'cmbird-zoho-inventory-items-sync' )
98 );
99 }
100
101 return $response;
102 }
103 }
104