Merge pull request #20470 from woocommerce/update/20454
Support limiting search results
This commit is contained in:
commit
fc02687545
|
@ -1310,8 +1310,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'] );
|
||||
|
@ -1321,10 +1327,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();
|
||||
|
||||
|
|
|
@ -1348,9 +1348,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
|
|||
* @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' );
|
||||
|
@ -1358,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.
|
||||
|
@ -1411,6 +1413,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 +1427,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
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue