1

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

3
  • What have you tried so far? the code you placed here is exactly the same as the url you shared
    – 7uc1f3r
    May 13, 2020 at 10:30
  • code changed to what Im using. May 13, 2020 at 12:43
  • Anyone? a bit stuck on this one, I am thinking it should be some small tweaking, just can't figure out what to tweak to get it working. May 19, 2020 at 8:58

1 Answer 1

1

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' => '='
            );
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.