need help with woocommerce search parameter

I have a request from a client to change the woocommerce search that is integrated in the theme. We want to search everything (posts , pages, products) on the Woocommerce search.

Woocommerce adds a paramater in the search php file and it is:<input type="hidden" name="post_type" value="product" />

current situation when click search: https://s*****lan.com/?s=pure&post_type=product

wanted situation when click search: https://s*****lan.com/?s=pure

  • Luís

    Hi Thomas ,

    Hope you're doing well today!

    I made a few more tests in my site after our live chat, and I will suggest you to try the following:

    1) Firstly, remove the hidden field on the Woocommerce form, this one:

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

    2) Then, add this code to your theme functions.php or via mu-plugin in order to search the different post types(In my example, I just added support for Posts, Pages and Products):

    add_filter( 'pre_get_posts', 'change_woo_search' );
    function change_woo_search( $query ) {
    
        if ( $query->is_search ) {
    	$query->set( 'post_type', array( 'post', 'products', 'pages' ) );
        }
    
        return $query;
       }

    Note: As you are modifying theme core files, consider using a child theme to override the default files, otherwise, once you updated the theme, you will lose these changes;

    If this doesn't work for you, can you send us the FTP access to your site, so we can give a look? Please send it privately through our contact form:

    https://premium.wpmudev.org/contact/#i-have-a-different-question

    Send in:
    Subject: "Attn: Luis Lopes"
    - WordPress admin username
    - WordPress admin password
    - Login url
    - FTP credentials (host/username/password)
    - Link back to this thread for reference
    - Any other relevant urls/information

    Cheers, Luís

  • Thomas

    Hey Luis,

    Thats awesome, thanks. It works well.
    However... There is one problem with this setup. The ajax search feature we like so much is now broken. It endlessly searches.

    The way I was trying was just to overrule the mother file.
    The file is included in the main template functions.php

    //Product Search
    include_once('inc/woocommerce/product-search.php');

    In that file when I change product to all it works with ajax search!
    I will send ftp and admin by contactform.

    Regards,
    Thomas

  • Panos

    Hi Thomas ,

    You mean the products that appear under the search text box after typing some search words right?

    This is sort of bonded to the theme. We can't provide support for this as it requires some amount of custom coding also doesn't provide direct hooks.

    I have prepared a snippet which you can use as a guide. It's a hacky approach but it seems to be close to what you need, so here you go:

    function wpmudev_custom_search_posts_results( $post_id ) {
    
    	$out = $post_id;
    	//$post_thumb = get_the_post_thumbnail( $post_id, array(182,182), array( 'class' => 'alignleft' ) );
    	$post_thumb_url = get_the_post_thumbnail_url( $post_id, array(182,182) );
    	$post_thumb = "<img src='$post_thumb_url' width='182' height='182' />";
    
    	ob_start();
    	?>
    	<div class="product_thumbnail_wrapper">
    		<div class="product_thumbnail" style=>
    			<a href="<?php echo get_permalink( $post_id ); ?>">
    				<span class="product_thumbnail_background"></span>
    				<?php echo $post_thumb; ?>
    			</a>
    		</div>
    	</div>
    	<h3>
    		<a class="product-title-link" href="<?php echo get_permalink( $post_id ); ?>">
    			<?php echo get_the_title( $post_id ); ?>
    		</a>
    	</h3>
    	<?php
    	$out = ob_get_clean();
    
    	return "<li class=column>{$out}</li>";
    }
    
    function wpmudev_custom_search_products_shortcode($params = array(), $content = null) {
    	extract(shortcode_atts(array(
    		'ids'     => ''
    	), $params));
    
    	global $shopkeeper_theme_options;
    
    	$output = '<div class="woocommerce columns-8">';
    	$output .= '<ul id="products-grid" class="products small-up-4 medium-up-4 large-up-5 xlarge-up-6 xxlarge-up-8">';
    
    	$ids = explode( ',', $params['ids']);
    
    	foreach ( $ids as $product_id ) {
    		$product = wc_get_product( $product_id );
    
    		if ( ! $product ) {
    			$output .= wpmudev_custom_search_posts_results( $product_id );
    			continue;
    		}
    
    		$output .= '<li class="column">';
    
    		if ( (isset($shopkeeper_theme_options['second_image_product_listing'])) && ($shopkeeper_theme_options['second_image_product_listing'] == "1" ) ) {
    
    			$product_thumbnail_second = [];
    			$attachment_ids = $product->get_gallery_image_ids();
    			if ( $attachment_ids ) {
    				$loop = 0;
    				foreach ( $attachment_ids as $attachment_id ) {
    					$image_link = wp_get_attachment_url( $attachment_id );
    					if (!$image_link) continue;
    					$loop++;
    					$product_thumbnail_second = wp_get_attachment_image_src($attachment_id, 'shop_catalog');
    					if ($loop == 1) break;
    				}
    			}
    
    			$style = '';
    			$class = '';
    			if (isset($product_thumbnail_second[0])) {
    				$style = 'background-image:url(' . $product_thumbnail_second[0] . ')';
    				$class = 'with_second_image second_image_loaded';
    			}
    		}
    
    		$instock_class = '';
    		if( !$product->is_in_stock() ){
    			$instock_class = 'outofstock';
    		}
    
    		$output .= '<div class="product_thumbnail_wrapper ' . $instock_class . '">';
    
    		$output .= '<div class="product_thumbnail ' . $class . '">';
    		$output .= '<a href="' . get_permalink( $product->get_id() ) . '">';
    		$output .= '<span class="product_thumbnail_background" style="'. $style .'"></span>';
    
    		if ( has_post_thumbnail( $product->get_id() ) ) {
    			$output .= get_the_post_thumbnail( $product->get_id(), 'shop_catalog');
    		} else {
    			$output .= apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', wc_placeholder_img_src() ), $product->get_id() );
    		}
    
    		$output .= '</a></div>';
    
    		if ( (isset($shopkeeper_theme_options['catalog_mode'])) && ($shopkeeper_theme_options['catalog_mode'] == 0) ) {
    			if ( $product->is_on_sale() ) :
    				$output .= '<span class="onsale">' . esc_html__( 'Sale!', 'woocommerce' ) . '</span>';
    			endif;
    
    			if ( !$product->is_in_stock() && !empty($shopkeeper_theme_options['out_of_stock_label']) ) {
    	            $output .= '<div class="out_of_stock_badge_loop">' . esc_html( $shopkeeper_theme_options['out_of_stock_label'], 'woocommerce' ) . '</div>';
    	        }
    	    }
    
    		$output .= '</div>';
    
    		$output .= '<h3><a class="product-title-link" href="' . get_permalink( $product->get_id() ) . '">' . $product->get_title() . '</a></h3>';   
    
    	    $output .= '<div class="product_after_shop_loop">';					
    
    		if ( $price_html = $product->get_price_html() ) :
    			$output .= '<span class="price">' . $price_html . '</span>';
    		endif;
    
    		$output .= '</div>';
    		$output .= '</li>';
    
    	}
    
    	$output .= '</ul>';
    	$output .= '</div>';
    
    	return $output;
    }
    
    add_action( 'init', function(){
    	remove_shortcode('search_products');
    	add_shortcode('search_products', 'wpmudev_custom_search_products_shortcode');
    }, 999 );
    
    add_filter( 'search_products_args', function( $args ){
    
    	$args['post_type'] = 'any';
    	$products = get_posts( $args );
    	$ids = '';
    
    	if ( !empty( $products ) ) {
    
    		foreach ( $products as $post ) {
    			$ids .= $post->ID . ',';
    		}
    	}
    	wp_reset_postdata();
    
    	if( !empty( $ids ) ) {
    		$output[ 'suggestions' ] = (string) do_shortcode('[search_products columns="8" orderby="title" order="" ids="' . rtrim($ids,',') . '"]');
    	} else {
    		$output['suggestions'] =  '';
    	}
    
    	echo json_encode( $output );
    	die();
    });

    You can give this a try. Hope it helps :slight_smile:

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.