wc_change_term_counts()WC 1.0

Overrides the original term count for product categories and tags with the product count. that takes catalog visibility into account.

Хуки из функции

Возвращает

Массив.

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

wc_change_term_counts( $terms, $taxonomies );
$terms(массив) (обязательный)
List of terms.
$taxonomies(строка|массив) (обязательный)
Single taxonomy or list of taxonomies.

Код wc_change_term_counts() WC 10.4.0

function wc_change_term_counts( $terms, $taxonomies ) {
	if ( is_admin() || wp_doing_ajax() ) {
		return $terms;
	}

	/**
	 * Filter which product taxonomies should have their term counts overridden to take catalog visibility into account.
	 *
	 * @since 2.1.0
	 *
	 * @param array $valid_taxonomies List of taxonomy slugs.
	 */
	$valid_taxonomies   = apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag', 'product_brand' ) );
	$current_taxonomies = array_intersect( (array) $taxonomies, $valid_taxonomies );

	if ( empty( $current_taxonomies ) ) {
		return $terms;
	}

	$o_term_counts = get_transient( 'wc_term_counts' );
	$term_counts   = false === $o_term_counts ? array() : $o_term_counts;

	foreach ( $terms as &$term ) {
		if ( $term instanceof WP_Term && in_array( $term->taxonomy, $current_taxonomies, true ) ) {
			$key = $term->term_id . '_' . $term->taxonomy;
			if ( ! isset( $term_counts[ $key ] ) ) {
				$count               = get_term_meta( $term->term_id, 'product_count_' . $term->taxonomy, true );
				$count               = '' !== $count ? absint( $count ) : 0;
				$term_counts[ $key ] = $count;
			}

			$term->count = $term_counts[ $key ];
		}
	}

	// Update transient.
	if ( $term_counts !== $o_term_counts ) {
		set_transient( 'wc_term_counts', $term_counts, MONTH_IN_SECONDS );
	}

	return $terms;
}