• SE

    (@brandon-sheley)


    I’m working on a site and the person that touched it before me messed up a search box somehow on our product page. How can I setup a search box to search the woocommerce products only?

    I looked at the search widget to compare the code but everything I’ve tried hasn’t worked.

    Thanks
    -Brandon

    https://www.ads-software.com/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter SE

    (@brandon-sheley)

    Anyone with suggestions on this issue?

    Thanks

    I’d like to know the same thing! ??

    Try this:

    <form action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
         <input name="s" type="text" value="Enter product name..."  onblur="this.value = this.value || this.defaultValue;" onfocus="this.value = '';" />
         <input type="submit" value="Search" />
         <input type="hidden" name="post_type" value="product" />
    </form>

    Hello! Sorry to bump this old thread, but I have the same problem. I used the code suggested by sta777, but still no luck.

    My search link is this type: “site.com/?s=keyword”, but I need it to be like this: “site.com/?s=keyword&post_type=product”.

    Any suggestions?

    Thanks

    The code I posted does work and will display “site.com/?s=keyword&post_type=product” in the address bar.

    Where are you using the code? Have you copied it in full?

    Hi, sta777, thanks for the quick response.

    Actually I edited the code a little bit and used it like this, so it has a nice design for my theme:

    <div id="headersearchform-container" class="group">
        <form role="search" method="get" id="headersearchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" >
            <div class="group formborder">
                <input  type="text" value="" name="s" id="headers" placeholder="Ce produs cauti astazi?" />
                <input  type="submit" class="button" id="headersearchsubmit" value="Cauta" />
            </div>
            <input type="hidden" name="post_type" value="product" />
        </form>
    </div>

    And used it via a snippet in my home page. But it still has the part with:

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

    Which causes the “&post_type=product” to appear, no?

    Also the problems with my searches are a little bigger, if I try to search by a keyword with spaces in between, like “key%20word”, my search link looks like “https://site.com/?s=key%20word&#8221; and goes in an infinite loop.

    The search.php and searchfrom.php are the theme’s default:

    Here is searchform.php: https://pastebin.com/UHU8Bghm

    Here is search.php: https://pastebin.com/d3znLznz

    Have a look at this and see if it helps – https://www.remicorson.com/customize-woocommerce-products-search-form/

    ??

    I tried with the filter and the product-searchform.php file, nothing seems to work. The searches are the same, with no post_type.
    I tried it with twentytwelve theme and still no change, I guess this is not an error from my theme, but maybe from the woocommerce plugin?

    Are you getting any search results back at all?

    yes, results are showing, but only when i use a single keyword, if I search for something with spaces in between like “black shoes” for example, it gets into an infinite loop.

    Sounds like you may have an error in your loop…

    Problem solved! It was an error form YOAST SEO plugin, I had ticked the boxes for ‘?replytocom’ and ‘Redirect ugly URL’s to clean permalinks. (Not recommended in many cases!)’, under the ‘permanent links’ menu.
    As soon as I unticked them, the searches work perfect and &post_type=product is back in his place.
    Thanks for the help and suggestions sta777 ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘wooCommerce search is searching whole site, need products only’ is closed to new replies.