How to exclude certain attributes from the search scope?

The solution works only for the Pro plugin version.

Search in attributes feature allows users to search in all attributes. In some cases, you should exclude attributes that make no sense for the search process.

How to implement the code?

To do it, you have to paste the following code into functions.php in your child-theme or use Code Snippets plugin.

Where can you find the attribute names?

In WooCommerce an attribute is custom taxonomy. To exclude attributes we use taxonomy name. WooCommerce adds prefix “pa_” for each attribute slug to create custom taxonomies. If an attribute has slag “height”, the taxonomy will be “pa_height”. If you don’t know how to check taxonomy for an attribute, take a look at the following screen.

Go to WooCommerce -> Attributes -> Edit -> check the URL

Custom snippet

/**
 * Attributes to exclude
 *
 * @return array
 */
function wcas_get_attributes_to_exclude() {
	return array(
		'pa_calipers', //Replace with yours!!!
		'pa_case-size' //Replace with yours!!!
	);
}

/**
 * Exclude attribute every time when the product is updated
 *
 * @return array
 */
add_filter( 'dgwt/wcas/product/attributes', function ( $attributes ) {

	foreach ( wcas_get_attributes_to_exclude() as $taxonomy ) {
		if ( array_key_exists( $taxonomy, $attributes ) ) {
			unset( $attributes[ $taxonomy ] );
		}
	}

	return $attributes;
} );

/**
 * Exclude attribute every time when the whole index process is running
 *
 * @return array
 */
add_filter( 'dgwt/wcas/attribute_taxonomies', function ( $taxonomies ) {

	foreach ( wcas_get_attributes_to_exclude() as $taxonomy ) {
		if ( ( $key = array_search( $taxonomy, $taxonomies ) ) !== false ) {
			unset( $taxonomies[ $key ] );
		}
	}

	return $taxonomies;
} );

After implementing the code, remember to rebuild the search index. To do it got to WooCommerce -> AJAX Search bar -> Indexer (tab) and click “Rebuild index” button.