PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.3
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.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 / woo-functions.php
commercebird / includes Last commit date
classes 1 year ago sync 1 year ago index.php 1 year ago woo-functions.php 1 year ago
woo-functions.php
619 lines
1 <?php
2
3 /**
4 * All WooCommerce related functions.
5 *
6 * @category WooCommerce
7 * @package CommerceBird
8 * @author Fawad Tiemoerie <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 /**
18 * Helper functions to ensure correct handling of Data being transferred via rest api
19 */
20
21 function cmbird_clear_product_cache( $object, $request, $is_creating ) {
22 if ( ! $is_creating ) {
23 $product_id = $object->get_id();
24 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
25 $cmbird_zi_product_sync = get_option( 'cmbird_zoho_disable_product_sync_status' );
26 if ( ! empty( $zoho_inventory_access_token ) && ! $cmbird_zi_product_sync ) {
27 $product_handler = new CMBIRD_Products_ZI_Export();
28 $product_handler->cmbird_zi_product_sync( $product_id );
29 }
30 wc_delete_product_transients( $product_id );
31 }
32 }
33 add_action( 'woocommerce_rest_insert_product_object', 'cmbird_clear_product_cache', 10, 3 );
34
35
36
37 /**
38 * Function to update the Contact in Zoho when customer updates address on frontend
39 * @param $user_id
40 */
41 function cmbird_update_contact_via_accountpage( $user_id ) {
42 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
43 if ( ! empty( $zoho_inventory_access_token ) ) {
44 $contact_class_handle = new CMBIRD_Contact_ZI();
45 $contact_class_handle->cmbird_contact_update_function( $user_id );
46
47 global $wpdb;
48 $meta_key_search = 'zi_contact_person_id%';
49 $query = $wpdb->get_results(
50 $wpdb->prepare(
51 "SELECT meta_key, meta_value
52 FROM {$wpdb->usermeta}
53 WHERE user_id = %d
54 AND meta_key LIKE %s",
55 $user_id,
56 $meta_key_search
57 )
58 );
59 if ( ! empty( $query ) ) {
60 // Usermeta keys containing 'zi_contact_person_id' exist.
61 foreach ( $query as $row ) {
62 $contact_class_handle->cmbird_update_contact_person( $user_id, $row->meta_value );
63 }
64 } else {
65 return;
66 }
67 } else {
68 return;
69 }
70 }
71 add_action( 'profile_update', 'cmbird_update_contact_via_accountpage' );
72
73 /**
74 * Function to be called by hook when new product is added in WooCommerce.
75 *
76 * @param $product_id
77 * @return void
78 */
79 add_action( 'woocommerce_update_product', 'cmbird_zi_product_sync_class', 10, 1 );
80 add_action( 'wp_ajax_zoho_admin_product_sync', 'cmbird_zi_product_sync_class' );
81 function cmbird_zi_product_sync_class( $product_id ) {
82 if ( ! is_admin() ) {
83 return;
84 }
85 if ( ! $product_id ) {
86 // Check nonce for security
87 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zoho_admin_product_sync' ) ) {
88 wp_send_json_error( 'Nonce verification failed' );
89 } else {
90 $product_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
91 if ( ! $product_id ) {
92 wp_send_json_error( 'Invalid Product ID' );
93 }
94 }
95 }
96 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
97 // return if zoho inventory access token is empty
98 if ( empty( $zoho_inventory_access_token ) ) {
99 return;
100 }
101 $cmbird_zi_product_sync = get_option( 'cmbird_zoho_disable_product_sync_status' );
102 if ( ! $cmbird_zi_product_sync ) {
103 $product_handler = new CMBIRD_Products_ZI_Export();
104 $product_handler->cmbird_zi_product_sync( $product_id );
105 }
106 // if its variable product but without variations, then sync it.
107 $product = wc_get_product( $product_id );
108 if ( $product->is_type( 'variable' ) ) {
109 $variations = $product->get_available_variations();
110 if ( isset( $variations ) && count( $variations ) === 0 ) {
111 $zi_product_id = get_post_meta( $product_id, 'zi_item_id', true );
112 if ( ! empty( $zi_product_id ) ) {
113 $product_handler = new CMBIRD_Products_ZI();
114 $product_handler->import_variable_product_variations( $zi_product_id, $product_id );
115 }
116 }
117 }
118 }
119
120 /**
121 * Bulk-action to sync products from WooCommerce to Zoho
122 *
123 * @param: $bulk_array
124 * @return: $bulk_array
125 */
126 add_filter( 'bulk_actions-edit-product', 'cmbird_zi_sync_all_items_to_zoho' );
127 function cmbird_zi_sync_all_items_to_zoho( $bulk_array ) {
128 $bulk_array['sync_item_to_zoho'] = 'Sync to Zoho';
129 return $bulk_array;
130 }
131
132 add_filter( 'handle_bulk_actions-edit-product', 'cmbird_zi_sync_all_items_to_zoho_handler', 10, 3 );
133 function cmbird_zi_sync_all_items_to_zoho_handler( $redirect, $action, $object_ids ) {
134 // let's remove query args first
135 $redirect = remove_query_arg( 'sync_item_to_zoho_done', $redirect );
136
137 // do something for "Make Draft" bulk action
138 if ( 'sync_item_to_zoho' === $action ) {
139
140 foreach ( $object_ids as $post_id ) {
141 $product_handler = new CMBIRD_Products_ZI_Export();
142 $product_handler->cmbird_zi_product_sync( $post_id );
143 }
144
145 // do not forget to add query args to URL because we will show notices later
146 $redirect = add_query_arg( 'sync_item_to_zoho_done', count( $object_ids ), $redirect );
147
148 }
149
150 return $redirect;
151 }
152
153 // output the message of bulk action
154 add_action( 'admin_notices', 'cmbird_sync_item_to_zoho_notices' );
155 function cmbird_sync_item_to_zoho_notices() {
156 // verify nonce
157 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-edit-products' ) ) {
158 return;
159 }
160 if ( ! empty( $_REQUEST['sync_item_to_zoho_done'] ) ) {
161 echo '<div id="message" class="updated notice is-dismissible">
162 <p>Products Synced. If product is not synced, please click on Edit Product to see the API response.</p>
163 </div>';
164 }
165 }
166
167 /**
168 * Function to be called by ajax hook when unmap button called.
169 * This function remove zoho mapped id.
170 */
171 add_action( 'wp_ajax_zi_product_unmap_hook', 'cmbird_zi_product_unmap_hook' );
172 function cmbird_zi_product_unmap_hook( $product_id ) {
173 // verify Nonce
174 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zi_product_unmap_hook' ) ) {
175 wp_send_json_error( 'Nonce verification failed' );
176 }
177 if ( ! $product_id && isset( $_POST['product_id'] ) ) {
178 $product_id = sanitize_text_field( wp_unslash( $_POST['product_id'] ) );
179 }
180
181 if ( $product_id ) {
182 $product = wc_get_product( $product_id );
183 // If this is variable items then unmap all of it's variations.
184 if ( $product->is_type( 'variable' ) ) {
185 $variations = $product->get_available_variations();
186 if ( isset( $variations ) && count( $variations ) > 0 ) {
187 foreach ( $variations as $child ) {
188 delete_post_meta( $child['variation_id'], 'zi_item_id' );
189 delete_post_meta( $child['variation_id'], 'zi_account_id' );
190 delete_post_meta( $child['variation_id'], 'zi_account_name' );
191 delete_post_meta( $child['variation_id'], 'zi_category_id' );
192 delete_post_meta( $child['variation_id'], 'zi_inventory_account_id' );
193 delete_post_meta( $child['variation_id'], 'zi_purchase_account_id' );
194 }
195 }
196 }
197 delete_post_meta( $product_id, 'zi_item_id' );
198 delete_post_meta( $product_id, 'zi_account_id' );
199 delete_post_meta( $product_id, 'zi_account_name' );
200 delete_post_meta( $product_id, 'zi_category_id' );
201 delete_post_meta( $product_id, 'zi_inventory_account_id' );
202 delete_post_meta( $product_id, 'zi_purchase_account_id' );
203 // update message
204 update_post_meta( $product_id, 'zi_product_errmsg', 'Product is Unmapped' );
205 }
206 }
207
208 /**
209 * Function to be called by ajax hook when unmap button called.
210 * This function remove zoho mapped id.
211 */
212 add_action( 'wp_ajax_zi_customer_unmap_hook', 'cmbird_zi_customer_unmap_hook' );
213 function cmbird_zi_customer_unmap_hook( $order_id ) {
214 // verify Nonce
215 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zi_customer_unmap_hook' ) ) {
216 wp_send_json_error( 'Nonce verification failed' );
217 }
218 if ( ! $order_id && isset( $_POST['order_id'] ) ) {
219 $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
220 }
221
222 $order = wc_get_order( $order_id );
223 $customer_id = $order->get_user_id();
224
225 if ( $customer_id ) {
226 delete_user_meta( $customer_id, 'zi_contact_id' );
227 delete_user_meta( $customer_id, 'zi_contact_persons_id' );
228 delete_user_meta( $customer_id, 'zi_contactperson_id_0' );
229 delete_user_meta( $customer_id, 'zi_contactperson_id_1' );
230 delete_user_meta( $customer_id, 'zi_currency_code' );
231 delete_user_meta( $customer_id, 'zi_currency_id' );
232 delete_user_meta( $customer_id, 'zi_created_time' );
233 delete_user_meta( $customer_id, 'zi_last_modified_time' );
234 delete_user_meta( $customer_id, 'zi_primary_contact_id' );
235 delete_user_meta( $customer_id, 'zi_billing_address_id' );
236 delete_user_meta( $customer_id, 'zi_shipping_address_id' );
237
238 $order->add_order_note( 'Zoho Sync: Customer is now unmapped. Please try syncing the order again' );
239 $order->save();
240 }
241 }
242
243 /**
244 * Add WordPress Meta box to show sync response
245 */
246 function cmbird_product_metabox() {
247 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
248 if ( ! $zoho_inventory_access_token ) {
249 return;
250 }
251 add_meta_box(
252 'zoho-product-sync',
253 __( 'Zoho Inventory', 'commercebird' ),
254 'cmbird_product_metabox_callback',
255 'product',
256 'side',
257 'high'
258 );
259 }
260 function cmbird_product_metabox_callback( $post ) {
261 $response = get_post_meta( $post->ID, 'zi_product_errmsg' );
262 echo 'API Response: ' . esc_html( implode( $response ) ) . '<br>';
263 // Generate nonce
264 $nonce = wp_create_nonce( 'zoho_admin_product_sync' );
265 $nonce_unmap = wp_create_nonce( 'zi_product_unmap_hook' );
266 $post_id = $post->ID;
267 echo '<br><a href="javascript:void(0)" style="width:100%; text-align: center;" class="button button-primary" onclick="zoho_admin_product_ajax(' . esc_attr( $post_id ) . ', \'' . esc_attr( $nonce ) . '\')">Sync Product</a>';
268 echo '<br><a href="javascript:void(0)" style="margin-top:10px; background:#b32d2e; border-color: #b32d2e; width:100%; text-align: center;" class="button button-primary" onclick="zoho_admin_unmap_product_ajax(' . esc_attr( $post_id ) . ', \'' . esc_attr( $nonce_unmap ) . '\')">Unmap this Product</a>';
269 $product = wc_get_product( $post->ID );
270 $product_type = $product->get_type();
271 if ( 'variable' === $product_type || 'variable-subscription' === $product_type ) {
272 echo '<p class="howto" style="color:#b32d2e;"><strong>Important : </strong> Please ensure all variations have price and SKU</p>';
273 }
274 // echo the zi_category_id
275 $zi_category_id = get_post_meta( $post->ID, 'zi_category_id', true );
276 if ( $zi_category_id ) {
277 echo '<p class="howto"><strong>Zoho Category: </strong>' . esc_html( $zi_category_id ) . '</p>';
278 }
279 // make api call to get the zoho item details
280 $zi_item_id = get_post_meta( $post->ID, 'zi_item_id', true );
281 if ( $zi_item_id && is_admin() ) {
282 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_organization_id' );
283 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
284 $urlitem = "{$zoho_inventory_url}inventory/v1/items/{$zi_item_id}?organization_id=$zoho_inventory_oid";
285 // fwrite( $fd, PHP_EOL . 'URL : ' . $urlitem );
286
287 $execute_curl_call = new CMBIRD_API_Handler_Zoho();
288 $json = $execute_curl_call->execute_curl_call_get( $urlitem );
289 $code = (int) property_exists( $json, 'code' ) ? $json->code : '0';
290 if ( '0' === $code || 0 === $code ) {
291 // echo the item details here that are in array called "item"
292 $item = $json->item;
293 $actual_available_stock = (int) property_exists( $item, 'actual_available_stock' ) ? $item->actual_available_stock : '0';
294 $rate = (int) property_exists( $item, 'rate' ) ? $item->rate : '0';
295 echo '<p class="howto"><strong>Rate: </strong>' . esc_html( $rate ) . '</p>';
296 // echo the actual_available_stock
297 echo '<p class="howto"><strong>Available Stock: </strong>' . esc_html( $actual_available_stock ) . '</p>';
298 }
299 }
300 }
301 add_action( 'add_meta_boxes', 'cmbird_product_metabox' );
302
303
304 /**
305 * Add Zoho and Exact Item IDs to Product and Variations
306 * @return void
307 */
308 add_action( 'woocommerce_product_options_pricing', 'cmbird_item_id_field' );
309 add_action( 'woocommerce_variation_options_pricing', 'cmbird_item_id_variation_field', 10, 3 );
310 function cmbird_item_id_field() {
311 woocommerce_wp_text_input(
312 array(
313 'id' => '_cost_price',
314 'class' => 'readonly',
315 'wrapper_class' => 'form-row',
316 'label' => esc_html__( 'Cost Price', 'commercebird' ),
317 'data_type' => 'price',
318 'description' => esc_html__( 'You can edit this via the CommerceBird App', 'commercebird' ),
319 )
320 );
321 woocommerce_wp_text_input(
322 array(
323 'id' => 'eo_item_id',
324 'label' => esc_html__( 'Exact Item ID', 'commercebird' ),
325 'class' => 'readonly',
326 'desc_tip' => true,
327 'description' => esc_html__( 'This is the Exact Item ID of this product. You cannot change this', 'commercebird' ),
328 )
329 );
330 woocommerce_wp_text_input(
331 array(
332 'id' => 'zi_item_id',
333 'label' => esc_html__( 'Zoho Item ID', 'commercebird' ),
334 'class' => 'readonly',
335 'desc_tip' => true,
336 'description' => esc_html__( 'This is the Zoho Item ID of this product. You cannot change this', 'commercebird' ),
337 )
338 );
339 }
340 function cmbird_item_id_variation_field( $loop, $variation_data, $variation ) {
341 woocommerce_wp_text_input(
342 array(
343 'id' => 'cost_price[' . $loop . ']',
344 'class' => 'readonly',
345 'wrapper_class' => 'form-row',
346 'data_type' => 'price',
347 'label' => esc_html__( 'Cost Price', 'commercebird' ),
348 'value' => get_post_meta( $variation->ID, '_cost_price', true ),
349 'description' => esc_html__( 'You can edit this via the CommerceBird App', 'commercebird' ),
350 )
351 );
352 woocommerce_wp_text_input(
353 array(
354 'id' => 'eo_item_id[' . $loop . ']',
355 'class' => 'readonly',
356 'label' => esc_html__( 'Exact Item ID', 'commercebird' ),
357 'value' => get_post_meta( $variation->ID, 'eo_item_id', true ),
358 'desc_tip' => true,
359 'description' => esc_html__( 'This is the Exact Item ID of this product. You cannot change this', 'commercebird' ),
360 )
361 );
362 woocommerce_wp_text_input(
363 array(
364 'id' => 'zi_item_id[' . $loop . ']',
365 'class' => 'readonly',
366 'label' => esc_html__( 'Zoho Item ID', 'commercebird' ),
367 'value' => get_post_meta( $variation->ID, 'zi_item_id', true ),
368 'desc_tip' => true,
369 'description' => esc_html__( 'This is the Zoho Item ID of this product. You cannot change this', 'commercebird' ),
370 )
371 );
372 }
373
374 /**
375 * Adds 'Zoho Sync' column header to 'Orders' page immediately after 'Total' column.
376 *
377 * @param string[] $columns
378 * @return string[] $new_columns
379 */
380 function cmbird_zi_sync_column_orders_overview( $columns ) {
381 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
382 if ( empty( $zoho_inventory_access_token ) ) {
383 return $columns;
384 }
385 $new_columns = array();
386 foreach ( $columns as $column_name => $column_info ) {
387
388 $new_columns[ $column_name ] = $column_info;
389
390 if ( 'order_total' === $column_name ) {
391 $new_columns['zoho_sync'] = __( 'Zoho Sync', 'commercebird' );
392 }
393 }
394 return $new_columns;
395 }
396 add_filter( 'manage_woocommerce_page_wc-orders_columns', 'cmbird_zi_sync_column_orders_overview', 20 );
397
398 /**
399 * Adding Sync Status for Orders Column
400 *
401 * @param string $column Column name.
402 * @param int $order_id $order id.
403 * @return void
404 */
405 function cmbird_zi_add_zoho_orders_content( $column, $order_id ) {
406 $zi_url = get_option( 'cmbird_zoho_inventory_url' );
407 $zi_visit_url = str_replace( 'www.zohoapis', 'inventory.zoho', $zi_url );
408 switch ( $column ) {
409 case 'zoho_sync':
410 // Get custom order meta data.
411 $order = wc_get_order( $order_id );
412 $zi_order_id = $order->get_meta( 'zi_salesorder_id', true, 'edit' );
413 $url = $zi_visit_url . 'app#/salesorders/' . $zi_order_id;
414 if ( $zi_order_id ) {
415 echo '<span class="dashicons dashicons-yes-alt" style="color:green;"></span><a href="' . esc_url( $url ) . '" target="_blank"> <span class="dashicons dashicons-external" style="color:green;"></span> </a>';
416 } else {
417 echo '<span class="dashicons dashicons-dismiss" style="color:red;"></span>';
418 }
419 unset( $order );
420 break;
421 }
422 }
423 add_action( 'manage_woocommerce_page_wc-orders_custom_column', 'cmbird_zi_add_zoho_orders_content', 20, 2 );
424
425 /**
426 * Adds 'Zoho Sync' column content.
427 *
428 * @param string[] $column name of column being displayed
429 */
430 function cmbird_zi_add_zoho_column_content( $column ) {
431 global $post;
432 $post_type = get_post_type( $post );
433
434 if ( 'zoho_sync' === $column && 'product' === $post_type ) {
435 $product_id = $post->ID;
436 $zi_product_id = get_post_meta( $product_id, 'zi_item_id' );
437 if ( $zi_product_id ) {
438 echo '<span class="dashicons dashicons-yes-alt" style="color:green;"></span>';
439 } else {
440 echo '<span class="dashicons dashicons-dismiss" style="color:red;"></span>';
441 }
442 }
443 }
444 add_action( 'manage_product_posts_custom_column', 'cmbird_zi_add_zoho_column_content' );
445
446 /**
447 * Adds 'Zoho Sync' column header to 'Products' page.
448 *
449 * @param string[] $columns
450 * @return string[] $new_columns
451 */
452 function cmbird_zi_sync_column_products_overview( $columns ) {
453 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
454 if ( empty( $zoho_inventory_access_token ) ) {
455 return $columns;
456 }
457 $new_columns = array();
458
459 foreach ( $columns as $column_name => $column_info ) {
460
461 $new_columns[ $column_name ] = $column_info;
462
463 if ( 'product_cat' === $column_name ) {
464 $new_columns['zoho_sync'] = __( 'Zoho Sync', 'commercebird' );
465 }
466 }
467
468 return $new_columns;
469 }
470 add_filter( 'manage_edit-product_columns', 'cmbird_zi_sync_column_products_overview', 20 );
471
472 /**
473 * Make 'Zoho Sync' column filterable.
474 */
475 function cmbird_zi_sync_column_filterable() {
476 global $typenow;
477
478 if ( 'product' === $typenow ) {
479 // code here
480 $value = isset( $_GET['zoho_sync_filter'] ) ? sanitize_text_field( wp_unslash( $_GET['zoho_sync_filter'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
481
482 echo '<select name="zoho_sync_filter">';
483 echo '<option value="">Zoho Sync Filter</option>';
484
485 // Count synced products
486 $synced_count = new WP_Query(
487 array(
488 'post_type' => 'product',
489 'meta_query' => array(
490 array(
491 'key' => 'zi_item_id',
492 'compare' => 'EXISTS',
493 ),
494 ),
495 'fields' => 'ids',
496 )
497 );
498 $synced_count = $synced_count->found_posts;
499 $synced_label = 'Synced';
500 if ( $synced_count > 0 ) {
501 $synced_label .= ' (' . $synced_count . ')';
502 }
503 echo '<option value="synced" ' . selected( $value, 'synced', false ) . '>' . esc_html( $synced_label ) . '</option>';
504
505 // Count not synced products
506 $not_synced_count = new WP_Query(
507 array(
508 'post_type' => 'product',
509 'meta_query' => array(
510 array(
511 'key' => 'zi_item_id',
512 'compare' => 'NOT EXISTS',
513 ),
514 ),
515 'fields' => 'ids',
516 )
517 );
518 $not_synced_count = $not_synced_count->found_posts;
519 $not_synced_label = 'Not Synced';
520 if ( $not_synced_count > 0 ) {
521 $not_synced_label .= ' (' . $not_synced_count . ')';
522 }
523 echo '<option value="not_synced" ' . selected( $value, 'not_synced', false ) . '>' . esc_html( $not_synced_label ) . '</option>';
524
525 echo '</select>';
526 }
527 }
528 add_action( 'restrict_manage_posts', 'cmbird_zi_sync_column_filterable' );
529
530 /**
531 * Modify the product query based on the filter.
532 *
533 * @param WP_Query $query The query object.
534 */
535 function cmbird_zi_sync_column_filter_query( $query ) {
536 global $typenow, $pagenow;
537
538 if ( $typenow === 'product' && $pagenow === 'edit.php' && isset( $_GET['zoho_sync_filter'] ) && $_GET['zoho_sync_filter'] !== '' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
539 $value = sanitize_text_field( wp_unslash( $_GET['zoho_sync_filter'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
540
541 $meta_query = array();
542
543 if ( $value === 'synced' ) {
544 $meta_query[] = array(
545 'key' => 'zi_item_id',
546 'compare' => 'EXISTS',
547 );
548 } elseif ( $value === 'not_synced' ) {
549 $meta_query[] = array(
550 'relation' => 'OR',
551 array(
552 'key' => 'zi_item_id',
553 'compare' => 'NOT EXISTS',
554 ),
555 array(
556 'key' => 'zi_item_id',
557 'value' => '',
558 'compare' => '=',
559 ),
560 );
561 }
562
563 $query->set( 'meta_query', $meta_query );
564 }
565 }
566 add_action( 'pre_get_posts', 'cmbird_zi_sync_column_filter_query' );
567
568 /**
569 * Change Action Scheduler default purge to 1 week
570 * @return int
571 */
572 function cmbird_action_scheduler_purge() {
573 return WEEK_IN_SECONDS;
574 }
575 add_filter( 'action_scheduler_retention_period', 'cmbird_action_scheduler_purge' );
576
577 add_filter(
578 'action_scheduler_default_cleaner_statuses',
579 function ($statuses) {
580 $statuses[] = ActionScheduler_Store::STATUS_FAILED;
581 return $statuses;
582 }
583 );
584
585 /**
586 * Modify the Products API to include brands.
587 *
588 * TODO: This is a temporary solution. We need to find a better way to include brands in the API response.
589 *
590 * @param $response The response object.
591 * @param $post The post object.
592 * @param $request The request object.
593 * @return mixed
594 */
595 add_filter( 'woocommerce_rest_prepare_product_object', 'cmbird_rest_api_prepare_brands_tax', 10, 3 );
596 function cmbird_rest_api_prepare_brands_tax( $response, $object, $request ) {
597 $product_id = $object->get_id();
598 if ( empty( $response->data['product_brands'] ) ) {
599 $terms = [];
600 foreach ( wp_get_post_terms( $product_id, 'product_brands' ) as $term ) {
601 $terms[] = [
602 'id' => $term->term_id,
603 'name' => $term->name,
604 'slug' => $term->slug,
605 ];
606 }
607 $response->data['product_brands'] = $terms;
608 }
609 return $response;
610 }
611
612
613 function cmbird_prepare_insert_product( $product, $request ) {
614 if ( isset( $request['product_brands'] ) ) {
615 wp_set_post_terms( $request['id'], $request['product_brands'], 'product_brands', false );
616 }
617 return $product;
618 }
619 add_filter( 'woocommerce_rest_pre_insert_product_object', 'cmbird_prepare_insert_product', 10, 2 );