• I’m running Relevanssi on a Flatsome themed site and also using the Product GTIN (EAN, UPC, ISBN) for WooCommerce plugin and I’m having an issue with variable products for some reason.

    Flatsome’s AJAX search will pull parent products based off of UPC codes but it won’t pull children/variations for some reason. I tried running an admin search on a variable UPC and it pulls it up there so I’m not exactly sure what the disconnect is. I’m running the following snippet for Relevanssi to pull SKUs for variable products so I’d imagine it just needs something similar for UPCs, are you able to offer any insight on this please?

    // RELEVANSSI VARIABLE SKU SEARCH
    add_filter('relevanssi_content_to_index', 'rlv_index_variation_skus', 10, 2);
    function rlv_index_variation_skus($content, $post) {
        if ($post->post_type == "product") {
            $args = array('post_parent' => $post->ID, 'post_type' => 'product_variation', 'posts_per_page' => -1);
            $variations = get_posts($args);
            if (!empty($variations)) {
                foreach ($variations as $variation) {
                    $sku = get_post_meta($variation->ID, '_sku', true);
                    $content .= " $sku";
                }
            }
        }
     
        return $content;
    }

    Also another issue that I’m having is that Flatsome’s AJAX search will pull SKUs correctly but if I just hit enter instead of letting the AJAX results load then it takes me to the shop page and says no products found. I’m running Search & Filter Pro for my shop page if that gives you any insight.

    Any info you’re able to provide on either of these issues would be incredibly appreciated, thank you for your time.

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

    (@msaari)

    If the Relevanssi admin search (Dashboard > Admin search) finds something the Flatsome Ajax search doesn’t, that means the Flatsome Ajax search is not using Relevanssi. I’m not sure what’s up with that; it sometimes work and sometimes doesn’t. There’s a way to make it always work, though, and that’s documented here. Does that fix your problem?

    When debugging the problems, it’s also a good idea to try disabling Search & Filter Pro to rule out if that has an effect on anything.

    Thread Starter lateralus821

    (@lateralus821)

    Thanks for getting back to me so quickly! I forgot to mention that I already tried both of those snippets. They help to get the AJAX search to pull UPCs for simple products but it doesn’t work for variable products.

    Plugin Author Mikko Saari

    (@msaari)

    Ok, in that case try disabling Search & Filter Pro. Does that have any effect on this?

    Thread Starter lateralus821

    (@lateralus821)

    Hey Mikko,

    I tried disabling S&FP and that didn’t help, I’m still unable to search for variable product UPCs. Simple and variable SKUs work as well as simple UPCs but no variable UPCs.

    I did notice that my second issue regarding SKUs is straightened out both with SF&P enabled and disabled though – if I punch a SKU into search and hit enter before the Flatsome AJAX search loads it takes me right to the product page.

    Plugin Author Mikko Saari

    (@msaari)

    I just realized you’re talking about UPC, and not just SKU. Yes, you need to amend the variation SKU function so that it also adds the UPC. Where’s the UPC stored?

    Thread Starter lateralus821

    (@lateralus821)

    I am using the Product GTIN (EAN, UPC, ISBN) for WooCommerce plugin and it stores the UPC meta as _wpm_gtin_code.

    Plugin Author Mikko Saari

    (@msaari)

    Fixing that isn’t complicated. Change your SKU function to this:

    // RELEVANSSI VARIABLE SKU SEARCH
    add_filter('relevanssi_content_to_index', 'rlv_index_variation_skus', 10, 2);
    function rlv_index_variation_skus($content, $post) {
        if ($post->post_type == "product") {
            $args = array('post_parent' => $post->ID, 'post_type' => 'product_variation', 'posts_per_page' => -1);
            $variations = get_posts($args);
            if (!empty($variations)) {
                foreach ($variations as $variation) {
                    $sku = get_post_meta($variation->ID, '_sku', true);
                    $upc = get_post_meta($variation->ID, '_wpm_gtin_code', true);
                    $content .= " $sku $upc";
                }
            }
        }
     
        return $content;
    }
    Thread Starter lateralus821

    (@lateralus821)

    That worked perfectly, thank you!

    Another issue I ran into in the process of getting this working however is tied to the below code. In order to use Flatsome’s AJAX search with SKUs or UPCs I had to add the following code but it made the search results a bit odd. The top of the list will be the product that is searched for but it will also add about 50 completely unrelated products below it. Before this snippet it would only pull the actual product or anything relevant to the search terms, do you have any ideas on how I might be able to stop all of the extra products from loading?

    function rlv_flatsome_search_function( $query, $args, $defaults ) {
      $_wp_query = new WP_Query();
      $_wp_query->parse_query( $args );
      relevanssi_do_query( $_wp_query );
      return $_wp_query->posts;
    }
    Plugin Author Mikko Saari

    (@msaari)

    The snippet should run a search with exactly the same parameters as the Flatsome default search does.

    If you change the function to this:

    function rlv_flatsome_search_function( $query, $args, $defaults ) {
      error_log( wp_json_encode( $args ) );
      $args['relevanssi'] = true;
      return get_posts( $args );
    }

    and then run a search and look at the server PHP error log, what does that print out in there?

    Thread Starter lateralus821

    (@lateralus821)

    I tried that and ran a few searches but it didn’t output any errors.

    Plugin Author Mikko Saari

    (@msaari)

    Are you perhaps missing the line that activates the function?

    add_filter( 'flatsome_ajax_search_function', function() { return 'rlv_flatsome_search_function'; } );

    Thread Starter lateralus821

    (@lateralus821)

    That line is there but I had forgot to copy/paste it in my earlier message.

    Plugin Author Mikko Saari

    (@msaari)

    You can also try this:

    function rlv_flatsome_search_function( $query, $args, $defaults ) {
      error_log( wp_json_encode( $args ), 1, '[email protected]' );
      $args['relevanssi'] = true;
      return get_posts( $args );
    }

    This should email you instead of writing to the error log.

    Thread Starter lateralus821

    (@lateralus821)

    That worked perfectly, below is the emailed error log.

    {"s":"","orderby":"relevance","post_type":["product","product_variation"],"post_status":"publish","posts_per_page":100,"ignore_sticky_posts":1,"post_password":"","suppress_filters":false,"order":"DESC","meta_query":[{"key":"_sku","value":"850002123159"}],"tax_query":[{"taxonomy":"product_visibility","field":"term_taxonomy_id","terms":6,"operator":"NOT IN"}]}

    Plugin Author Mikko Saari

    (@msaari)

    Ah, I see. That’s the SKU product search. Can you disable that from Flatsome settings? It shouldn’t be necessary with Relevanssi.

    If you can’t, this should do the trick:

    add_filter( 'theme_mod_search_by_sku', '__return_zero' );

    Does disabling the SKU search help?

Viewing 15 replies - 1 through 15 (of 31 total)
  • The topic ‘Issue with UPCs’ is closed to new replies.