Posted on

WooCommerce: Return only exact matches for SKU searches

This little filter function works on relevanssi_hits_filter. When a search query is made that matches an SKU (or any other custom field, but SKUs are the most likely scenario here), only results that match the SKU will be returned.

For this to work, Relevanssi must be set to index the _sku custom field (because otherwise, SKU search will not find anything), and the search query must match the SKU exactly.

Add this code to your site:

add_filter( 'relevanssi_hits_filter', 'rlv_sku_exact_match' );
function rlv_sku_exact_match( $hits ) {
    global $wpdb;
    $post_ids = $wpdb->get_col(
		$wpdb->prepare(
			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sku' AND meta_value = %s",
			$hits[1]
		)
	);
    if ( ! $post_ids ) {
		// No matches found, don't touch the results.
        return $hits;
    }
	// Return only results with ID numbers that are in $post_ids.
	$hits[0] = array_filter(
		$hits[0],
		function( $hit ) use ( $post_ids ) {
            $hit_object = relevanssi_get_an_object( $hit )['object'];
			return in_array( $hit_object->ID, $post_ids, false );
		}
	);
	return $hits;
}

Any searches that match an SKU in the database should only return the product matching the SKU.

If you index product variation SKUs for the main product, the function above won’t work. When searching for product variation SKUs, it will insist that Relevanssi returns the variation product, but that’s not possible. Thus, Relevanssi returns no results. This version will return the main product:

add_filter( 'relevanssi_hits_filter', 'rlv_sku_exact_match' );
function rlv_sku_exact_match( $hits ) {
    global $wpdb;
    $post_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sku' AND meta_value = %s",
            $hits[1]
        )
    );
    if ( ! $post_ids ) {
        // No matches found, don't touch the results.
        return $hits;
    }
    $post_ids = array_map(
        function( $post_id ) {
            if ( 'product_variation' === get_post_type( $post_id ) ) {
                return get_post_parent( $post_id )->ID;
            } else {
				return $post_id;
			}
        },
        $post_ids
    );
    // Return only results with ID numbers that are in $post_ids.
    $hits[0] = array_filter(
        $hits[0],
        function( $hit ) use ( $post_ids ) {
            $hit_object = relevanssi_get_an_object( $hit )['object'];
			return in_array( $hit_object->ID, $post_ids, false );
        }
    );
    return $hits;
}

One comment WooCommerce: Return only exact matches for SKU searches

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *