[Plugin: WooCommerce] Order Admin – Searching for order notes
-
Being able to search the Order Notes in the Order Admin screen in WooCommerce is not a standard feature today. As I spent some significant time resolving it and I also believe it is relevant for others I will share it with you. Hopefully it will be useful for someone else too.
I found very few articles on this subject matter. Two very helpful sources of inspiration and guiding were:
1. Tamara Zuk’s reply to a person asking for order notes search functionality
2. Scott Anderson’s article about overriding WooCommerce/WP core class functionsWhat did I do?
- All modifications were put in the child themes functions.php
- Made a filter searching in order notes which actually are put in the WP comments table as comment_type = ‘order_note’.
- Overrided the function WC_Admin_Post_Types::shop_order_search_custom_fields in a sub-class called My_WC Admin_Post_Types
- Added a filter hook (shop_order_search_custom_order_ids) to the overridden function above. Doing this I was now able to merge post_ids from the filter into the post__in variable.
The code looks like this:
add_filter( 'shop_order_search_custom_order_ids', 'shop_order_search_order_notes'); function shop_order_search_order_notes ($post_ids) { global $pagenow, $wpdb; // Search order notes (from comments table) $post_ids = array_unique( array_merge( $wpdb->get_col( $wpdb->prepare( " SELECT comment_post_ID FROM {$wpdb->prefix}comments as comments WHERE comment_type = 'order_note' AND comment_content LIKE '%%%s%%' ", esc_attr( $_GET['s'] ) ) ) , array() ) ); return $post_ids; } class My_WC_Admin_Post_Types { public function shop_order_search_custom_fields( $wp ) { global $pagenow; if ( 'edit.php' != $pagenow || empty( $wp->query_vars['s'] ) || $wp->query_vars['post_type'] != 'shop_order' ) { echo 'RETURNED'; return; } $post_ids = wc_order_search( $_GET['s'] ); // BF: Added this line to enable to hook into the post_ids $post_ids = array_unique(array_merge( $post_ids, apply_filters('shop_order_search_custom_order_ids',array()))); if ( ! empty( $post_ids ) ) { // Remove "s" - we don't want to search order name. unset( $wp->query_vars['s'] ); // so we know we're doing this. $wp->query_vars['shop_order_search'] = true; // Search by found posts. $wp->query_vars['post__in'] = array_merge( $post_ids, array( 0 ) ); } } } remove_action( 'parse_query', 'WC_Admin_Post_Types::shop_order_search_custom_fields' ); add_action( 'parse_query', 'My_WC_Admin_Post_Types::shop_order_search_custom_fields' );
Thanks for any reply on improvements to the code itself or suggestions for other strategies to resolve this functionality.
[moderator note – removed “modlook” tag; please do not use this tag unless a post/thread is actually breaking the forum rules]
- The topic ‘[Plugin: WooCommerce] Order Admin – Searching for order notes’ is closed to new replies.