Support » Plugin: Ajax Search Lite » product number search

  • Resolved neueweide

    (@neueweide)


    Hi i have a client he wants his product numbers searchable…

    the problem ist his product numbers are not fix strings.
    the number of one product can have multiple variations it depends on differents adapter wich can be combined with the product. so in each configuration the same product has a different product number.

    For example a base product (with base number B-544) can result in diffeent product numbers:
    B-544A
    B-544A-g4
    B-544B-c3
    B-544C-Ah
    etc….

    the clients don’t want to write down all this variations …

    so my question is, is there a posssible workaround to search for a subpattern in the search-string?
    When a website user searches for A-544-g4 the ajax search should also show the Product with the number A-544 !
    Actually this kind of reverse substring search is not possible!

    best robert

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author wpdreams

    (@wpdreams)

    Hi,

    This is a tough one, and is probably not possible. However maybe doing something like removing the “-” dash symbols programmatically could actually work to some extent.

    Try this custom code 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('asl_search_phrase_before_cleaning', 'asl_replace_characters', 10, 1);
    
    function asl_replace_characters( $s ) {
      $characters = "',._-?!"; // Type characters one after another
      $replace_with = ' ';     // Replace them with this (space by default)
    
      if ( is_array($s) ) {
        if ( isset($s['s']) && !$s['_ajax_search'] ) 
          $s['s'] = str_replace(str_split($characters), $replace_with, $s['s']);      
      } else {
        $s = str_replace(str_split($characters), $replace_with, $s);
      }
    
      return $s; 
    }

    With this code enabled, searching “A-544-g4” will actually search “A 544 g4”. Now, if you set the keyword logic to OR, then it will match the original product “A-544”.

    Best,
    Ernest M.

    Thread Starter neueweide

    (@neueweide)

    Oh wow Ernest,

    thank you so much for this hint …

    i never thought about such a simple string operation 😉

    i just changed a bit your code, especially to use REGEX wich can be much more accurate in string replacements 🙂

    the new code:

    add_filter('asl_search_phrase_before_cleaning', 'asl_replace_characters', 10, 1);
    function asl_replace_characters( $s ) {
    
        $characters = '/(?<!B)[.-]/';
        $replace_with = ' ';
        if ( is_array($s) ) {
            if ( isset($s['s']) && !$s['_ajax_search'] )
                $s['s'] = preg_replace($characters, $replace_with, $s['s']);
        } else {
            $s = preg_replace($characters, $replace_with, $s);
        }
    
        return $s;
    }

    I also added a negative lookahead because i do not want the script to search for a single character like “B”… this ended up in unqualified results.

    thanks a lot
    best robert

    Plugin Author wpdreams

    (@wpdreams)

    That looks really great, thank you for sharing 🙂

    If you don’t mind, I will close this topic soon and mark it as resolved. Feel free to rate the plugin, it is greatly appreciated.

    Best,
    Ernest M.

    Thread Starter neueweide

    (@neueweide)

    Yes you can close it as resolved 🙂

    thanks again and i will leave you a 5 star rating !

    best robert

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘product number search’ is closed to new replies.