From 81149157a3e25e04442be2f75952474c9c514c29 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Sat, 9 Jun 2018 17:22:26 +0100 Subject: [PATCH 1/3] woocommerce_json_search_limit filter --- includes/class-wc-ajax.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index ee2496f08b2..a1b36d291e5 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -1392,8 +1392,14 @@ class WC_AJAX { wp_die(); } + if ( ! empty( $_GET['limit'] ) ) { + $limit = absint( $_GET['limit'] ); + } else { + $limit = absint( apply_filters( 'woocommerce_json_search_limit', 30 ) ); + } + $data_store = WC_Data_Store::load( 'product' ); - $ids = $data_store->search_products( $term, '', (bool) $include_variations ); + $ids = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit ); if ( ! empty( $_GET['exclude'] ) ) { $ids = array_diff( $ids, (array) $_GET['exclude'] ); @@ -1403,10 +1409,6 @@ class WC_AJAX { $ids = array_intersect( $ids, (array) $_GET['include'] ); } - if ( ! empty( $_GET['limit'] ) ) { - $ids = array_slice( $ids, 0, absint( $_GET['limit'] ) ); - } - $product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' ); $products = array(); From 4bc78413251e8f15fbda9a740d47b36756d8f919 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Sat, 9 Jun 2018 17:23:07 +0100 Subject: [PATCH 2/3] search_products limit support --- .../class-wc-product-data-store-cpt.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index d85151f8bd3..6a57e101c5a 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1344,13 +1344,14 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da /** * Search product data for a term and return ids. * - * @param string $term Search term. - * @param string $type Type of product. - * @param bool $include_variations Include variations in search or not. - * @param bool $all_statuses Should we search all statuses or limit to published. + * @param string $term Search term. + * @param string $type Type of product. + * @param bool $include_variations Include variations in search or not. + * @param bool $all_statuses Should we search all statuses or limit to published. + * @param null|int $limit Limit returned results. @since 3.5.0. * @return array of ids */ - public function search_products( $term, $type = '', $include_variations = false, $all_statuses = false ) { + public function search_products( $term, $type = '', $include_variations = false, $all_statuses = false, $limit = null ) { global $wpdb; $post_types = $include_variations ? array( 'product', 'product_variation' ) : array( 'product' ); @@ -1411,6 +1412,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $status_where = " AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "') "; } + if ( $limit ) { + $limit_query = $wpdb->prepare( ' LIMIT %d ', $limit ); + } + // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery $search_results = $wpdb->get_results( // phpcs:disable @@ -1421,7 +1426,9 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $search_where $status_where $type_where - ORDER BY posts.post_parent ASC, posts.post_title ASC" + ORDER BY posts.post_parent ASC, posts.post_title ASC + $limit_query + " // phpcs:enable ); From a0eb6843c261cec58d8bac05a7337411a23e70f8 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 15 Jun 2018 15:28:29 +0100 Subject: [PATCH 3/3] define limit query --- includes/data-stores/class-wc-product-data-store-cpt.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 6a57e101c5a..f27c3975d2f 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1359,6 +1359,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $type_join = ''; $type_where = ''; $status_where = ''; + $limit_query = ''; $term = wc_strtolower( $term ); // See if search term contains OR keywords.