Automattic\WooCommerce\Internal\ProductAttributesLookup
Filterer::get_filtered_term_product_counts
Count products within certain terms, taking the main WP query into consideration, for the WC_Widget_Layered_Nav widget.
This query allows counts to be generated based on the viewed products, not all products.
Метод класса: Filterer{}
Хуки из метода
Возвращает
array.
Использование
$Filterer = new Filterer(); $Filterer->get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type );
- $term_ids(массив) (обязательный)
- Term IDs.
- $taxonomy(строка) (обязательный)
- Taxonomy.
- $query_type(строка) (обязательный)
- Query Type.
Код Filterer::get_filtered_term_product_counts() Filterer::get filtered term product counts WC 11.3.0
public function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ) {
global $wpdb;
$tax_query = \WC_Query::get_main_tax_query();
$meta_query = \WC_Query::get_main_meta_query();
if ( 'or' === $query_type ) {
foreach ( $tax_query as $key => $query ) {
if ( is_array( $query ) && $taxonomy === $query['taxonomy'] ) {
unset( $tax_query[ $key ] );
}
}
}
$meta_query = new \WP_Meta_Query( $meta_query );
$tax_query = new TaxQuery( $tax_query );
if ( $this->filtering_via_lookup_table_is_active() ) {
$query = $this->get_product_counts_query_using_lookup_table( $tax_query, $meta_query, $taxonomy, $term_ids );
} else {
$query = $this->get_product_counts_query_not_using_lookup_table( $tax_query, $meta_query, $term_ids );
}
/**
* Customizes the SQL query that populates product counts in layered navigation.
*
* @since 10.9.0 the query was optimized to remove the join on term_relationships in favor of a nested select from term_relationships.
* @since 2.6.1
*
* @param array $query Query fragments (the available fragments are: select, from, join, where, group_by).
*/
$query = apply_filters( 'woocommerce_get_filtered_term_product_counts_query', $query );
$query_sql = implode( ' ', $query );
// We have a query - let's see if cached results of this query already exist.
$query_hash = md5( $query_sql );
// Maybe store a transient of the count values.
$cache = apply_filters( 'woocommerce_layered_nav_count_maybe_cache', true );
if ( true === $cache ) {
$cached_counts = (array) get_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ) );
} else {
$cached_counts = array();
}
if ( ! isset( $cached_counts[ $query_hash ] ) ) {
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$results = $wpdb->get_results( $query_sql, ARRAY_A );
$counts = array_map( 'absint', wp_list_pluck( $results, 'term_count', 'term_count_id' ) );
$cached_counts[ $query_hash ] = $counts;
if ( true === $cache ) {
$cached_counts = self::limit_layered_nav_count_cache_entries( $cached_counts, $query_hash );
set_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ), $cached_counts, DAY_IN_SECONDS );
}
}
return array_map( 'absint', (array) $cached_counts[ $query_hash ] );
}