2
votes

When using the order panel of woocommerce I noticed that an unnecessary query is executed on using search. This is the query on Woocommerce file (/includes/data-stores/class-wc-order-data-store-cpt.php):

SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items as order_items WHERE order_item_name LIKE '%%%s%%'

And this is Woocommerce code:

  public function search_orders( $term ) { 
  global $wpdb; $search_fields = array_map( 'wc_clean', apply_filters( 'woocommerce_shop_order_search_fields', array( 
      '_billing_address_index',  
      '_shipping_address_index',  
      '_billing_last_name',  
      '_billing_email', ) ) ); 
  $order_ids = array(); 

  if ( is_numeric( $term ) ) { 
      $order_ids[] = absint( $term ); 
  } 

  if ( ! empty( $search_fields ) ) { 
      $order_ids = array_unique( array_merge( 
          $order_ids,  
          $wpdb->get_col( 
              $wpdb->prepare( "SELECT DISTINCT p1.post_id FROM {$wpdb->postmeta} p1 WHERE p1.meta_value LIKE '%%%s%%'", $wpdb->esc_like( wc_clean( $term ) ) ) . " AND p1.meta_key IN ('" . implode( "', '", array_map( 'esc_sql', $search_fields ) ) . "')" ),  
          $wpdb->get_col( 
              $wpdb->prepare( " 
                  SELECT order_id 
                  FROM {$wpdb->prefix}woocommerce_order_items as order_items 
                  WHERE order_item_name LIKE '%%%s%%' 
                  ",  
                  $wpdb->esc_like( wc_clean( $term ) ) ) ) ) ); 
  } 

  return apply_filters( 'woocommerce_shop_order_search_results', $order_ids, $term, $search_fields ); } 

Is there any way to get rid of this query with a function?

1
Let me know if you were able to get my answer to work!Ruvee

1 Answers

2
votes

You could use __return_false on it, like so:

add_filter( 'woocommerce_shop_order_search_results', '__return_false')

__return_falseDocs

Code goes to the functions.php file of your active theme.