Automattic\WooCommerce\Internal\ProductFilters

FilterData::get_attribute_counts │ public │ WC 1.0

Get attribute counts for the current products.

Method of the class: FilterData{}

Returns

array. termId=>count pairs.

Usage

$FilterData = new FilterData();
$FilterData->get_attribute_counts( $query_vars, $attribute_to_count );
$query_vars(array) (required)
The WP_Query arguments.
$attribute_to_count(string) (required)
Attribute taxonomy name.

FilterData::get_attribute_counts() code WC 11.3.0

public function get_attribute_counts( array $query_vars, string $attribute_to_count ) {
	$query_vars = $this->get_query_vars_with_default_post_status( $query_vars );

	/**
	 * Filter the data. @see get_filtered_price() for full documentation.
	 */
	$pre_filter_counts = apply_filters( 'woocommerce_pre_product_filter_data', null, 'attribute', $query_vars, array( 'taxonomy' => $attribute_to_count ) ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment

	if ( is_array( $pre_filter_counts ) ) {
		return $pre_filter_counts;
	}

	$use_lookup_table  = 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' );
	$hide_out_of_stock = 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' );
	$transient_key     = $this->get_transient_key(
		$query_vars,
		'attribute',
		array(
			'taxonomy'          => $attribute_to_count,
			'use_lookup_table'  => $use_lookup_table,
			'hide_out_of_stock' => $hide_out_of_stock,
		)
	);
	$cached_data       = $this->get_cache( $transient_key );

	if ( ! empty( $cached_data ) ) {
		return $cached_data;
	}

	$results     = array();
	$product_ids = $this->get_cached_product_ids( $query_vars );

	if ( $product_ids ) {
		global $wpdb;

		$taxonomy_escaped = esc_sql( wc_sanitize_taxonomy_name( $attribute_to_count ) );
		if ( $use_lookup_table ) {
			$lookup_table        = wc_get_container()->get( LookupDataStore::class )->get_lookup_table_name();
			$in_stock_clause     = $hide_out_of_stock ? ' AND in_stock = 1' : '';
			$attribute_count_sql = "
				SELECT COUNT( DISTINCT product_or_parent_id ) as term_count, term_id as term_count_id
				FROM {$lookup_table}
				WHERE product_or_parent_id IN ( {$product_ids} )
				AND taxonomy = '{$taxonomy_escaped}'
				{$in_stock_clause}
				GROUP BY term_id
			";
		} else {
			$attribute_count_sql = "
				SELECT COUNT( DISTINCT term_relationships.object_id ) as term_count, terms.term_id as term_count_id
				FROM {$wpdb->term_relationships} AS term_relationships
				INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )
				INNER JOIN {$wpdb->terms} AS terms USING( term_id )
				WHERE term_relationships.object_id IN ( {$product_ids} )
				AND term_taxonomy.taxonomy = '{$taxonomy_escaped}'
				GROUP BY terms.term_id
			";
		}

		/**
		 * We can't use $wpdb->prepare() here because using %s with
		 * $wpdb->prepare() for a subquery won't work as it will escape the
		 * SQL query.
		 * We're using the query as is, same as Core does.
		 */
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$results = $wpdb->get_results( $attribute_count_sql );
		$results = array_map( 'absint', wp_list_pluck( $results, 'term_count', 'term_count_id' ) );
	}

	/**
	 * Filter the results. @see get_filtered_price() for full documentation.
	 *
	 * @since 9.9.0
	 */
	$results = apply_filters( 'woocommerce_product_filter_data', $results, 'attribute', $query_vars, array( 'taxonomy' => $attribute_to_count ) );

	$this->set_cache( $transient_key, $results );

	return $results;
}