Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterAttribute::get_attribute_counts
Retrieve the attribute count for current block.
Method of the class: ProductFilterAttribute{}
No Hooks.
Returns
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() ProductFilterAttribute::get attribute counts code WC 10.8.1
private function get_attribute_counts( $block, $slug, $query_type ) {
if ( ! isset( $block->context['filterParams'] ) ) {
return array();
}
$query_vars = ProductCollectionUtils::get_query_vars( $block, 1 );
if ( 'and' !== strtolower( $query_type ) ) {
unset( $query_vars[ 'filter_' . str_replace( 'pa_', '', $slug ) ] );
}
if ( isset( $query_vars['taxonomy'] ) && false !== strpos( $query_vars['taxonomy'], 'pa_' ) ) {
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 );
}
$container = wc_get_container();
$counts = $container->get( FilterDataProvider::class )->with( $container->get( QueryClauses::class ) )->get_attribute_counts( $query_vars, $slug );
$attribute_counts = array();
foreach ( $counts as $key => $value ) {
$attribute_counts[] = array(
'term' => $key,
'count' => intval( $value ),
);
}
$attribute_counts = array_reduce(
$attribute_counts,
function ( $acc, $count ) {
$acc[ $count['term'] ] = $count['count'];
return $acc;
},
array()
);
return $attribute_counts;
}