WC_Widget_Brand_Nav::get_filtered_term_product_counts()protectedWC 1.0

Count products within certain terms, taking the main WP query into consideration.

Метод класса: WC_Widget_Brand_Nav{}

Возвращает

Массив.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type );
$term_ids(массив) (обязательный)
Term IDs.
$taxonomy(строка) (обязательный)
Taxonomy.
$query_type(строка)
Query type.
По умолчанию: 'and'

Код WC_Widget_Brand_Nav::get_filtered_term_product_counts() WC 9.5.1

protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type = 'and' ) {
	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 WP_Tax_Query( $tax_query );
	$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
	$tax_query_sql  = $tax_query->get_sql( $wpdb->posts, 'ID' );

	// Generate query.
	$query             = array();
	$query['select']   = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) as term_count, terms.term_id as term_count_id";
	$query['from']     = "FROM {$wpdb->posts}";
	$query['join']     = "
		INNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id
		INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )
		INNER JOIN {$wpdb->terms} AS terms USING( term_id )
		" . $tax_query_sql['join'] . $meta_query_sql['join'];
	$query['where']    = "
		WHERE {$wpdb->posts}.post_type IN ( 'product' )
		AND {$wpdb->posts}.post_status = 'publish'
		" . $tax_query_sql['where'] . $meta_query_sql['where'] . '
		AND terms.term_id IN (' . implode( ',', array_map( 'absint', $term_ids ) ) . ')
	';
	$query['group_by'] = 'GROUP BY terms.term_id';
	$query             = apply_filters( 'woocommerce_get_filtered_term_product_counts_query', $query ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
	$query             = implode( ' ', $query );

	// We have a query - let's see if cached results of this query already exist.
	$query_hash = md5( $query );

	$cache = apply_filters( 'woocommerce_layered_nav_count_maybe_cache', true ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
	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 ] ) ) {
		$results                      = $wpdb->get_results( $query, ARRAY_A ); // @codingStandardsIgnoreLine
		$counts                       = array_map( 'absint', wp_list_pluck( $results, 'term_count', 'term_count_id' ) );
		$cached_counts[ $query_hash ] = $counts;
		if ( true === $cache ) {
			set_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ), $cached_counts, HOUR_IN_SECONDS );
		}
	}

	return array_map( 'absint', (array) $cached_counts[ $query_hash ] );
}