By default, ElasticPress does not index WooCommerce product variation skus. This means that if a customer searches for products by the specific sku that product will not show up in the search results. The following code will allow you to index product skus for simple products as well as variable products.

This snippet was originally posted here.

function ep_custom_index_and_search_prod_variations( $post_types ) {
	$post_types['product_variation'] = 'product_variation';
	
	return $post_types;
}
add_filter( 'ep_indexable_post_types', 'ep_custom_index_and_search_prod_variations' );
add_filter( 'ep_searchable_post_types', 'ep_custom_index_and_search_prod_variations' );

function ep_custom_add_sku_field_weighting_prod_variations( $fields, $post_type ) {
	if ( 'product_variation' === $post_type ) {
		$key = 'meta._sku.value';

		$fields['attributes']['children'][ $key ] = array(
			'key'   => $key,
			'label' => __( 'SKU', 'textdomain' ),
		);
	}

	return $fields;
}
add_filter(
	'ep_weighting_fields_for_post_type',
	'ep_custom_add_sku_field_weighting_prod_variations',
	10,
	2
);

After adding the above snippet to your functions.php file you will need to go back into your ElasticPress plugin settings and go to the “Manage Search Fields & Weighting” area. Scroll down to the variations settings and mark “sku” as searchable.

Load WooCommerce Stores in 249ms!

Leave a Reply

Get in Touch

Please fill out the form below and we will get back to you ASAP

  • This field is for validation purposes and should be left unchanged.