WooCommerce 搜索多个订单

WooCommerce search for multiple orders

灵感来源于此post - woocommerce admin product search multiple skus 我正在尝试向 WooCommerce 添加对多个订单的搜索 是否可以调整此代码以使其适用于订单而不是产品?

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 );

似乎一直工作到 foreach($posts as $post){

如果您在此部分中将“_order_id”更改为正确的字段名称“_order_number”,则此代码将完美运行:

        foreach($order_ids as $order_id) {
            $meta_query[] = array(
                'key' => '_order_number', // correct field name
                'value' => $order_id,
                'compare' => '='
            );
        }