PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.6.0
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.6.0
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 9 months ago
order-backend.php
325 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 $webhook = wc_get_webhook( $id );
152
153 // if webhook name contains 'CommerceBird Customers' using strpos, then add the customer_id to the payload.
154 $customers_webhook_name = 'CommerceBird Customers';
155 $webhook_name = $webhook->get_name();
156 if ( $webhook && strpos( $webhook_name, $customers_webhook_name ) !== false ) {
157 // include eo_gl_account and eo_account_id in the payload.
158 $customer_id = (int) $payload['id'];
159 $eo_gl_account = get_user_meta( $customer_id, 'eo_gl_account', true );
160 $eo_account_id = get_user_meta( $customer_id, 'eo_account_id', true );
161 $eo_contact_id = get_user_meta( $customer_id, 'eo_contact_id', true );
162 if ( ! empty( $eo_gl_account ) ) {
163 $payload['meta_data'][] = array(
164 'key' => 'eo_gl_account',
165 'value' => $eo_gl_account,
166 );
167 }
168 if ( ! empty( $eo_account_id ) ) {
169 $payload['meta_data'][] = array(
170 'key' => 'eo_account_id',
171 'value' => $eo_account_id,
172 );
173 }
174 if ( ! empty( $eo_contact_id ) ) {
175 $payload['meta_data'][] = array(
176 'key' => 'eo_contact_id',
177 'value' => $eo_contact_id,
178 );
179 }
180 return $payload;
181 }
182
183 if ( $webhook && 'CommerceBird Orders' !== $webhook_name ) {
184 return $payload;
185 }
186
187 $eo_account_id = '';
188 $customer_id = isset( $payload['customer_id'] ) ? (int) $payload['customer_id'] : 0;
189
190 // All guest users will have the customer_id field set to 0.
191 if ( $customer_id > 0 ) {
192 $eo_account_id = (string) get_user_meta( $customer_id, 'eo_account_id', true );
193 if ( ! empty( $eo_account_id ) ) {
194 $payload['meta_data'][] = array(
195 'key' => 'eo_account_id',
196 'value' => $eo_account_id,
197 );
198 }
199 }
200 // add eo_order_id to the meta_data array.
201 $order_object = wc_get_order( $resource_id );
202 $eo_order_id = $order_object->get_meta( 'eo_order_id', true );
203 if ( ! empty( $eo_order_id ) ) {
204 $payload['meta_data'][] = array(
205 'key' => 'eo_order_id',
206 'value' => $eo_order_id,
207 );
208 }
209 // do same for eo_invoice_id.
210 $eo_invoice_id = $order_object->get_meta( 'eo_invoice_id', true );
211 if ( ! empty( $eo_invoice_id ) ) {
212 $payload['meta_data'][] = array(
213 'key' => 'eo_invoice_id',
214 'value' => $eo_invoice_id,
215 );
216 }
217 // Loop through line items in and add the eo_item_id to the line item.
218 if ( isset( $payload['line_items'] ) && is_array( $payload['line_items'] ) ) {
219 foreach ( $payload['line_items'] as &$item ) {
220 // Get the product ID associated with the line item.
221 $product_id = $item['product_id'];
222 $variation_id = $item['variation_id'];
223 // Get the product meta value based on the product ID and meta key.
224 if ( $variation_id ) {
225 $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true );
226 } else {
227 $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true );
228 }
229 // Add the product meta to the line item.
230 if ( ! empty( $eo_item_id ) ) {
231 $item['meta'][] = array(
232 'key' => 'eo_item_id',
233 'value' => $eo_item_id,
234 );
235 }
236 }
237 }
238
239 return $payload;
240 }
241
242 /**
243 * Prevent Webhook delivery execution when order gets updated too often per minute
244 *
245 * @param bool $should_deliver
246 * @param WC_Webhook $webhook
247 * @param array $arg
248 * @return bool
249 */
250 add_filter( 'woocommerce_webhook_should_deliver', 'cmbird_skip_webhook_delivery', 10, 3 );
251 function cmbird_skip_webhook_delivery( $should_deliver, $webhook, $arg ) {
252
253 $cmbird_orders = 'CommerceBird Orders';
254 if ( $webhook->get_name() === $cmbird_orders ) {
255 $order = wc_get_order( $arg );
256 // check if order status is failed, pending, on-hold or cancelled.
257 $order_status = $order->get_status();
258 if ( in_array( $order_status, array( 'failed', 'pending', 'on-hold', 'cancelled', 'auto-draft' ) ) ) {
259 $should_deliver = false;
260 }
261 // check if order contains meta data for eo_order_id.
262 $eo_order_id = $order->get_meta( 'eo_order_id', true );
263 if ( ! empty( $eo_order_id ) && 'refunded' !== $order_status ) {
264 $should_deliver = false;
265 }
266 // check if order status is processing and webhook status is disabled or paused.
267 $webhook_status = $webhook->get_status();
268 // also return false if webhoook status is disabled or paused.
269 if ( 'disabled' === $webhook_status || 'paused' === $webhook_status ) {
270 $should_deliver = false;
271 }
272 // Check if the order is older than 2 months, return false if so.
273 // first check if site_url is perfecthealth.nl.
274 $site_url = get_site_url();
275 if ( strpos( $site_url, 'perfecthealth.nl' ) === false ) {
276 return $should_deliver;
277 }
278 $current_date = new DateTime();
279 $order_date = $order->get_date_created();
280
281 if ( $order_date instanceof WC_DateTime ) {
282 $two_months_ago = $current_date->modify( '-4 months' );
283 if ( $order_date < $two_months_ago ) {
284 $should_deliver = false;
285 }
286 }
287 }
288 $cmbird_customers = 'CommerceBird Customers Update';
289 if ( $webhook->get_name() === $cmbird_customers ) {
290 $customer_id = $arg['customer_id'];
291 $last_order_id = wc_get_customer_last_order( $customer_id );
292 // if last order created date is less than 5 minutes, then return false.
293 if ( $last_order_id ) {
294 $last_order = wc_get_order( $last_order_id );
295 $last_order_date = $last_order->get_date_created();
296 $current_date = new DateTime();
297 $interval = $current_date->diff( $last_order_date );
298 $minutes = $interval->i;
299 if ( $minutes < 5 ) {
300 $should_deliver = false;
301 }
302 }
303 }
304
305 return $should_deliver; // Continue with normal webhook delivery.
306 }
307
308 /**
309 * Update Customer Meta Data when Order is updated
310 *
311 * @param $order_id
312 */
313 function cmbird_update_customer_meta( $order_id ) {
314 $order = wc_get_order( $order_id );
315 $customer_id = $order->get_user_id();
316 $eo_gl_account = $order->get_meta( 'glaccount', true );
317 if ( ! empty( $eo_gl_account ) ) {
318 // get the value of the glaccount meta, which is everything before : in the value.
319 $string = $eo_gl_account;
320 $value = strstr( $string, ':', true );
321 update_user_meta( $customer_id, 'eo_gl_account', trim( $value ) );
322 }
323 }
324 add_action( 'woocommerce_update_order', 'cmbird_update_customer_meta' );
325