This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Displaying products with images first

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Displaying products with images first

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #25426
    shubham05shubham05
    Participant

    Hello,

    How can I display the search results with images over the ones without images? I want to give priority to search results of products with images.

    Thank you

    #25473
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Well, I’m afraid there is no way to do that at the moment via settings. It might be doable though via custom code.

    Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter('asp_results', 'asp_image_results_first', 10, 1);
    function asp_image_results_first( $results ) {
    	$image_res = array();
    	$non_image_res = array();
    	foreach ( $results as $k => &$r ) {
    		if ( !empty($r->image) ) {
    			$image_res[] = $r;
    		} else {
    			$non_image_res[] = $r;
    		}
    	}           
    	return $image_res + $non_image_res;
    }
    #25477
    shubham05shubham05
    Participant

    It doesn’t seem to work, I added it to the functions.php 🙁

    #25495
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    You can try this variation maybe, but I am not sure if it would work:

    add_filter('asp_results', 'asp_image_results_first', 10, 1);
    function asp_image_results_first( $results ) {
    	$image_res = array();
    	$non_image_res = array();
    	foreach ( $results as $k => &$r ) {
    		if ( !empty($r->image) ) {
    			$image_res[] = $r;
    		} else {
    			$non_image_res[] = $r;
    		}
    	}           
    	return array_merge($image_res, $non_image_res);
    }
    #28258
    shubham05shubham05
    Participant

    Hi,

    This did not work. Can you also tell me how to add Product SKUs to the search results? I am using woo commerce!

    #28264
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    This documentation will help you wit that. The SKU field name is _sku

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Displaying products with images first’ is closed to new replies.