Community Forums › Forums › Archived Forums › General Discussion › Woocommerece Product Search Bar
Tagged: bar, search, WooCommerce
- This topic has 4 replies, 4 voices, and was last updated 6 years, 11 months ago by
moma0503.
-
AuthorPosts
-
January 21, 2015 at 8:07 pm #138233
jfarr
MemberHow can I put a Woocommerce Product Search Bar in my primary navigation?
I currently have these hooks to add a general search bar in my navigation.
http://www.trade-tools.com/demo// Add search bar add_filter( 'wp_nav_menu_items', 'genesis_search_primary_nav_menu', 10, 2 ); function genesis_search_primary_nav_menu( $menu, stdClass $args ){ if ( 'primary' != $args->theme_location ) return $menu; if( genesis_get_option( 'nav_extras' ) ) return $menu; $menu .= sprintf( '<li class="custom-search">%s</li>', __( genesis_search_form( $echo ) ) ); return $menu; } // Customize search bar text add_filter( 'genesis_search_text', 'sp_search_text' ); function sp_search_text( $text ) { return esc_attr( 'Search...' ); } // Customize search bar button add_filter( 'genesis_search_button_text', 'sp_search_button_text' ); function sp_search_button_text( $text ) { return esc_attr( '' ); }
January 23, 2015 at 3:24 am #138396nickjco
MemberFirst we need to modify the search form and add the hidden input field:
<input type="hidden" name="post_type" value="product">
This will tell search that we are searching for a specific post type. Here is the function I created with the hidden input field.
function custom_products_search( $form, $search_text, $button_text, $label ) { $form = sprintf( '<form method="get" class="search-form" action="%s" role="search">%s<input type="search" name="s" placeholder="%s" /><input type="submit" value="%s" /><input type="hidden" name="post_type" value="product"></form>', home_url( '/' ), $label, $search_text, esc_attr( $button_text ) ); return $form; }
and then I modified your function above to with the add_filter and remove_filter lines:
function genesis_search_primary_nav_menu( $menu, stdClass $args ){ if ( 'primary' != $args->theme_location ) return $menu; if( genesis_get_option( 'nav_extras' ) ) return $menu; add_filter( 'genesis_search_form', 'custom_products_search', 10, 4 ); $menu .= sprintf( '<li class="custom-search">%s</li>', __( genesis_search_form( $echo ) ) ); remove_filter( 'genesis_search_form', 'custom_products_search', 10 ); return $menu; }
I added the filter in this function so that it will only affect that form you are adding. Hope this helps!
January 24, 2015 at 8:37 am #138505jfarr
Member@nickjco thank you for your response! Where do I put <input type="hidden" name="post_type" value="product"> ? Do I have to put it in index.php? Is there any way I can put it in through my menu or by a hook? Also this will not affect my 404 search bar correct. Thank you again.
January 30, 2015 at 12:24 am #139016Wooassist
MemberHey @jfarr, the hidden input field is already added in the function
custom_products_search
. You'll just have to add this to your functions.php and replace your original function ofgenesis_search_primary_nav_menu
with the function I posted above.April 14, 2016 at 12:25 pm #183615moma0503
MemberThis has helped me out tremendously. Thank you both.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.