• Resolved andy85

    (@andy85)


    Hi.

    I am trying to sort Woocommerce products in admin by relevance first and in stock. Is this possible? At the moment I am getting results based on relevance which is good, but the first 10 or so products are out of stock and then the in stock ones show. When o try to sort a column by stock after a search, the sorting doesn’t work

    When relevanssi is disabled I can search and show by in stock first using the normal search but with bad relevance, but I hope to be able to use relevanssi to sort by relevance and in stock for Woocommerce products first. Is this possible? I tried your in stock weight boost snippet but doesn’t seem to be working.

    Thanks

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

    (@msaari)

    First of all, the product search in the WP admin does not use Relevanssi reliably, even if you enable admin searches in Relevanssi. It just may not work, for reasons I don’t fully understand and have not been able to replicate.

    Anyway, the in-stock weight boost snippet works. Here’s how you should use it, in order to avoid heavy database load:

    function rlv_has_term( $term, $taxonomy, $post_id ) {
        global $wpdb, $relevanssi_taxonomy_cache;
        if ( isset( $relevanssi_taxonomy_cache[ $taxonomy ] ) ) {
            return isset( $relevanssi_taxonomy_cache[ $taxonomy ][ $term ][ $post_id ] );
        }
        $taxonomy_array = $wpdb->get_results(
            "SELECT t.slug, object_id FROM $wpdb->term_relationships AS tr,
                $wpdb->term_taxonomy AS tt, $wpdb->terms AS t
            WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
                AND tt.taxonomy = '$taxonomy' AND t.term_id = tt.term_id" );
        foreach ( $taxonomy_array as $term_object ) {
            $relevanssi_taxonomy_cache[ $taxonomy ][ $term_object->slug ][ $term_object->object_id ] = true;
        }
        return isset( $relevanssi_taxonomy_cache[ $taxonomy ][ $term ][ $post_id ] );
    }
    
    add_filter( 'relevanssi_match', 'rlv_woo_stocklevel' );
    function rlv_woo_stocklevel( $match ) {
        $in_stock = true;
        if ( rlv_has_term( 'outofstock', 'product_visibility', $match->doc ) ) {
          // Product is not in stock.
          $in_stock = false;
        }
        if ( $in_stock ) {
          // If product is in stock, multiply the weight by 10.
          $match->weight *= 10;
        }
        return $match;
    }

    If this doesn’t have any effect, then the search is not Relevanssi. The effect may not be big enough, in which case you can multiply the weight by 100 instead of multiplying by 10. That should create a more visible effect.

    You can also test this by using the Relevanssi admin search (Dashboard > Admin search). That’s guaranteed to use Relevanssi and it will also show you the weights, so you can see whether this has any effect or not.

    Another option is to not use weights, but instead just sort by taxonomy:

    add_filter( 'relevanssi_hits_filter', 'in_stock_first' );
    function in_stock_first( $hits ) {
      $in_stock        = array();
      $everything_else = array();
      foreach ( $hits[0] as $_post ) {
        $post_object = relevanssi_get_an_object( $_post )['object'];    
        $is_in_stock = true;
        if ( rlv_has_term( 'outofstock', 'product_visibility', $post_object->ID ) ) {
          $is_in_stock = false;
        }
        $is_in_stock ? array_push( $in_stock, $_post ) : array_push( $everything_else, $_post );
      }
      $hits[0] = array_merge( $in_stock, $everything_else );
      return $hits;
    }

    This also uses the rlv_has_term() function for increased performance, so make sure that is available. This function doesn’t care about weights, it just keeps the posts in the same relevance order, but puts all in-stock products first.

    • This reply was modified 2 years, 6 months ago by Mikko Saari.
    Thread Starter andy85

    (@andy85)

    Hi Mikko

    Thanks for the quick and detailed reply. I have tried these and the first one doesn’t seem to affect anything. When trying to add the second option I get a critical error when performing a search. Details below from the logs

    CRITICAL Uncaught Error: Call to undefined function rlv_has_term() in /home/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()'d code:30
    Stack trace:
    #0 /home//public_html/wp-includes/class-wp-hook.php(309): in_stock_first(Array)
    #1 /home/public_html/wp-includes/plugin.php(191): WP_Hook->apply_filters(Array, Array)
    #2 /home/public_html/wp-content/plugins/relevanssi/lib/search.php(603): apply_filters('relevanssi_hits...', Array, Object(WP_Query))
    #3 /home/public_html/wp-content/plugins/relevanssi/lib/search.php(84): relevanssi_do_query(Object(WP_Query))
    #4 /home/public_html/wp-includes/class-wp-hook.php(307): relevanssi_query(NULL, Object(WP_Query))
    #5 /home/public_html/wp-includes/plugin.php(235): WP_Hook->apply_filters(NULL, Array)
    #6 /home/public_html/wp-includes/class-wp-query.php(3055): apply_filters_ref_array('posts_pre_query', Array)
    #7 /home/ in /home/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(484) : eval()'d code on line 30

    Any ideas?

    Thanks

    Thread Starter andy85

    (@andy85)

    Hi Mikko

    Sorry, I misread your reply and I missed the rlv_has_term() function.

    This is all working now. Thank you very much

    dilusionz

    (@dilusionz)

    I’m dealing with the same problem here. Tried above snippets but error as well. How do I make sure rlv_has_term() is available ?

    Plugin Author Mikko Saari

    (@msaari)

    In general, it’s better to start a new thread and refer to this one from there instead of replying to a months-old resolved thread.

    Where have you placed the rlv_has_term() function? Is it in your theme functions.php?

    dilusionz

    (@dilusionz)

    Absolutely – apologies for the post necro. I actually managed to resolve it. There was another script causing an issue.
    Thankyou for your time & work – it’s much appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sort by relevance and in stock’ is closed to new replies.