Merging a general wordpress search with a woocommerce product search?

I am using the following two codes to generate a wordpress search and a woo commerce product search? Is there a code to merge theme into one search bar doing both functions?

  <?php get_search_form(); ?>
  <?php get_product_search_form(); ?>

Related posts

Leave a Reply

3 comments

  1. get_search_from(); covers both.
    get_product_search_form() is same as get_search_from() just that it restricts the wordpress search to products only.

    If you see the markup of both the functions, there’s only one difference. There is an additional post type value set as a product.

    <input type="hidden" name="post_type" value="product" />
    

    So,

    get_search_from() >> get_product_search_form()
    
  2. I used the plugin Relevanssi which allows you to include custom post types (including woocommerce products) in the search results along with posts.

  3. For those using JQuery, you can easily append the hidden post type input to any search form when the page is loaded. Just add the hidden input that specifies which type of search to run like the following example that I had to do for the DIVI search bar to search products instead of blog:

    <script>
        jQuery('document').ready(function(e){
            var productSearchSetting = '<input type="hidden" name="post_type" value="product" />'; // product post type, use 'any' for all post types
            var diviSearchForm$ = jQuery('header form.et-search-form');
            console.log('adding search for products to: ', diviSearchForm$ );
            diviSearchForm$.append(productSearchSetting);
        })
    </script>
    

    You can adapt this to your own site by changing around the selector in the jQuery line that defines the diviSearchForm$ reference to whatever form(s) you want to target.

    Also, if you want to search all post types and not just product, change the value product value to any