Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Results override behaviour › Reply To: Results override behaviour
Hi ernest – me again.
I’ve managed to create a mostly working solution to the issues above (where the results should be filterable) but could just do with your thoughts on one more thing. I appreciate this is out of bounds of plugin support, so please let me know if the fix is very complex and I won’t bother you anymore on this. But I’m hoping it’s a quick fix.
To summarize what we discussed before, I needed default woocommerce results to be displayed so they are filterable, which ajax search pro can do. I also needed the results to behave similarly to how ajax search pro produces preview results (search deeper in taxonomy, attributes). I now have a snippet that makes default woo results look deeper in taxonomy and attributes. It works exactly as I need but is missing one thing – displaying results from partial searches (like your plugin can do).
For example, I have a product tax value called ‘fashion’, it needs the full term to be typed for results to appear on archive. If I type ‘fash’, or ‘fashio’, and search, no results will appear. I’m trying to get it to work without the need for exact match.
Here’s the snippet that works with exact match only – if there is a quick fix to make it work with partial searches too, please let me know! Noone else on the internet has been able to help and I’ve tried everywhere! :
add_filter( ‘posts_search’, ‘woocommerce_search_product_tag_extended’, 999, 2 );
function woocommerce_search_product_tag_extended( $search, $query ) {
global $wpdb, $wp;
$qvars = $wp->query_vars;
if ( is_admin() || empty($search) || ! ( isset($qvars[‘s’])
&& isset($qvars[‘post_type’]) && ! empty($qvars[‘s’])
&& $qvars[‘post_type’] === ‘product’ ) ) {
return $search;
}
// Here set your custom taxonomies in the array
$taxonomies = array(‘product_cat’, ‘pa_region’);
$tax_query = array(‘relation’ => ‘OR’); // Initializing tax query
// Loop through taxonomies to set the tax query
foreach( $taxonomies as $taxonomy ) {
$tax_query[] = array(
‘taxonomy’ => $taxonomy,
‘field’ => ‘name’,
‘terms’ => esc_attr($qvars[‘s’]),
);
}
// Get the product Ids
$ids = get_posts( array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product’,
‘post_status’ => ‘publish’,
‘fields’ => ‘ids’,
‘tax_query’ => $tax_query,
) );
if ( sizeof( $ids ) > 0 ) {
$search = str_replace( ‘AND (((‘, “AND ((({$wpdb->posts}.ID IN (” . implode( ‘,’, $ids ) . “)) OR (“, $search);
}
return $search;
}