Support » Plugin: Relevanssi - A Better Search » Results displayed for partial word query, but not for the full one

  • Hi,

    I’m facing a rather strange situation. I use Relevanssy to search in brand and product category fields and it seems to work fine, but there is one strange exception.

    One of the brands, and product tags is Aesop.
    If I search for “Aeso” results are displayed.
    If I search for “Aesop” no results are displayed.

    I tried searching in Admin area and here I only get 2 results (none of them having Aesop in title or content), so I suppose the query gets them from brand or product tag. But only 2, although there are tens of Aesop products.

    I have added the code necessary for admin search in functions.php. I also tried to use Query Monitor, but I was able to get anything out of it :o(.

    How can we solve this?

    Best regards
    Alex

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Searching for “aesop” leads to an automatic redirect to /category/uncategorized/. That’s odd.

    What is the code you’re using in functions.php?

    You can try disabling the WooCommerce auto-redirect for search results to see if that helps:

    add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );

    I added the suggested code, but nothing changed.

    I discovered the error is generated by the pwb-brand plugin. I tried to regenerate the index without the pwb-brand taxonomy, but nothing changed.

    Unfortunately I really need the pwb plugin, so I need to find an work-around. Do you have any suggestion?

    The current code in functions.php is:

    <?php

    /**
    * Vayne Theme Function
    *
    */

    add_action( ‘after_setup_theme’, ‘vayne_child_theme_setup’ );
    add_action( ‘wp_enqueue_scripts’, ‘vayne_child_enqueue_styles’, 20);

    if( !function_exists(‘vayne_child_enqueue_styles’) ) {
    function vayne_child_enqueue_styles() {
    wp_enqueue_style( ‘vayne-child-style’,
    get_stylesheet_directory_uri() . ‘/style.css’,
    array( ‘vayne-theme’ ),
    wp_get_theme()->get(‘Version’)
    );
    }
    }

    if( !function_exists(‘vayne_child_theme_setup’) ) {
    function vayne_child_theme_setup() {
    load_child_theme_textdomain( ‘vayne-child’, get_stylesheet_directory() . ‘/languages’ );
    }
    }

    /**
    * Allow HTML in term (category, tag) descriptions
    */
    foreach ( array( ‘pre_term_description’ ) as $filter ) {
    remove_filter( $filter, ‘wp_filter_kses’ );
    if ( ! current_user_can( ‘unfiltered_html’ ) ) {
    add_filter( $filter, ‘wp_filter_post_kses’ );
    }
    }

    foreach ( array( ‘term_description’ ) as $filter ) {
    remove_filter( $filter, ‘wp_kses_data’ );
    }

    // resize logos
    //
    add_filter(‘wp_sir_enable_category_image’, ‘__return_true’);

    add_filter(‘wp_sir_allowed_taxonomies’, function(){
    return ‘pwb-brand’;
    });

    add_filter(‘wp_sir_is_attached_to’, function($return, $attachment_id){

    $args = [
    ‘meta_query’ => [
    [
    ‘key’ => ‘pwb_brand_image’,
    ‘value’ => $attachment_id
    ]
    ]
    ];

    $count = wp_count_terms( ‘pwb-brand’, $args );

    if ( intval($count) > 0 ) {
    return true;
    }

    return $return;

    }, 10, 2);

    add_filter( ‘rest_attachment_query’, function($args, $request ){

    if ( ! $request->get_param( ‘is_regeneratable’ ) ) {
    return $args;
    }

    $logos = get_terms([
    ‘hide_empty’ => false,
    ‘taxonomy’=> ‘pwb-brand’,
    ‘fields’=>’ids’
    ]);

    $ids = [];
    foreach($logos as $logo_id){
    $ids[] = (int)get_term_meta($logo_id, ‘pwb_brand_image’, true);
    }
    $ids = array_filter($ids);

    $args[ ‘post__in’ ] = empty( $args[ ‘post__in’ ] )
    ? $ids
    : array_merge( (array)$args[ ‘post__in’ ], $ids );

    return $args;

    }, 99, 2);

    add_filter( ‘relevanssi_search_ok’, ‘rlv_product_search_override’, 10, 2 );
    function rlv_product_search_override( $ok, $query ) {
    if ( isset( $query->query_vars[‘product_search’] ) ) $ok = true;
    return $ok;
    }

    add_filter( ‘relevanssi_modify_wp_query’, ‘rlv_product_search_modify’ );
    function rlv_product_search_modify( $query ) {
    if ( isset( $query->query_vars[‘product_search’] ) ) {
    $s = $_GET[‘s’];
    $query->set( ‘s’, $s );
    }
    return $query;
    }

    add_filter( ‘woocommerce_redirect_single_search_result’, ‘__return_false’ );

    // Utility: Get the product brand term names (from the product ID)
    function wc_get_product_brand( $product_id ) {
    return implode(‘, ‘, wp_get_post_terms($product_id, ‘pwb-brand’, [‘fields’ => ‘names’]));
    }

    // Display product brand in Cart and checkout pages
    add_filter( ‘woocommerce_cart_item_name’, ‘customizing_cart_item_name’, 10, 3 );
    function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item[‘data’]; // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $cart_item[‘product_id’] ) ) {
    if ( is_cart() )
    return sprintf(‘%s %s‘, esc_url($permalink), $brand, $product->get_name());
    else
    return $brand . ‘ ‘ . $product_name;
    }
    return $product_name;
    }

    // Display product brand in order pages and email notification
    add_filter( ‘woocommerce_order_item_name’, ‘customizing_order_item_name’, 10, 2 );
    function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product(); // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $item->get_product_id() ) ) {
    if ( is_wc_endpoint_url() )
    return sprintf(‘%s %s‘, esc_url($permalink), $brand, $product->get_name());
    else
    return $brand . ‘ ‘ . $product_name;
    }
    return $product_name;
    }

    Plugin Author Mikko Saari

    (@msaari)

    Yes, I see – the Perfect Brands plugin has a function that does a brand name redirect, and that’s messing up with the Relevanssi search. I’ve made a pull request to the plugin authors for a way to disable the PWB brand search.

    Meanwhile, you could try adding this to your functions.php:

    add_filter( 'init', function() {
        remove_class_action( 'search_by_brand_name', 'Perfect_Woocommerce_Brands' );
    });
    
    function remove_class_action($tag, $class = '', $method, $priority = null) {
        global $wp_filter;
        if (isset($wp_filter[$tag])) {
            $len = strlen($method);
    
            foreach($wp_filter[$tag] as $_priority => $actions) {
    
                if ($actions) {
                    foreach($actions as $function_key => $data) {
    
                        if ($data) {
                            if (substr($function_key, -$len) == $method) {
    
                                if ($class !== '') {
                                    $_class = '';
                                    if (is_string($data['function'][0])) {
                                        $_class = $data['function'][0];
                                    }
                                    elseif (is_object($data['function'][0])) {
                                        $_class = get_class($data['function'][0]);
                                    }
                                    else {
                                        return false;
                                    }
    
                                    if ($_class !== '' && $_class == $class) {
                                        if (is_numeric($priority)) {
                                            if ($_priority == $priority) {
                                                //if (isset( $wp_filter->callbacks[$_priority][$function_key])) {}
                                                return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                            }
                                        }
                                        else {
                                            return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                        }
                                    }
                                }
                                else {
                                    if (is_numeric($priority)) {
                                        if ($_priority == $priority) {
                                            return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                        }
                                    }
                                    else {
                                        return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                    }
                                }
    
                            }
                        }
                    }
                }
            }
    
        }
    
        return false;
    }

    I tried adding the suggested code, but I get this error:

    Your PHP code changes were rolled back due to an error on line 151 of file wp-content/themes/vayne-child/functions.php. Please fix and try saving again.

    Uncaught ArgumentCountError: Too few arguments to function remove_class_action(), 2 passed in wp-content/themes/vayne-child/functions.php on line 149 and at least 3 expected in wp-content/themes/vayne-child/functions.php:151
    Stack trace:
    #0 wp-content/themes/vayne-child/functions.php(149): remove_class_action(‘search_by_brand…’, ‘Perfect_Woocomm…’)
    #1 wp-includes/class-wp-hook.php(287): {closure}(”)
    #2 wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
    #3 wp-includes/plugin.php(478): WP_Hook->do_action(Array)
    #4 wp-settings.php(540): do_action(‘init’)
    #5 wp-config.php(90): require_once(‘/home/madisonp/…’)
    #6 wp-load.php(37): req

    Plugin Author Mikko Saari

    (@msaari)

    remove_class_action( 'search_by_brand_name', 'Perfect_Woocommerce_Brands' );

    should be

    remove_class_action( 'pre_get_posts', 'search_by_brand_name', 'Perfect_Woocommerce_Brands', );

    Hi,

    I really appreciate your support. It means a lot to have your focus on this.

    The last correction got rid of the error, but, unfortunately, the code does not solve the issue. It’s still the same redirect to the uncategorized page for every search on a full brand query term.

    Is there anything else we could do until pwb hopefully answer your request?

    Many thanks
    Alex

    Plugin Author Mikko Saari

    (@msaari)

    Well, if you don’t mind editing PWB files, you could just take the fixed version I did and use that already.

    Replace the file classes/class-perfect-woocommerce-brands.php with this file: https://github.com/msaari/perfect-woocommerce-brands/blob/master/classes/class-perfect-woocommerce-brands.php

    Then add this to theme functions.php:

    add_filter( 'pwb_prevent_brand_redirect', '__return_true' );

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.