Combining generic WordPress search with woocommerce product searches?

Am I using the following two codes to generate a search in Wordpress and search for a product on the Internet? Is there any code to combine themes into one search bar that performs both functions?

<?php get_search_form(); ?> <?php get_product_search_form(); ?> 
+4
source share
4 answers

get_search_from (); covers both.
get_product_search_form () is similar to get_search_from () only to restrict wordpress searches to products only.

If you see the markup of both functions, there is only one difference. An additional message type value is specified as the product.

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

So,

get_search_from () → get_product_search_form ()

+9
source

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

+2
source

For those using jQuery, you can easily add a hidden post type to any search form when the page is loaded. Just add a hidden input that indicates what type of search to run, as in the following example, which I had to do for the DIVI search panel to search for products instead of a 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 switching around the selector in the jQuery line, which defines diviSearchForm$ to whatever form (s) you want to target.

In addition, if you want to search for all types of messages, not just the product, change the value of product to any

0
source

You mention this adaptation to work elsewhere.

I tried to get this to work with the Divi search module, I believe the form identifier is et_pb_searchform. How can this be done to search for Woocommerce products from the Divi search module?

0
source

Source: https://habr.com/ru/post/1468787/


All Articles