'Woocommerce Search Result showing post result not product result

I am using by default product search of woocommerce but search result is showing result like post NOT like product. my search get query is like mydomainname/?s=men&post_type=product means my theme only taking post so what can do for getting product type result. Any help. Thanks

<div class="advance-product-search">
<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <?php
        $terms = get_terms( array(
            'taxonomy'   => 'product_cat',
            'hide_empty' => true,
            'parent'     => 0,
        ) );
    ?>
    <?php if (  ! empty( $terms ) && ! is_wp_error( $terms ) ) : ?>
        <div class="advance-search-wrap">
            <?php $current = ( isset( $_GET['product_category'] ) ) ? absint( $_GET['product_category'] ) : ''; ?>
            <select class="select_products" name="product_category">
                <option value=""><?php esc_html_e( 'All Categories', 'wen-associate' ); ?></option>
                <?php foreach ( $terms as $cat ) : ?>
                    <option value="<?php echo esc_attr( $cat->term_id ); ?>" <?php selected( $current, $cat->term_id ); ?> ><?php echo esc_html( $cat->name ); ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif; ?>

    <div class="advance-search-form">
        <input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products&hellip;', 'wen-associate' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
        <input type="submit" value="&#xf002;" />
        <input type="hidden" name="post_type" value="product" />
    </div><!-- .advance-search-form -->

</form><!-- .woocommerce-product-search -->



Solution 1:[1]

You can add this function in Your function.php

add_action( 'pre_get_posts', 'search_woocommerce_only' );

function search_woocommerce_only( $query ) {
  if( ! is_admin() && is_search() && $query->is_main_query() ) {
    $query->set( 'post_type', 'product' );
  }
}

Solution 2:[2]

//Alter the WordPress search to return ONLY products, no pages
function my_search_filter($query) {
    if ( $query->is_search && ! is_admin() ) {
        $query->set( 'post_type', 'product' );
        $query->is_post_type_archive = true;
    }
}
add_filter('pre_get_posts','my_search_filter', 9);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 developerme
Solution 2 Stiliyan Stoyanov