• Hi. I just tried the lite version with woocommerce. I wanted to search for sku so i activated the _sku field and product post type in the plugin settings. For single products it is functioning but for variable products it showed no results if i search for sku.

    So i also activated the product_variation post type. Now it is showing results for sku but not the main product. Instead it is showing variant #1234 from product xyz. When i click the result it shows an error that the url was not found.

    How can i search for sku of variable products and show the main product and not the variation itself?

    Thanks.

    https://www.ads-software.com/plugins/ajax-search-lite/

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

    (@wpdreams)

    Hi!

    I remember I solved this for someone in the pro version support forums. Let me look it up for you. I might be able to change the solution to work with the lite version.

    Plugin Author wpdreams

    (@wpdreams)

    Okay, I think I got this. Try to add the following code to your themes functions.php file:

    function fix_asl_prod_variation( $results ) {
      foreach ($results as $k=>$r) {
        // Regexp fixing the title
        $results[$k]->title  = preg_replace("/(Variation) \#(\d+) (of)/si", '', $results[$k]->title);
    
        // A trick to fix the url
        if ( $r->post_type == 'product_variation' &&
             class_exists('WC_Product_Variation') ) {
            $wc_prod_var_o = new WC_Product_Variation($r->id);
            $results[$k]->link = $wc_prod_var_o->get_permalink();
        }
      }
      return $results;
    }
    add_filter( 'asl_results', 'fix_asl_prod_variation', 1, 1 );

    It should remove the “Variation Of” part of the title and fix the broken url.

    Thread Starter Stefan1904

    (@stefan1904)

    Thanks for the fast answer.

    I tried your code and while the url is now correct, the title remains unchanged.

    Plugin Author wpdreams

    (@wpdreams)

    Okays. I just uploaded plugin version 3.5, which includes this fix in code, so I recommend updating and removing it from your theme functions.php file. (since it’s already in the plugin).

    I’m sorry for having you added the code, then remove it, I didn’t know I was going to be able to finish the new version today ??

    I’m hoping it will work after update. If not, then could you please tell an exact example of a variation title? Maybe it’s different from what the code is looking for in your case.

    Thread Starter Stefan1904

    (@stefan1904)

    Found the problem. I use german language so i had to change the preg_replace term. Now it is functioning. But how to make this multi language compatible? We use german and englisch language for our site.

    Another problem: If the main product has a SKU, for example 100 and a variation 100-S, both would be shown if i search for 100. I think this would be a little confusing for the user.

    One request: It would be nice if the shown product title also shows the variation attributs, for example Product title, size S, color red etc.

    Plugin Author wpdreams

    (@wpdreams)

    Hi!

    I don’t think it’s possible to make it multi language compatible with an ultimate solution. WooCommerce might have a built in function though which might help, but I’m not sure, will check on it.

    Try to turn on to search for exact matches only. I’m not sure if that helps though, custom fields are pretty tricky when it comes to search. If it doesn’t help, let me know and I can maybe suggest some kind of modification.

    Yes, this had been requested before, and I managed to test a few possible solutions, but none of them worked correctly. It’s a bit hard to operate with product attributes outside WooCommerce unfortunately. But I’m sure I will find a solution sooner or later.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Search SKU for variable products’ is closed to new replies.