Maxim Colesnic
Maxim Colesnic

Reputation: 475

Woocommerce - search in product category by keyword

This is my search form. Im use woocommerce. And need to search products by keywords and categories.

         <form role="search" method="get" action="<?php echo get_permalink( woocommerce_get_page_id( 'shop' ) ); ?>">
            <div class="search-bar-select hidden-sm hidden-xs">
                <span></span>
                <i></i>
                <select name="category">
                <option value="" class="search-bar-select-text"><?php _e('[:ru]Все категории[:ro]Toate categoriile') ?></option>
                <?php foreach(woo_category_list(FALSE) as $category) { ?>
                <option value="<?php echo $category->slug; ?>"><?php echo $category->cat_name; ?></option>
                <?php } ?>
                </select>
            </div>
            <div class="search-bar-input">
                <input type="text" name="s" value="<?php echo get_search_query(); ?>" placeholder="<?php _e('[:ru]Поиск по сайту ...[:ro]Căutare pe site') ?>" />
            </div>
            <input type="hidden" name="post_type" value="product" />
            <div class="search-bar-btn">
                <button type="submit"><i class="fa fa-search"></i></button>
            </div>
        </form>

this is my filter code

function advanced_search_query($query)
{
if($query->is_search()) {
    // 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);

but wordpress show all products from all categories by keyword. WHAT IS WRONG?

Upvotes: 1

Views: 13482

Answers (1)

webelizer
webelizer

Reputation: 418

you can do this with out hooks.

simply change the "name" attribute for the category input to "product_cat".

so remove the hook and change your code like this:

  <form role="search" method="get" action="<?php echo get_permalink( woocommerce_get_page_id( 'shop' ) ); ?>">
            <div class="search-bar-select hidden-sm hidden-xs">
                <strong textspan></span>
                <i></i>
                <select name="product_cat">
                <option value="" class="search-bar-select-text"><?php _e('[:ru]Все категории[:ro]Toate categoriile') ?></option>
                <?php foreach(woo_category_list(FALSE) as $category) { ?>
                <option value="<?php echo $category->slug; ?>"><?php echo $category->cat_name; ?></option>
                <?php } ?>
                </select>
            </div>
            <div class="search-bar-input">
                <input type="text" name="s" value="<?php echo get_search_query(); ?>" placeholder="<?php _e('[:ru]Поиск по сайту ...[:ro]Căutare pe site') ?>" />
            </div>
            <input type="hidden" name="post_type" value="product" />
            <div class="search-bar-btn">
                <button type="submit"><i class="fa fa-search"></i></button>
            </div>
        </form>

to list all the product cats in a dropdown list, use this method:

(instead of foreach(woo_category_list(FALSE) as $category) )

<?php 
$args = array(
   'taxonomy' => 'product_cat',
   'name' => 'product_cat',
   'value_field' => 'slug',
   'class' => 'something'
);
wp_dropdown_categories( $args );

?>

Upvotes: 0

Related Questions