allow searching from predefined set and introduce search limit

Makes it possible to search from a pre-defined set of known ids without returning all results, avoiding an excessive amount of product instantiations with large result sets.
This commit is contained in:
Manos Psychogyiopoulos 2015-12-12 14:28:45 +02:00
parent 1a17c0f404
commit 2a6a26f800
2 changed files with 12 additions and 3 deletions

View File

@ -95,7 +95,9 @@ jQuery( function( $ ) {
term: term,
action: $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
security: wc_enhanced_select_params.search_products_nonce,
exclude: $( this ).data( 'exclude' )
exclude: $( this ).data( 'exclude' ),
include: $( this ).data( 'include' ),
limit: $( this ).data( 'limit' )
};
},
results: function( data ) {

View File

@ -1749,8 +1749,7 @@ class WC_AJAX {
check_ajax_referer( 'search-products', 'security' );
$term = (string) wc_clean( stripslashes( $_GET['term'] ) );
$exclude = array();
$term = (string) wc_clean( stripslashes( $_GET['term'] ) );
if ( empty( $term ) ) {
die();
@ -1791,6 +1790,14 @@ class WC_AJAX {
$query .= " AND posts.ID NOT IN (" . implode( ',', array_map( 'intval', explode( ',', $_GET['exclude'] ) ) ) . ")";
}
if ( ! empty( $_GET['include'] ) ) {
$query .= " AND posts.ID IN (" . implode( ',', array_map( 'intval', explode( ',', $_GET['include'] ) ) ) . ")";
}
if ( ! empty( $_GET['limit'] ) ) {
$query .= " LIMIT " . intval( $_GET['limit'] );
}
$posts = array_unique( $wpdb->get_col( $query ) );
$found_products = array();