Home Forums WoodMart support forum On search result page brand filter is not showing all brands Reply To: On search result page brand filter is not showing all brands

#571284

Hung Pham
Keymaster

Hi Vok,

Please try to add the code below in the functions.php file in your child theme, let me know if it works correctly.

add_filter( 'woocommerce_get_filtered_term_product_counts_query', function ( $query ) {
	if ( empty( $_GET['s'] ) || ! apply_filters( 'woodmart_search_by_sku', woodmart_get_opt( 'search_by_sku' ) ) ) {
		return $query;
	}

	global $wpdb;

	$term = woodmart_clean( $_GET['s'] );

	$sku_to_parent_id = $wpdb->get_col( $wpdb->prepare( "SELECT p.post_parent as post_id FROM {$wpdb->posts} as p join {$wpdb->wc_product_meta_lookup} ml on p.ID = ml.product_id and ml.sku LIKE '%%%s%%' where p.post_parent <> 0 group by p.post_parent", $term ) );

	$sku_to_id = $wpdb->get_results( "SELECT product_id FROM {$wpdb->wc_product_meta_lookup} WHERE sku LIKE '%{$term}%';", ARRAY_N );

	$sku_to_id_results = array();
	if ( is_array( $sku_to_id ) ) {
		foreach ( $sku_to_id as $id ) {
			$sku_to_id_results[] = $id[0];
		}
	}

	$search_ids = array_merge( $sku_to_id_results, $sku_to_parent_id );

	if ( $search_ids ) {
		$sql = $wpdb->prepare( "{$wpdb->posts}.ID IN (%s)", implode( ',', $search_ids ) );

		$query['where'] = str_replace( "))", ") OR ({$sql}))", $query['where'] );
	}

	return $query;
});

Regards,