PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.4.3
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.4.3
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 / sync / order-backend.php
commercebird / includes / sync Last commit date
index.php 1 year ago order-backend.php 11 months ago
order-backend.php
323 lines
1 <?php
2
3 /**
4 * All backend order sync related functions.
5 *
6 * @category Fulfillment
7 * @package commercebird
8 * @author Roadmap Studios <info@roadmapstudios.com>
9 * @license GNU General Public License v3.0
10 * @link https://commercebird.com
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
18 // use CommerceBird\Admin\Actions\Ajax\ZohoInventoryAjax;
19 use CommerceBird\Admin\Actions\Ajax\SettingsAjax;
20
21 /**
22 * Loading admin order sync script.
23 */
24 function cmbird_load_script() {
25 if ( is_admin() ) {
26 $screen = get_current_screen();
27 if ( $screen->id === 'product' || $screen->id === 'shop_order' || $screen->id === 'woocommerce_page_wc-orders' ) {
28 wp_enqueue_script( 'zoho-admin-order-ajax-script', CMBIRD_URL . 'admin/js/zoho_admin_order_ajax.js', array( 'jquery' ), CMBIRD_VERSION, true );
29 wp_register_script( 'sweatAlert', CMBIRD_URL . 'admin/js/sweetalert.min.js', array( 'jquery' ), CMBIRD_VERSION, true );
30 wp_enqueue_script( 'sweatAlert' );
31 }
32 }
33 }
34 add_action( 'admin_enqueue_scripts', 'cmbird_load_script' );
35
36 function cmbird_zoho_admin_metabox() {
37 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
38 $zoho_crm_access_token = get_option( 'cmbird_zoho_crm_access_token' );
39 if ( empty( $zoho_inventory_access_token ) && empty( $zoho_crm_access_token ) ) {
40 return;
41 }
42 $screen = wc_get_container()->get( CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()
43 ? 'woocommerce_page_wc-orders'
44 : 'shop_order';
45
46 add_meta_box(
47 'zoho-admin-sync',
48 'Sync Order to Zoho',
49 'cmbird_admin_metabox_callback',
50 $screen,
51 'side',
52 'high'
53 );
54 }
55 add_action( 'add_meta_boxes', 'cmbird_zoho_admin_metabox' );
56
57 function cmbird_admin_metabox_callback( $post_or_order_object ) {
58 $subscription = new SettingsAjax();
59 $data = $subscription->get_subscription_data();
60 $status = $data['status'];
61 // Flag to check if 'ZohoInventory' is found
62 $found = false;
63 // Loop through fee_lines and check for the 'name' key
64 if ( isset( $data['fee_lines'] ) && is_array( $data['fee_lines'] ) ) {
65 foreach ( $data['fee_lines'] as $fee_line ) {
66 if ( isset( $fee_line['name'] ) && ( $fee_line['name'] === 'ZohoInventory' || $fee_line['name'] === 'ZohoCRM' ) ) {
67 $found = true;
68 break;
69 }
70 }
71 }
72 $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : wc_get_order( $post_or_order_object->get_id() );
73 $userid = $order->get_user_id();
74 $order_id = $order->get_id();
75 if ( $found && 'active' === $status ) {
76 $nonce_order = wp_create_nonce( 'zoho_admin_order_sync' );
77 echo '<a href="javascript:void(0)" style="width:100%; text-align: center;"
78 class="button save_order button-primary" onclick="zoho_admin_order_ajax(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce_order ) . '\')">Sync Order</a>';
79 if ( $userid ) {
80 $nonce = wp_create_nonce( 'zi_customer_unmap_hook' );
81 echo '<br><p style="color:red;">Click on below button if you are seeing the error "Billing AddressID passed is invalid"</p>';
82 echo '<a href="javascript:void(0)" style="width:100%; text-align: center;"
83 class="button customer_unmap" onclick="zoho_admin_customer_unmap(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce ) . '\')">Unmap Customer</a>';
84 }
85 } else {
86 echo '<p style="color:red;">Please activate the Zoho Inventory Integration</p>';
87 }
88 }
89
90 /**
91 * Bulk-action to sync orders from WooCommerce to Zoho
92 * @param: $bulk_array
93 */
94 add_filter( 'bulk_actions-woocommerce_page_wc-orders', 'cmbird_zi_sync_all_orders_to_zoho', 10, 1 );
95 function cmbird_zi_sync_all_orders_to_zoho( $actions ) {
96 $actions['sync_order_to_zoho'] = __( 'Sync to Zoho', 'commercebird' );
97 return $actions;
98 }
99
100 add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', 'cmbird_zi_sync_all_orders_to_zoho_handler', 10, 3 );
101 function cmbird_zi_sync_all_orders_to_zoho_handler( $redirect, $action, $object_ids ) {
102 if ( 'sync_order_to_zoho' !== $action ) {
103 return $redirect; // Exit
104 }
105 // let's remove query args first
106 $redirect = remove_query_arg( 'sync_order_to_zoho_done', $redirect );
107
108 // do something for "Make Draft" bulk action
109 if ( 'sync_order_to_zoho' === $action ) {
110
111 foreach ( $object_ids as $post_id ) {
112 $order_sync = new CMBIRD_Order_Sync_ZI();
113 $order_sync->zi_order_sync( $post_id );
114 // Also sync to Zoho CRM
115 $zoho_crm_sync = new CMBIRD_ZCRM_SalesOrder();
116 $zoho_crm_sync->cmbird_zcrm_order_sync( $post_id );
117 }
118
119 // do not forget to add query args to URL because we will show notices later
120 $redirect = add_query_arg( 'sync_order_to_zoho_done', count( $object_ids ), $redirect );
121 }
122
123 return $redirect;
124 }
125
126 // output the message of bulk action
127 add_action( 'admin_notices', 'cmbird_sync_order_to_zoho_notices' );
128 function cmbird_sync_order_to_zoho_notices() {
129 // verify nonce
130 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'bulk-orders' ) ) {
131 return;
132 }
133 if ( ! empty( $_REQUEST['sync_order_to_zoho_done'] ) ) {
134 echo '<div id="message" class="updated notice is-dismissible">
135 <p>Orders Synced. If order is not synced, please click on Edit Order to see the API response.</p>
136 </div>';
137 }
138 }
139
140 /**
141 * Add the product meta as line item meta to the Order Webhook Payload
142 * @param: $payload
143 * @param: $resource
144 * @param: $resource_id
145 * @param: $id
146 * @return: $payload
147 */
148 add_filter( 'woocommerce_webhook_payload', 'cmbird_modify_order_webhook_payload', 10, 4 );
149 function cmbird_modify_order_webhook_payload( $payload, $resource, $resource_id, $id ) {
150 $webhook = wc_get_webhook( $id );
151
152 // if webhook name contains 'CommerceBird Customers' using strpos, then add the customer_id to the payload
153 $customers_webhook_name = 'CommerceBird Customers';
154 $webhook_name = $webhook->get_name();
155 if ( $webhook && strpos( $webhook_name, $customers_webhook_name ) !== false ) {
156 // include eo_gl_account and eo_account_id in the payload
157 $customer_id = (int) $payload['id'];
158 $eo_gl_account = get_user_meta( $customer_id, 'eo_gl_account', true );
159 $eo_account_id = get_user_meta( $customer_id, 'eo_account_id', true );
160 $eo_contact_id = get_user_meta( $customer_id, 'eo_contact_id', true );
161 if ( ! empty( $eo_gl_account ) ) {
162 $payload['meta_data'][] = array(
163 'key' => 'eo_gl_account',
164 'value' => $eo_gl_account,
165 );
166 }
167 if ( ! empty( $eo_account_id ) ) {
168 $payload['meta_data'][] = array(
169 'key' => 'eo_account_id',
170 'value' => $eo_account_id,
171 );
172 }
173 if ( ! empty( $eo_contact_id ) ) {
174 $payload['meta_data'][] = array(
175 'key' => 'eo_contact_id',
176 'value' => $eo_contact_id,
177 );
178 }
179 return $payload;
180 }
181
182 if ( $webhook && 'CommerceBird Orders' !== $webhook_name ) {
183 return $payload;
184 }
185
186 $eo_account_id = '';
187 $customer_id = isset( $payload['customer_id'] ) ? (int) $payload['customer_id'] : 0;
188
189 // All guest users will have the customer_id field set to 0
190 if ( $customer_id > 0 ) {
191 $eo_account_id = (string) get_user_meta( $customer_id, 'eo_account_id', true );
192 if ( ! empty( $eo_account_id ) ) {
193 $payload['meta_data'][] = array(
194 'key' => 'eo_account_id',
195 'value' => $eo_account_id,
196 );
197 }
198 }
199 // add eo_order_id to the meta_data array
200 $order_object = wc_get_order( $resource_id );
201 $eo_order_id = $order_object->get_meta( 'eo_order_id', true );
202 if ( ! empty( $eo_order_id ) ) {
203 $payload['meta_data'][] = array(
204 'key' => 'eo_order_id',
205 'value' => $eo_order_id,
206 );
207 }
208 // do same for eo_invoice_id
209 $eo_invoice_id = $order_object->get_meta( 'eo_invoice_id', true );
210 if ( ! empty( $eo_invoice_id ) ) {
211 $payload['meta_data'][] = array(
212 'key' => 'eo_invoice_id',
213 'value' => $eo_invoice_id,
214 );
215 }
216 // Loop through line items in and add the eo_item_id to the line item
217 if ( isset( $payload['line_items'] ) && is_array( $payload['line_items'] ) ) {
218 foreach ( $payload['line_items'] as &$item ) {
219 // Get the product ID associated with the line item
220 $product_id = $item['product_id'];
221 $variation_id = $item['variation_id'];
222 // Get the product meta value based on the product ID and meta key
223 if ( $variation_id ) {
224 $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true );
225 } else {
226 $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true );
227 }
228 // Add the product meta to the line item
229 if ( ! empty( $eo_item_id ) ) {
230 $item['meta'][] = array(
231 'key' => 'eo_item_id',
232 'value' => $eo_item_id,
233 );
234 }
235 }
236 }
237
238 return $payload;
239 }
240
241 /**
242 * Prevent Webhook delivery execution when order gets updated too often per minute
243 * @param bool $should_deliver
244 * @param WC_Webhook $webhook
245 * @param array $arg
246 * @return bool
247 */
248 add_filter( 'woocommerce_webhook_should_deliver', 'cmbird_skip_webhook_delivery', 10, 3 );
249 function cmbird_skip_webhook_delivery( $should_deliver, $webhook, $arg ) {
250
251 $cmbird_orders = 'CommerceBird Orders';
252 if ( $webhook->get_name() === $cmbird_orders ) {
253 $order = wc_get_order( $arg );
254 // check if order status is failed, pending, on-hold or cancelled
255 $order_status = $order->get_status();
256 if ( in_array( $order_status, array( 'failed', 'pending', 'on-hold', 'cancelled', 'auto-draft' ) ) ) {
257 $should_deliver = false;
258 }
259 // check if order contains meta data for eo_order_id
260 $eo_order_id = $order->get_meta( 'eo_order_id', true );
261 if ( ! empty( $eo_order_id ) && 'refunded' !== $order_status ) {
262 $should_deliver = false;
263 }
264 // check if order status is processing and webhook status is disabled or paused
265 $webhook_status = $webhook->get_status();
266 // also return false if webhoook status is disabled or paused
267 if ( $webhook_status === 'disabled' || $webhook_status === 'paused' ) {
268 $should_deliver = false;
269 }
270 // Check if the order is older than 2 months, return false if so
271 // first check if site_url is perfecthealth.nl
272 $site_url = get_site_url();
273 if ( strpos( $site_url, 'perfecthealth.nl' ) === false ) {
274 return $should_deliver;
275 }
276 $current_date = new DateTime();
277 $order_date = $order->get_date_created();
278
279 if ( $order_date instanceof WC_DateTime ) {
280 $two_months_ago = $current_date->modify( '-4 months' );
281 if ( $order_date < $two_months_ago ) {
282 $should_deliver = false;
283 }
284 }
285 }
286 $cmbird_customers = 'CommerceBird Customers Update';
287 if ( $webhook->get_name() === $cmbird_customers ) {
288 $customer_id = $arg['customer_id'];
289 $last_order_id = wc_get_customer_last_order( $customer_id );
290 // if last order created date is less than 5 minutes, then return false
291 if ( $last_order_id ) {
292 $last_order = wc_get_order( $last_order_id );
293 $last_order_date = $last_order->get_date_created();
294 $current_date = new DateTime();
295 $interval = $current_date->diff( $last_order_date );
296 $minutes = $interval->i;
297 if ( $minutes < 5 ) {
298 $should_deliver = false;
299 }
300 }
301 }
302
303 return $should_deliver; // Continue with normal webhook delivery
304 }
305
306 /**
307 * Update Customer Meta Data when Order is updated
308 * @param $order_id
309 *
310 */
311 function cmbird_update_customer_meta( $order_id ) {
312 $order = wc_get_order( $order_id );
313 $customer_id = $order->get_user_id();
314 $eo_gl_account = $order->get_meta( 'glaccount', true );
315 if ( ! empty( $eo_gl_account ) ) {
316 // get the value of the glaccount meta, which is everything before : in the value
317 $string = $eo_gl_account;
318 $value = strstr( $string, ':', true );
319 update_user_meta( $customer_id, 'eo_gl_account', trim( $value ) );
320 }
321 }
322 add_action( 'woocommerce_update_order', 'cmbird_update_customer_meta' );
323