diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 940b1ca1aa2..d4cb2f063e4 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -1543,16 +1543,11 @@ class WC_AJAX { $limit = absint( apply_filters( 'woocommerce_json_search_limit', 30 ) ); } + $include_ids = ! empty( $_GET['include'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) : array(); + $exclude_ids = ! empty( $_GET['exclude'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) : array(); + $data_store = WC_Data_Store::load( 'product' ); - $ids = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit ); - - if ( ! empty( $_GET['exclude'] ) ) { - $ids = array_diff( $ids, array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) ); - } - - if ( ! empty( $_GET['include'] ) ) { - $ids = array_intersect( $ids, array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) ); - } + $ids = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit, $include_ids, $exclude_ids ); $product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' ); $products = array(); @@ -1590,21 +1585,18 @@ class WC_AJAX { public static function json_search_downloadable_products_and_variations() { check_ajax_referer( 'search-products', 'security' ); + if ( ! empty( $_GET['limit'] ) ) { + $limit = absint( $_GET['limit'] ); + } else { + $limit = absint( apply_filters( 'woocommerce_json_search_limit', 30 ) ); + } + + $include_ids = ! empty( $_GET['include'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) : array(); + $exclude_ids = ! empty( $_GET['exclude'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) : array(); + $term = isset( $_GET['term'] ) ? (string) wc_clean( wp_unslash( $_GET['term'] ) ) : ''; $data_store = WC_Data_Store::load( 'product' ); - $ids = $data_store->search_products( $term, 'downloadable', true ); - - if ( ! empty( $_GET['exclude'] ) ) { - $ids = array_diff( $ids, array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) ); - } - - if ( ! empty( $_GET['include'] ) ) { - $ids = array_intersect( $ids, array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) ); - } - - if ( ! empty( $_GET['limit'] ) ) { - $ids = array_slice( $ids, 0, absint( $_GET['limit'] ) ); - } + $ids = $data_store->search_products( $term, 'downloadable', true, false, $limit ); $product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' ); $products = array(); 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 b12ed87d3e3..eb6bdc80ddc 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1422,14 +1422,16 @@ 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 null|int $limit Limit returned results. @since 3.5.0. + * @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. + * @param null|array $include Keep specific results. @since 3.6.0. + * @param null|array $exclude Discard specific results. @since 3.6.0. * @return array of ids */ - public function search_products( $term, $type = '', $include_variations = false, $all_statuses = false, $limit = null ) { + public function search_products( $term, $type = '', $include_variations = false, $all_statuses = false, $limit = null, $include = null, $exclude = null ) { global $wpdb; $custom_results = apply_filters( 'woocommerce_product_pre_search_products', false, $term, $type, $include_variations, $all_statuses, $limit ); @@ -1484,7 +1486,15 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da } if ( ! empty( $search_queries ) ) { - $search_where = 'AND (' . implode( ') OR (', $search_queries ) . ')'; + $search_where = ' AND (' . implode( ') OR (', $search_queries ) . ') '; + } + + if ( ! empty( $include ) && is_array( $include ) ) { + $search_where .= ' AND posts.ID IN(' . implode( ',', array_map( 'absint', $include ) ) . ') '; + } + + if ( ! empty( $exclude ) && is_array( $exclude ) ) { + $search_where .= ' AND posts.ID NOT IN(' . implode( ',', array_map( 'absint', $exclude ) ) . ') '; } if ( 'virtual' === $type ) { diff --git a/tests/unit-tests/product/data-store.php b/tests/unit-tests/product/data-store.php index e0925d40914..ee34a2b5eac 100644 --- a/tests/unit-tests/product/data-store.php +++ b/tests/unit-tests/product/data-store.php @@ -804,5 +804,20 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case { $this->assertContains( $product2->get_id(), $results ); $this->assertNotContains( $product3->get_id(), $results ); $this->assertNotContains( $product4->get_id(), $results ); + + $results = $data_store->search_products( 'green', '', true, true, 1 ); + $this->assertEquals( 1, count( array_diff( $results, array( 0 ) ) ) ); + + $results = $data_store->search_products( 'green', '', true, true, null, array( $product3->get_id() ) ); + $this->assertNotContains( $product->get_id(), $results ); + $this->assertNotContains( $product2->get_id(), $results ); + $this->assertContains( $product3->get_id(), $results ); + $this->assertNotContains( $product4->get_id(), $results ); + + $results = $data_store->search_products( 'green', '', true, true, null, null, array( $product3->get_id() ) ); + $this->assertNotContains( $product->get_id(), $results ); + $this->assertNotContains( $product2->get_id(), $results ); + $this->assertNotContains( $product3->get_id(), $results ); + $this->assertContains( $product4->get_id(), $results ); } }