PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.2.19
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.2.19
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
312 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 && $webhook_name !== 'CommerceBird Orders' ) {
182 return $payload;
183 }
184
185 $eo_account_id = '';
186 $customer_id = (int) $payload['customer_id'];
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 // Loop through line items in and add the eo_item_id to the line item
208 foreach ( $payload['line_items'] as &$item ) {
209 // Get the product ID associated with the line item
210 $product_id = $item['product_id'];
211 $variation_id = $item['variation_id'];
212 // Get the product meta value based on the product ID and meta key
213 if ( $variation_id ) {
214 $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true );
215 } else {
216 $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true );
217 }
218 // Add the product meta to the line item
219 if ( ! empty( $eo_item_id ) ) {
220 $item['meta'][] = array(
221 'key' => 'eo_item_id',
222 'value' => $eo_item_id,
223 );
224 }
225 }
226
227 return $payload;
228 }
229
230 /**
231 * Prevent Webhook delivery execution when order gets updated too often per minute
232 * @param bool $should_deliver
233 * @param WC_Webhook $webhook
234 * @param array $arg
235 * @return bool
236 */
237 add_filter( 'woocommerce_webhook_should_deliver', 'cmbird_skip_webhook_delivery', 10, 3 );
238 function cmbird_skip_webhook_delivery( $should_deliver, $webhook, $arg ) {
239
240 $cmbird_orders = 'CommerceBird Orders';
241 if ( $webhook->get_name() === $cmbird_orders ) {
242 $order = wc_get_order( $arg );
243 // check if order status is failed, pending, on-hold or cancelled
244 $order_status = $order->get_status();
245 if ( in_array( $order_status, array( 'failed', 'pending', 'on-hold', 'cancelled', 'auto-draft' ) ) ) {
246 $should_deliver = false;
247 }
248 // check if order contains meta data for eo_order_id
249 $eo_order_id = $order->get_meta( 'eo_order_id', true );
250 if ( ! empty( $eo_order_id ) && 'refunded' !== $order_status ) {
251 $should_deliver = false;
252 }
253 // check if order status is processing and webhook status is disabled or paused
254 $webhook_status = $webhook->get_status();
255 // also return false if webhoook status is disabled or paused
256 if ( $webhook_status === 'disabled' || $webhook_status === 'paused' ) {
257 $should_deliver = false;
258 }
259 // Check if the order is older than 2 months, return false if so
260 // first check if site_url is perfecthealth.nl
261 $site_url = get_site_url();
262 if ( strpos( $site_url, 'perfecthealth.nl' ) === false ) {
263 return $should_deliver;
264 }
265 $current_date = new DateTime();
266 $order_date = $order->get_date_created();
267
268 if ( $order_date instanceof WC_DateTime ) {
269 $two_months_ago = $current_date->modify( '-6 months' );
270 if ( $order_date < $two_months_ago ) {
271 $should_deliver = false;
272 }
273 }
274 }
275 $cmbird_customers = 'CommerceBird Customers Update';
276 if ( $webhook->get_name() === $cmbird_customers ) {
277 $customer_id = $arg['customer_id'];
278 $last_order_id = wc_get_customer_last_order( $customer_id );
279 // if last order created date is less than 5 minutes, then return false
280 if ( $last_order_id ) {
281 $last_order = wc_get_order( $last_order_id );
282 $last_order_date = $last_order->get_date_created();
283 $current_date = new DateTime();
284 $interval = $current_date->diff( $last_order_date );
285 $minutes = $interval->i;
286 if ( $minutes < 5 ) {
287 $should_deliver = false;
288 }
289 }
290 }
291
292 return $should_deliver; // Continue with normal webhook delivery
293 }
294
295 /**
296 * Update Customer Meta Data when Order is updated
297 * @param $order_id
298 *
299 */
300 function cmbird_update_customer_meta( $order_id ) {
301 $order = wc_get_order( $order_id );
302 $customer_id = $order->get_user_id();
303 $eo_gl_account = $order->get_meta( 'glaccount', true );
304 if ( ! empty( $eo_gl_account ) ) {
305 // get the value of the glaccount meta, which is everything before : in the value
306 $string = $eo_gl_account;
307 $value = strstr( $string, ':', true );
308 update_user_meta( $customer_id, 'eo_gl_account', trim( $value ) );
309 }
310 }
311 add_action( 'woocommerce_update_order', 'cmbird_update_customer_meta' );
312