Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › asp_results filter gives result where there is none…
- This topic has 2 replies, 2 voices, and was last updated 10 years, 4 months ago by
Jacques Letesson.
-
AuthorPosts
-
January 10, 2016 at 8:47 pm #7277
Jacques Letesson
ParticipantDear 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 :
[php][10-Jan-2016 19:13:16 UTC] Array
(
[keywords] => Array
(
[0] => pommejo
)[nores] => 1
)[/php]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 :
[php]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;
}
}
[/php]-
This topic was modified 10 years, 4 months ago by
Jacques Letesson.
January 11, 2016 at 11:00 am #7285Ernest Marcinko
KeymasterHi!
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:
[php]if ( !empty($results[‘nores’] ) return $results;[/php]
That should terminate execution if there are keyword suggestion available as result.
January 11, 2016 at 7:34 pm #7291Jacques Letesson
Participant@Ernest It totally did the trick! Thank you man.
-
This topic was modified 10 years, 4 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.