Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search argument in WC_Product_Query и wc_get_products? And Args can be applied to variations? #21450

Closed
jensen6720185 opened this issue Sep 26, 2018 · 6 comments

Comments

@jensen6720185
Copy link

jensen6720185 commented Sep 26, 2018

Describe the question
Is there any way to use search by product title or custom meta with new functions? because with wp_query we can use it like that:
$query = new WP_Query( array( 's' => 'keyword' ) );

or like alternative we can get products with wp_query and add ids to include

For example if we create ajax search with fields: search, date range, sku, price, etc
Search Ids by s field if we get it earlier...

$somekeywords = 'Some Product Name';
$main_args = '';

if ($somekeywords != '') {
	$args = array(
		'post_type' => 'product',
		's' => $somekeywords, 
		'fields' => 'ids'
	)
	
	$products = new WP_Query( $args );
	
	$main_args ['include'] = $products->posts;
}

$main_args = array(
	'orderby' => 'date',
	'order' => 'desc',
	return' => 'ids'
);

$products = wc_get_products( $args );

PS Change from WP_Query doesn't make sense because all attributes like tags, categories, price, sku etc don't work with variations, only for parent products.

@jensen6720185 jensen6720185 changed the title Search argument in WC_Product_Query и wc_get_products? Search argument in WC_Product_Query и wc_get_products? And Args can be applied to variations? Sep 26, 2018
@claudiulodro
Copy link
Contributor

Hi there,

You can use WC_Product_Data_Store_CPT::search_products to do a product search similar to the s param on WP_Query.

wc_get_products does not support partial title search, but it does have support for custom params if you want to add this functionality to whatever you are doing.

Hope this clears things up. Thanks.

@cody-rees
Copy link

cody-rees commented Apr 9, 2020

For anyone else (and likely myself while googling this problem again) who needs a more straightforward resolution to this problem feel free to use the following code snippets.

/**
 * Add support for 'search_term' query var (Add to functions file)
 */
function support_search_term_query_var( $query, $query_vars ) {
    if( empty( $query_vars['search_term'] ) ) {
        return $query;
    }

    $query['s'] = $query_vars['search_term'];
    return $query;
 }
 add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'support_search_term_query_var', 10, 2 );



 /**
  * Usage
  */
 $my_search_term = $_POST['search'];
 $products = wc_get_products([
    'limit'          => 10,   
    'post_type'      => 'product',
    'search_term'    => $my_search_term,
    'visibility'     => 'catalog',
]);

@claudiulodro
Copy link
Contributor

Sorry! In retrospect that wasn't a super helpful response because the line I linked to now points to a different thing. :) Thanks for adding the snippet.

For reference, this is what my recommendation looks like as a snippet:

$data_store = WC_Data_Store::load( 'product' );
$search_results = $data_store->search_products( 'my-search-term' );

@erikdemarco
Copy link
Contributor

For anyone else (and likely myself while googling this problem again) who needs a more straightforward resolution to this problem feel free to use the following code snippets.

/**
 * Add support for 'search_term' query var (Add to functions file)
 */
function support_search_term_query_var( $query, $query_vars ) {
    if( empty( $query_vars['search_term'] ) ) {
        return $query;
    }

    $query['s'] = $query_vars['search_term'];
    return $query;
 }
 add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'support_search_term_query_var', 10, 2 );



 /**
  * Usage
  */
 $my_search_term = $_POST['search'];
 $products = wc_get_products([
    'limit'          => 10,   
    'post_type'      => 'product',
    'search_term'    => $my_search_term,
    'visibility'     => 'catalog',
]);

We can't use order id as the search term. It will return zero result.

@yossefEl
Copy link

This code is a part of my custom search endpoint, its results are similar to woocommerce REST API endpoint
/products?search=keyword

$args = array(
           'post_type' => 'product',
           'posts_per_page' => 10,
           'paged' => $page,
           's' => $request['s'],
           'paginate' => true,
           'meta_query' => array(
               array(
                   'key' => '_sku',
                   'value' => $request['s'],
                   'compare' => 'LIKE',
               ),
               array(
                   'key' => '_title',
                   'value' => $request['s'],
                   'compare' => 'LIKE',
               ),
           ),
       );

       $query = wc_get_products($args);
       //get products 
      $products=  $query->products

@noknokcody
Copy link

For anyone else (and likely myself while googling this problem again) who needs a more straightforward resolution to this problem feel free to use the following code snippets.

/**
 * Add support for 'search_term' query var (Add to functions file)
 */
function support_search_term_query_var( $query, $query_vars ) {
    if( empty( $query_vars['search_term'] ) ) {
        return $query;
    }

    $query['s'] = $query_vars['search_term'];
    return $query;
 }
 add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'support_search_term_query_var', 10, 2 );



 /**
  * Usage
  */
 $my_search_term = $_POST['search'];
 $products = wc_get_products([
    'limit'          => 10,   
    'post_type'      => 'product',
    'search_term'    => $my_search_term,
    'visibility'     => 'catalog',
]);

Thanks past me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants