asp_results filter gives result where there is none…

Home Forums Product Support Forums Ajax Search Pro for WordPress Support asp_results filter gives result where there is none…

This topic contains 2 replies, has 2 voices, and was last updated by Jacques Letesson Jacques Letesson 7 years, 8 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7277
    Jacques Letesson
    Jacques Letesson
    Participant

    Dear Ernest,

    I use the filer asp_results to add some custom information to the results of the plugin.

    When I type ‘sdfsgshdh’, I obviously got the No results message and the write_log function output me an empty array.

    However with other keywords like ‘pommejo’ even if there is nothing in my DB called ‘pommejo’ I got a JS error on line 11 45 of jquery.ajaxsearchpro.js :

    Uncaught TypeError: Cannot read property ‘1’ of null

    Because my function write_log outputs me this array :

    [10-Jan-2016 19:13:16 UTC] Array
    (
        [keywords] => Array
            (
                [0] => pommejo
            )
    
        [nores] => 1
    )

    Could you please let me know what’s wrong?

    You can use the Admin Username / Password to log in to the website front-end. It’s protected right now.

    Thank you for your time.

    This is my PHP function :

    add_filter( 'asp_results', 'restofrais_asp_rewrite_title', 1, 1 );
    
    if ( ! function_exists( 'restofrais_asp_rewrite_title' ) ) {
     	function restofrais_asp_rewrite_title($results) {
          write_log($results);
          foreach ($results as $k=>$v) {
             $_pf = new WC_Product_Factory();
             if($product = $_pf->get_product($results[$k]->id)){
               if($terms = get_the_terms($product->id, 'product_cat')){
                   foreach($terms as $key => $term){
                       if($term->parent == 0){
                           $results[$k]->category = $term->term_id;
                       }
                   }
               } else {
                   $results[$k]->category = 'xx';
               }
    
               // Rewrite the title of the result based on the name of the product only (not the price)
               $results[$k]->title = $product->post->post_title;
               // Add an other object price containing the regular price of the product
               $results[$k]->price  = $product->get_price();
             }
    
           }
        // Sort the results by category
        usort($results, "cmp");
        return $results;
     	}
     }
    
    #7285
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    The issue is that there are keyword suggestions returned, but the filter is still executed. It’s my mistake, I accidentally changed this part in the recent version, and the “asp_results” filter executes when there are no result returned.

    Try adding this line as the first line to your function:

    if ( !empty($results['nores'] ) return $results;

    That should terminate execution if there are keyword suggestion available as result.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #7291
    Jacques Letesson
    Jacques Letesson
    Participant

    @Ernest It totally did the trick! Thank you man.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.