Category based search in woocommerce products

  • 0

Add following hook in function.php

function advanced_search_query($query) {

if($query->is_search()) {
$query->set(‘post_type’, ‘product’);
// category terms search.
if (isset($_GET[‘category’]) && !empty($_GET[‘category’])) {
$query->set(‘tax_query’, array(array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array($_GET[‘category’]) )
));
}
return $query;
}
}
add_action(‘pre_get_posts’, ‘advanced_search_query’, 1000);

Add following code where you need to include the search form:

<form role=”search” method=”get” action=”<?php echo get_permalink( woocommerce_get_page_id( ‘shop’ )); ?>”>
<input type=”hidden” name=”post_type” value=”product” />
<select name=”category” id=”category”>
<option value=”clothing” selected>Clothing</option>
<option value=”posters”>Posters</option>
<option value=”music”>Music</option>
</select>
<input name=”s” type=”text”/>
<button type=”submit”>Submit</button>
</form>

 

 

Reference: http://wordpress.stackexchange.com/questions/184166/create-product-category-and-keyword-search-form-in-woocommerce

Add following hook in function.php function advanced_search_query($query) { if($query->is_search()) { $query->set(‘post_type’, ‘product’); // category terms search. if (isset($_GET[‘category’]) && !empty($_GET[‘category’])) { $query->set(‘tax_query’, array(array( ‘taxonomy’ => ‘product_cat’, ‘field’ => ‘slug’, ‘terms’ => array($_GET[‘category’]) ) )); } return $query; } } add_action(‘pre_get_posts’, ‘advanced_search_query’, 1000); Add following code where you need to include the search form: <form role=”search”…

Add following hook in function.php function advanced_search_query($query) { if($query->is_search()) { $query->set(‘post_type’, ‘product’); // category terms search. if (isset($_GET[‘category’]) && !empty($_GET[‘category’])) { $query->set(‘tax_query’, array(array( ‘taxonomy’ => ‘product_cat’, ‘field’ => ‘slug’, ‘terms’ => array($_GET[‘category’]) ) )); } return $query; } } add_action(‘pre_get_posts’, ‘advanced_search_query’, 1000); Add following code where you need to include the search form: <form role=”search”…