PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.8
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.8
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 1 year 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\ZohoInventoryAjax;
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 if ( empty( $zoho_inventory_access_token ) ) {
38 return;
39 }
40 $screen = wc_get_container()->get( CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()
41 ? 'woocommerce_page_wc-orders'
42 : 'shop_order';
43
44 add_meta_box(
45 'zoho-admin-sync',
46 'Sync Order to Zoho',
47 'cmbird_admin_metabox_callback',
48 $screen,
49 'side',
50 'high'
51 );
52 }
53 add_action( 'add_meta_boxes', 'cmbird_zoho_admin_metabox' );
54
55 function cmbird_admin_metabox_callback( $post_or_order_object ) {
56 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
57 if ( empty( $zoho_inventory_access_token ) ) {
58 return;
59 }
60 $subscription = new ZohoInventoryAjax();
61 $data = $subscription->get_subscription_data();
62 $status = $data['status'];
63 // Flag to check if 'ZohoInventory' is found
64 $found = false;
65 // Loop through fee_lines and check for the 'name' key
66 if ( isset( $data['fee_lines'] ) && is_array( $data['fee_lines'] ) ) {
67 foreach ( $data['fee_lines'] as $fee_line ) {
68 if ( isset( $fee_line['name'] ) && $fee_line['name'] === 'ZohoInventory' ) {
69 $found = true;
70 break;
71 }
72 }
73 }
74 $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() );
75 $userid = $order->get_user_id();
76 $order_id = $order->get_id();
77 if ( $found && 'active' === $status ) {
78 $nonce_order = wp_create_nonce( 'zoho_admin_order_sync' );
79 echo '<a href="javascript:void(0)" style="width:100%; text-align: center;"
80 class="button save_order button-primary" onclick="zoho_admin_order_ajax(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce_order ) . '\')">Sync Order</a>';
81 if ( $userid ) {
82 $nonce = wp_create_nonce( 'zi_customer_unmap_hook' );
83 echo '<br><p style="color:red;">Click on below button if you are seeing the error "Billing AddressID passed is invalid"</p>';
84 echo '<a href="javascript:void(0)" style="width:100%; text-align: center;"
85 class="button customer_unmap" onclick="zoho_admin_customer_unmap(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce ) . '\')">Unmap Customer</a>';
86 }
87 } else {
88 echo '<p style="color:red;">Please activate the Zoho Inventory Integration</p>';
89 }
90 }
91
92 /**
93 * Bulk-action to sync orders from WooCommerce to Zoho
94 * @param: $bulk_array
95 */
96 add_filter( 'bulk_actions-woocommerce_page_wc-orders', 'cmbird_zi_sync_all_orders_to_zoho', 10, 1 );
97 function cmbird_zi_sync_all_orders_to_zoho( $actions ) {
98 $actions['sync_order_to_zoho'] = __( 'Sync to Zoho', 'commercebird' );
99 return $actions;
100 }
101
102 add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', 'cmbird_zi_sync_all_orders_to_zoho_handler', 10, 3 );
103 function cmbird_zi_sync_all_orders_to_zoho_handler( $redirect, $action, $object_ids ) {
104 if ( 'sync_order_to_zoho' !== $action ) {
105 return $redirect; // Exit
106 }
107 // let's remove query args first
108 $redirect = remove_query_arg( 'sync_order_to_zoho_done', $redirect );
109
110 // do something for "Make Draft" bulk action
111 if ( 'sync_order_to_zoho' === $action ) {
112
113 foreach ( $object_ids as $post_id ) {
114 $order_sync = new CMBIRD_Order_Sync_ZI();
115 $order_sync->zi_order_sync( $post_id );
116 }
117
118 // do not forget to add query args to URL because we will show notices later
119 $redirect = add_query_arg( 'sync_order_to_zoho_done', count( $object_ids ), $redirect );
120 }
121
122 return $redirect;
123 }
124
125 // output the message of bulk action
126 add_action( 'admin_notices', 'cmbird_sync_order_to_zoho_notices' );
127 function cmbird_sync_order_to_zoho_notices() {
128 // verify nonce
129 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'bulk-orders' ) ) {
130 return;
131 }
132 if ( ! empty( $_REQUEST['sync_order_to_zoho_done'] ) ) {
133 echo '<div id="message" class="updated notice is-dismissible">
134 <p>Orders Synced. If order is not synced, please click on Edit Order to see the API response.</p>
135 </div>';
136 }
137 }
138
139 /**
140 * Add the product meta as line item meta to the Order Webhook Payload
141 * @param: $payload
142 * @param: $resource
143 * @param: $resource_id
144 * @param: $id
145 * @return: $payload
146 */
147 add_filter( 'woocommerce_webhook_payload', 'cmbird_modify_order_webhook_payload', 10, 4 );
148 function cmbird_modify_order_webhook_payload( $payload, $resource, $resource_id, $id ) {
149 $webhook = wc_get_webhook( $id );
150
151 // if webhook name contains 'CommerceBird Customers' using strpos, then add the customer_id to the payload
152 $customers_webhook_name = 'CommerceBird Customers';
153 $webhook_name = $webhook->get_name();
154 if ( $webhook && strpos( $webhook_name, $customers_webhook_name ) !== false ) {
155 // include eo_gl_account and eo_account_id in the payload
156 $customer_id = (int) $payload['id'];
157 $eo_gl_account = get_user_meta( $customer_id, 'eo_gl_account', true );
158 $eo_account_id = get_user_meta( $customer_id, 'eo_account_id', true );
159 $eo_contact_id = get_user_meta( $customer_id, 'eo_contact_id', true );
160 if ( ! empty( $eo_gl_account ) ) {
161 $payload['meta_data'][] = array(
162 'key' => 'eo_gl_account',
163 'value' => $eo_gl_account,
164 );
165 }
166 if ( ! empty( $eo_account_id ) ) {
167 $payload['meta_data'][] = array(
168 'key' => 'eo_account_id',
169 'value' => $eo_account_id,
170 );
171 }
172 if ( ! empty( $eo_contact_id ) ) {
173 $payload['meta_data'][] = array(
174 'key' => 'eo_contact_id',
175 'value' => $eo_contact_id,
176 );
177 }
178 return $payload;
179 }
180
181 if ( $webhook && 'CommerceBird Orders' !== $webhook_name ) {
182 return $payload;
183 }
184
185 $eo_account_id = '';
186 $customer_id = isset( $payload['customer_id'] ) ? (int) $payload['customer_id'] : 0;
187
188 // All guest users will have the customer_id field set to 0
189 if ( $customer_id > 0 ) {
190 $eo_account_id = (string) get_user_meta( $customer_id, 'eo_account_id', true );
191 if ( ! empty( $eo_account_id ) ) {
192 $payload['meta_data'][] = array(
193 'key' => 'eo_account_id',
194 'value' => $eo_account_id,
195 );
196 }
197 }
198 // add eo_order_id to the meta_data array
199 $order_object = wc_get_order( $resource_id );
200 $eo_order_id = $order_object->get_meta( 'eo_order_id', true );
201 if ( ! empty( $eo_order_id ) ) {
202 $payload['meta_data'][] = array(
203 'key' => 'eo_order_id',
204 'value' => $eo_order_id,
205 );
206 }
207 // do same for eo_invoice_id
208 $eo_invoice_id = $order_object->get_meta( 'eo_invoice_id', true );
209 if ( ! empty( $eo_invoice_id ) ) {
210 $payload['meta_data'][] = array(
211 'key' => 'eo_invoice_id',
212 'value' => $eo_invoice_id,
213 );
214 }
215 // Loop through line items in and add the eo_item_id to the line item
216 if ( isset( $payload['line_items'] ) && is_array( $payload['line_items'] ) ) {
217 foreach ( $payload['line_items'] as &$item ) {
218 // Get the product ID associated with the line item
219 $product_id = $item['product_id'];
220 $variation_id = $item['variation_id'];
221 // Get the product meta value based on the product ID and meta key
222 if ( $variation_id ) {
223 $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true );
224 } else {
225 $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true );
226 }
227 // Add the product meta to the line item
228 if ( ! empty( $eo_item_id ) ) {
229 $item['meta'][] = array(
230 'key' => 'eo_item_id',
231 'value' => $eo_item_id,
232 );
233 }
234 }
235 }
236
237 return $payload;
238 }
239
240 /**
241 * Prevent Webhook delivery execution when order gets updated too often per minute
242 * @param bool $should_deliver
243 * @param WC_Webhook $webhook
244 * @param array $arg
245 * @return bool
246 */
247 add_filter( 'woocommerce_webhook_should_deliver', 'cmbird_skip_webhook_delivery', 10, 3 );
248 function cmbird_skip_webhook_delivery( $should_deliver, $webhook, $arg ) {
249
250 $cmbird_orders = 'CommerceBird Orders';
251 if ( $webhook->get_name() === $cmbird_orders ) {
252 $order = wc_get_order( $arg );
253 // check if order status is failed, pending, on-hold or cancelled
254 $order_status = $order->get_status();
255 if ( in_array( $order_status, array( 'failed', 'pending', 'on-hold', 'cancelled', 'auto-draft' ) ) ) {
256 $should_deliver = false;
257 }
258 // check if order contains meta data for eo_order_id
259 $eo_order_id = $order->get_meta( 'eo_order_id', true );
260 if ( ! empty( $eo_order_id ) && 'refunded' !== $order_status ) {
261 $should_deliver = false;
262 }
263 // check if order status is processing and webhook status is disabled or paused
264 $webhook_status = $webhook->get_status();
265 // also return false if webhoook status is disabled or paused
266 if ( $webhook_status === 'disabled' || $webhook_status === 'paused' ) {
267 $should_deliver = false;
268 }
269 // Check if the order is older than 2 months, return false if so
270 // first check if site_url is perfecthealth.nl
271 $site_url = get_site_url();
272 if ( strpos( $site_url, 'perfecthealth.nl' ) === false ) {
273 return $should_deliver;
274 }
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 * @param $order_id
308 *
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