With inspiration in this post - woocommerce admin product search multiple skus I am trying add a search for multiple orders to WooCommerce Is it possible to tweak this code to make it work for orders instead of products?
function woo_multiple_order_search( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'shop_order' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
if (strpos($search_term, '|') == false) return $query_vars;
$order_ids = explode('|',$search_term);
$meta_query = array(
'relation' => 'OR'
);
if(is_array($order_ids) && $order_ids) {
foreach($order_ids as $order_id) {
$meta_query[] = array(
'key' => '_order_id',
'value' => $order_id,
'compare' => '='
);
}
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_query' => $meta_query
);
$posts = get_posts( $args );
if ( ! $posts ) return $query_vars;
foreach($posts as $post){
$query_vars['post__in'][] = $post->ID;
}
}
return $query_vars;
}
add_filter( 'request', 'woo_multiple_order_search', 20 );
Seems to be working until foreach($posts as $post){
This code works perfect if you change '_order_id' by the correct field name '_order_number' in this part:
foreach($order_ids as $order_id) {
$meta_query[] = array(
'key' => '_order_number', // correct field name
'value' => $order_id,
'compare' => '='
);
}