From 6047322c520ad9dcc0fdf320e764b4abb0408687 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 2 Feb 2018 15:11:23 +0000 Subject: [PATCH 1/3] Rather than query products by a SKU, use the CRUD search helper. --- .../class-wc-admin-list-table-products.php | 46 ++++--------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/includes/admin/list-tables/class-wc-admin-list-table-products.php b/includes/admin/list-tables/class-wc-admin-list-table-products.php index 4e3ffa78527..99afc2c676b 100644 --- a/includes/admin/list-tables/class-wc-admin-list-table-products.php +++ b/includes/admin/list-tables/class-wc-admin-list-table-products.php @@ -399,7 +399,6 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table { * @return array */ protected function query_filters( $query_vars ) { - if ( isset( $query_vars['orderby'] ) ) { if ( 'price' === $query_vars['orderby'] ) { // @codingStandardsIgnoreStart @@ -453,50 +452,23 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table { ); } + // Search using CRUD. + if ( isset( $query_vars['s'] ) ) { + $data_store = WC_Data_Store::load( 'product' ); + $ids = $data_store->search_products( wc_clean( $query_vars['s'] ), '', true ); + $query_vars['post__in'] = $ids; + } + return $query_vars; } /** * Search by SKU or ID for products. * + * @deprecated Logic moved to query_filters. * @param string $where Where clause SQL. - * @return string */ - public function sku_search( $where ) { - global $pagenow, $wpdb, $wp; - - if ( 'edit.php' !== $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'product' !== $wp->query_vars['post_type'] ) { - return $where; - } - - $search_ids = array(); - $terms = explode( ',', $wp->query_vars['s'] ); - - foreach ( $terms as $term ) { - if ( is_numeric( $term ) ) { - $search_ids[] = absint( $term ); - } else { - $id_from_sku = wc_get_product_id_by_sku( wc_clean( $term ) ); - - if ( $id_from_sku ) { - $product = wc_get_product( $id_from_sku ); - - if ( $product ) { - $search_ids[] = $product->get_id(); - $search_ids[] = $product->get_parent_id(); - } - } - } - } - - $search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) ); - - if ( count( $search_ids ) > 0 ) { - $where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ')) OR ((', $where ); - } - - return $where; - } + public function sku_search( $where ) {} /** * Change views on the edit product screen. From 59d38b5a85c73c8bfddb6ef9b3658f0343858344 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 2 Feb 2018 15:13:37 +0000 Subject: [PATCH 2/3] Handle no results --- .../admin/list-tables/class-wc-admin-list-table-products.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/list-tables/class-wc-admin-list-table-products.php b/includes/admin/list-tables/class-wc-admin-list-table-products.php index 99afc2c676b..18d3799e584 100644 --- a/includes/admin/list-tables/class-wc-admin-list-table-products.php +++ b/includes/admin/list-tables/class-wc-admin-list-table-products.php @@ -456,7 +456,7 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table { if ( isset( $query_vars['s'] ) ) { $data_store = WC_Data_Store::load( 'product' ); $ids = $data_store->search_products( wc_clean( $query_vars['s'] ), '', true ); - $query_vars['post__in'] = $ids; + $query_vars['post__in'] = array_merge( $ids, array( 0 ) ); } return $query_vars; From 2321e8bdc134b7d29779444ef47cf99caf5f66c7 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 2 Feb 2018 14:24:18 -0200 Subject: [PATCH 3/3] Properly deprecated WC_Admin_List_Table_Products->sku_search() --- .../list-tables/class-wc-admin-list-table-products.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/admin/list-tables/class-wc-admin-list-table-products.php b/includes/admin/list-tables/class-wc-admin-list-table-products.php index 18d3799e584..9b9d70ba0d3 100644 --- a/includes/admin/list-tables/class-wc-admin-list-table-products.php +++ b/includes/admin/list-tables/class-wc-admin-list-table-products.php @@ -39,7 +39,6 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table { parent::__construct(); add_filter( 'disable_months_dropdown', '__return_true' ); add_filter( 'query_vars', array( $this, 'add_custom_query_var' ) ); - add_filter( 'posts_search', array( $this, 'sku_search' ) ); add_filter( 'views_edit-product', array( $this, 'product_views' ) ); } @@ -467,8 +466,11 @@ class WC_Admin_List_Table_Products extends WC_Admin_List_Table { * * @deprecated Logic moved to query_filters. * @param string $where Where clause SQL. + * @return string */ - public function sku_search( $where ) {} + public function sku_search( $where ) { + return $where; + } /** * Change views on the edit product screen.