0

I'm tring to build an advanced search form for woocommerce based web site.

Found this answer (https://wordpress.stackexchange.com/a/184186/51376) and added all codes. But it doesn't work.

functions.php

// advanced search functionality
function advanced_search_query($query) {

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

search form;

<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
<div class="form-group">
    <div class="search-form">
        <label class="screen-reader-text" for="woocommerce-product-search-field"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
        <input type="search" id="woocommerce-product-search-field" class="search-field" placeholder="<?php echo esc_attr_x( 'Search Products&hellip;', 'placeholder', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'woocommerce' ); ?>" />
    </div>
    <?php wp_dropdown_categories( 'name=product_cat&hierarchical=1&taxonomy=product_cat&class=category-select&show_option_all=Tüm Kategoriler' ); ?>
    <div class="search-submit">
        <input type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" />
    </div>
</div>
<input type="hidden" name="post_type" value="product" />
</form>

1 Answer 1

1

You can try below code. Below code shows search box and woocommerce product categories . It will also show selected category.

   <form name="myform" method="GET" action="<?php echo esc_url(home_url('/')); ?>">

    <?php if (class_exists('WooCommerce')) : ?>
  <?php 
  if(isset($_REQUEST['product_cat']) && !empty($_REQUEST['product_cat']))
  {
   $optsetlect=$_REQUEST['product_cat'];
  }
 else{
  $optsetlect=0;  
  }
         $args = array(
                    'show_option_all' => esc_html__( 'All Categories', 'woocommerce' ),
                    'hierarchical' => 1,
                    'class' => 'cat',
                    'echo' => 1,
                    'value_field' => 'slug',
                    'selected' => $optsetlect
                );
          $args['taxonomy'] = 'product_cat';
          $args['name'] = 'product_cat';              
          $args['class'] = 'cate-dropdown hidden-xs';
          wp_dropdown_categories($args);

   ?>
  <input type="hidden" value="product" name="post_type">
<?php endif; ?>
               <input type="text"  name="s" class="searchbox" maxlength="128" value="<?php echo get_search_query(); ?>" placeholder="<?php esc_attr_e('Search entire store here...', 'woocommerce'); ?>">

    <button type="submit" title="<?php esc_attr_e('Search', 'woocommerce'); ?>" class="search-btn-bg"><span><?php esc_attr_e('Search','woocommerce');?></span></button>
  </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.