WordPress.org

Forums

[Plugin: Woocommerce] Search for products by product category (7 posts)

  1. wordsword
    Member
    Posted 1 year ago #

    I'm trying to change the default Woocommerce product search so that a user can specify which product category they'd like to search in. The code that I have in my product-searchform.php is:

    <form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
        	<div>
    		<label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
        		<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="<?php _e( 'Search for products', 'woocommerce' ); 
    
    ?>" />
    <?php
            	wp_dropdown_categories(
                		array(
                    		'name' => 'product_cat',
                    		'show_option_all' => 'Select a category',
                    		'taxonomy' => 'product_cat'
                	))
    ?>
        		<input type="submit" id="searchsubmit" value="<?php echo esc_attr__( 'Search' ); ?>" />
    		<input type="hidden" name="post_type" value="product" />
        	</div>
    </form>

    If I enter a search term, select a product category, and then click search, no results are found (when in fact there should be results). The search url looks like this: http://www.mydomain.com/?s=MYSEARCHTERM&product_cat=24&post_type=product

    Why is it not working?

    If I enter a search term without selecting a product category to search in, then the search returns products from all product categories. The search url looks like this: http://www.mydomain.com/?s=MYSEARCHTERM&product_cat=0&post_type=product

    Instead of returning results from all categories, I'd like for it to not do the search at all and display a message "Please select a product category to search". Any ideas on how to get this to work?

    Thanks!

  2. Palexandra
    Member
    Posted 1 year ago #

    Have you found a solution for this? Im trying to do the same thing and im not sure how to.

  3. nikkyandrei
    Member
    Posted 1 year ago #

    While looking for a solution to this problem, I read this post.
    Now I found the solution I will write it below.
    Also in my situation it war required to work on other language.

    <script>
      function avalcm_search_link(vas){
        var link1=document.getElementById("home_url").value;
        var sublink=document.getElementById("categ_base_lang").value;
        var link2="";
        if (vas!="") {
          var link2=sublink + "/" + vas + "/";
        }
        var link=link1+link2;
        document.getElementById('searchform_special').action = link;
      }
    </script>
    
    <form role="search" method="get" id="searchform_special" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
      <div>
        <label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
        <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="<?php _e( 'Search for products', 'woocommerce' ); ?>" />
        <select onchange="avalcm_search_link(this.value)" >
           <option value=''>All</option>
       <?php
        $sql="SELECT taxonomy,slug, <code>name</code> from wp_term_taxonomy , wp_terms where wp_term_taxonomy.term_id=wp_terms.term_id and taxonomy in ('product_cat','') ORDER BY taxonomy ASC , term_taxonomy_id asc;";
        $allfilters = $wpdb->get_results($sql);
        foreach ($allfilters as $singlefilter) {
          echo '<option value="'. $singlefilter->slug.'">' . $singlefilter->name. '</option>';
        }
       ?>
       </select>
       <input type="submit" id="searchsubmit" value="<?php echo esc_attr__( 'Search' ); ?>" />
       <input type="hidden" name="post_type" value="product" />
       <input type="hidden" id="home_url" value="<?php echo esc_url( home_url( '/'  ) ); ?>" />
       <input type="hidden" id="categ_base_lang" value="<?php echo _x('product-category', 'slug', 'woocommerce') ?>" />
      </div>
    </form>

    So i change the action value of the form every time that i change value in select .

    I hope this will help someone

  4. Kamlesh
    Member
    Posted 1 year ago #

    Hi nikkyandrei,
    I have pasted this code in my header and added some categories in option tag also but when i search for something within a category the result it shows includes items from other category.
    Can you help me on this?
    I want to search based on any specific category which user selects from dropdown.
    Thanks.

  5. nikkyandrei
    Member
    Posted 1 year ago #

    can you share a link ?

  6. Kamlesh
    Member
    Posted 1 year ago #

    i modified the code to have only top most parent categories, here is the code snippet:

    <?php
    $args = array(
        'number'     => $number,
        'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,
        'include'    => $ids,
         'parent'    => 0
    );
    $product_categories = get_terms( 'product_cat', $args );
    ?>
        <select onchange="avalcm_search_link(this.value)" >
        <option value='' selected>Select a Category</option>
       <?php foreach( $product_categories as $cat ) {
    	echo '<option value="'. $cat->slug .'">' . $cat->name . '</option>';
    	}
       ?>
       </select>

    the link

  7. nikkyandrei
    Member
    Posted 1 year ago #

    I try to find word "baby", by all category and , by category . i see i works great. can u give me more details ? what word you use , for what category was the search , what products belong to other category
    You can write to me at nikkyandrei [at] gmail [dot] com

Topic Closed

This topic has been closed to new replies.

About this Topic