PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.7.5
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.7.5
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 4 months ago
order-backend.php
322 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\SettingsAjax;
19
20 /**
21 * Loading admin order sync script.
22 */
23 function cmbird_load_script() {
24 if ( is_admin() ) {
25 $screen = get_current_screen();
26 if ( $screen->id === 'product' || $screen->id === 'shop_order' || $screen->id === 'woocommerce_page_wc-orders' ) {
27 wp_enqueue_script( 'zoho-admin-order-ajax-script', CMBIRD_URL . 'admin/js/zoho_admin_order_ajax.js', array( 'jquery' ), CMBIRD_VERSION, true );
28 wp_register_script( 'sweatAlert', CMBIRD_URL . 'admin/js/sweetalert.min.js', array( 'jquery' ), CMBIRD_VERSION, true );
29 wp_enqueue_script( 'sweatAlert' );
30 }
31 }
32 }
33 add_action( 'admin_enqueue_scripts', 'cmbird_load_script' );
34
35 function cmbird_zoho_admin_metabox() {
36 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
37 $zoho_crm_access_token = get_option( 'cmbird_zoho_crm_access_token' );
38 if ( empty( $zoho_inventory_access_token ) && empty( $zoho_crm_access_token ) ) {
39 return;
40 }
41 $screen = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled()
42 ? 'woocommerce_page_wc-orders'
43 : 'shop_order';
44
45 add_meta_box(
46 'zoho-admin-sync',
47 'Sync Order to Zoho',
48 'cmbird_admin_metabox_callback',
49 $screen,
50 'side',
51 'high'
52 );
53 }
54 add_action( 'add_meta_boxes', 'cmbird_zoho_admin_metabox' );
55
56 function cmbird_admin_metabox_callback( $post_or_order_object ) {
57 $subscription = new SettingsAjax();
58 $data = $subscription->get_subscription_data();
59 $status = $data['status'];
60 // Flag to check if 'ZohoInventory' is found.
61 $found = false;
62 // Loop through fee_lines and check for the 'name' key.
63 if ( isset( $data['fee_lines'] ) && is_array( $data['fee_lines'] ) ) {
64 foreach ( $data['fee_lines'] as $fee_line ) {
65 if ( isset( $fee_line['name'] ) && ( $fee_line['name'] === 'ZohoInventory' || $fee_line['name'] === 'ZohoCRM' ) ) {
66 $found = true;
67 break;
68 }
69 }
70 }
71 $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() );
72 $userid = $order->get_user_id();
73 $order_id = $order->get_id();
74 if ( $found && 'active' === $status ) {
75 $nonce_order = wp_create_nonce( 'zoho_admin_order_sync' );
76 echo '<a href="javascript:void(0)" style="width:100%; text-align: center;"
77 class="button save_order button-primary" onclick="zoho_admin_order_ajax(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce_order ) . '\')">Sync Order</a>';
78 if ( $userid ) {
79 $nonce = wp_create_nonce( 'zi_customer_unmap_hook' );
80 echo '<br><p style="color:red;">Click on below button if you are seeing the error "Billing AddressID passed is invalid"</p>';
81 echo '<a href="javascript:void(0)" style="width:100%; text-align: center;"
82 class="button customer_unmap" onclick="zoho_admin_customer_unmap(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce ) . '\')">Unmap Customer</a>';
83 }
84 } else {
85 echo '<p style="color:red;">Please activate the Zoho Inventory Integration</p>';
86 }
87 }
88
89 /**
90 * Bulk-action to sync orders from WooCommerce to Zoho
91 *
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 *
143 * @param: $payload
144 * @param: $resource
145 * @param: $resource_id
146 * @param: $id
147 * @return: $payload
148 */
149 add_filter( 'woocommerce_webhook_payload', 'cmbird_modify_order_webhook_payload', 10, 4 );
150 function cmbird_modify_order_webhook_payload( $payload, $resource, $resource_id, $id ) {
151 add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
152 // Get the webhook object using the ID.
153 $webhook = wc_get_webhook( $id );
154
155 // if webhook name contains 'CommerceBird Customers' using strpos, then add the customer_id to the payload.
156 $customers_webhook_name = 'CommerceBird Customers';
157 $webhook_name = $webhook->get_name();
158 if ( $webhook && strpos( $webhook_name, $customers_webhook_name ) !== false ) {
159 // include eo_gl_account and eo_account_id in the payload.
160 $customer_id = $resource_id;
161 $eo_gl_account = get_user_meta( $customer_id, 'eo_gl_account', true );
162 $eo_account_id = get_user_meta( $customer_id, 'eo_account_id', true );
163 $eo_contact_id = get_user_meta( $customer_id, 'eo_contact_id', true );
164 if ( ! empty( $eo_gl_account ) ) {
165 $payload['meta_data'][] = array(
166 'key' => 'eo_gl_account',
167 'value' => $eo_gl_account,
168 );
169 }
170 if ( ! empty( $eo_account_id ) ) {
171 $payload['meta_data'][] = array(
172 'key' => 'eo_account_id',
173 'value' => $eo_account_id,
174 );
175 }
176 if ( ! empty( $eo_contact_id ) ) {
177 $payload['meta_data'][] = array(
178 'key' => 'eo_contact_id',
179 'value' => $eo_contact_id,
180 );
181 }
182 return $payload;
183 }
184
185 if ( $webhook && 'CommerceBird Orders' !== $webhook_name ) {
186 return $payload;
187 }
188
189 $eo_account_id = '';
190 $customer_id = isset( $payload['customer_id'] ) ? (int) $payload['customer_id'] : 0;
191
192 // All guest users will have the customer_id field set to 0.
193 if ( $customer_id > 0 ) {
194 $eo_account_id = (string) get_user_meta( $customer_id, 'eo_account_id', true );
195 if ( ! empty( $eo_account_id ) ) {
196 $payload['meta_data'][] = array(
197 'key' => 'eo_account_id',
198 'value' => $eo_account_id,
199 );
200 }
201 }
202 // add eo_order_id to the meta_data array.
203 $order_object = wc_get_order( $resource_id );
204 $eo_order_id = $order_object->get_meta( 'eo_order_id', true );
205 if ( ! empty( $eo_order_id ) ) {
206 $payload['meta_data'][] = array(
207 'key' => 'eo_order_id',
208 'value' => $eo_order_id,
209 );
210 }
211 // do same for eo_invoice_id.
212 $eo_invoice_id = $order_object->get_meta( 'eo_invoice_id', true );
213 if ( ! empty( $eo_invoice_id ) ) {
214 $payload['meta_data'][] = array(
215 'key' => 'eo_invoice_id',
216 'value' => $eo_invoice_id,
217 );
218 }
219 // Loop through line items in and add the eo_item_id to the line item.
220 if ( isset( $payload['line_items'] ) && is_array( $payload['line_items'] ) ) {
221 foreach ( $payload['line_items'] as &$item ) {
222 // Get the product ID associated with the line item.
223 $product_id = $item['product_id'];
224 $variation_id = $item['variation_id'];
225 // Get the product meta value based on the product ID and meta key.
226 if ( $variation_id ) {
227 $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true );
228 } else {
229 $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true );
230 }
231 // Add the product meta to the line item.
232 if ( ! empty( $eo_item_id ) ) {
233 $item['meta'][] = array(
234 'key' => 'eo_item_id',
235 'value' => $eo_item_id,
236 );
237 }
238 }
239 }
240 remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );
241 return $payload;
242 }
243
244 /**
245 * Prevent Webhook delivery execution when order gets updated too often per minute
246 *
247 * @param bool $should_deliver
248 * @param WC_Webhook $webhook
249 * @param array $arg
250 * @return bool
251 */
252 add_filter( 'woocommerce_webhook_should_deliver', 'cmbird_skip_webhook_delivery', 10, 3 );
253 function cmbird_skip_webhook_delivery( $should_deliver, $webhook, $arg ) {
254
255 $cmbird_orders = 'CommerceBird Orders';
256 if ( $webhook->get_name() === $cmbird_orders ) {
257 $order = wc_get_order( $arg );
258 // check if order status is failed, pending, on-hold or cancelled.
259 $order_status = $order->get_status();
260 if ( in_array( $order_status, array( 'failed', 'pending', 'on-hold', 'cancelled', 'auto-draft' ) ) ) {
261 $should_deliver = false;
262 }
263 // check if order contains meta data for eo_order_id.
264 $eo_order_id = $order->get_meta( 'eo_order_id', true );
265 if ( ! empty( $eo_order_id ) && 'refunded' !== $order_status ) {
266 $should_deliver = false;
267 }
268 // check if order status is processing and webhook status is disabled or paused.
269 $webhook_status = $webhook->get_status();
270 // also return false if webhoook status is disabled or paused.
271 if ( 'disabled' === $webhook_status || 'paused' === $webhook_status ) {
272 $should_deliver = false;
273 }
274 // Check if the order is older than 4 months, return false if so.
275 $current_date = new DateTime();
276 $order_date = $order->get_date_created();
277
278 if ( $order_date instanceof WC_DateTime ) {
279 $two_months_ago = $current_date->modify( '-4 months' );
280 if ( $order_date < $two_months_ago ) {
281 $should_deliver = false;
282 }
283 }
284 }
285 $cmbird_customers = 'CommerceBird Customers Update';
286 if ( $webhook->get_name() === $cmbird_customers ) {
287 $customer_id = $arg['customer_id'];
288 $last_order_id = wc_get_customer_last_order( $customer_id );
289 // if last order created date is less than 5 minutes, then return false.
290 if ( $last_order_id ) {
291 $last_order = wc_get_order( $last_order_id );
292 $last_order_date = $last_order->get_date_created();
293 $current_date = new DateTime();
294 $interval = $current_date->diff( $last_order_date );
295 $minutes = $interval->i;
296 if ( $minutes < 5 ) {
297 $should_deliver = false;
298 }
299 }
300 }
301
302 return $should_deliver; // Continue with normal webhook delivery.
303 }
304
305 /**
306 * Update Customer Meta Data when Order is updated
307 *
308 * @param $order_id
309 */
310 function cmbird_update_customer_meta( $order_id ) {
311 $order = wc_get_order( $order_id );
312 $customer_id = $order->get_user_id();
313 $eo_gl_account = $order->get_meta( 'glaccount', true );
314 if ( ! empty( $eo_gl_account ) ) {
315 // get the value of the glaccount meta, which is everything before : in the value.
316 $string = $eo_gl_account;
317 $value = strstr( $string, ':', true );
318 update_user_meta( $customer_id, 'eo_gl_account', trim( $value ) );
319 }
320 }
321 add_action( 'woocommerce_update_order', 'cmbird_update_customer_meta' );
322