Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::get_attribute_counts()privateWC 1.0

Retrieve the attribute count for current block.

Method of the class: ProductFilterAttribute{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->get_attribute_counts( $block, $slug, $query_type );
$block(WP_Block) (required)
Block instance.
$slug(string) (required)
Attribute slug.
$query_type(string) (required)
Query type, accept 'and' or 'or'.

ProductFilterAttribute::get_attribute_counts() code WC 8.9.0

private function get_attribute_counts( $block, $slug, $query_type ) {
	$filters    = Package::container()->get( QueryFilters::class );
	$query_vars = ProductCollectionUtils::get_query_vars( $block, 1 );

	if ( 'and' !== strtolower( $query_type ) ) {
		unset( $query_vars[ 'filter_' . str_replace( 'pa_', '', $slug ) ] );
	}

	unset(
		$query_vars['taxonomy'],
		$query_vars['term']
	);

	if ( ! empty( $query_vars['tax_query'] ) ) {
		// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
		$query_vars['tax_query'] = ProductCollectionUtils::remove_query_array( $query_vars['tax_query'], 'taxonomy', $slug );
	}

	$counts           = $filters->get_attribute_counts( $query_vars, $slug );
	$attribute_counts = array();

	foreach ( $counts as $key => $value ) {
		$attribute_counts[] = array(
			'term'  => $key,
			'count' => $value,
		);
	}

	$attribute_counts = array_reduce(
		$attribute_counts,
		function( $acc, $count ) {
			$acc[ $count['term'] ] = $count['count'];
			return $acc;
		},
		[]
	);

	return $attribute_counts;
}