wc_change_term_counts

Overrides the original term count for product categories and tags with the product count.

Description

(array) wc_change_term_counts( (array) $terms, (string|array) $taxonomies ); 

that takes catalog visibility into account.

Returns (array)

Parameters (2)

0. $terms (array)
The terms.
1. $taxonomies (string|array)
The taxonomies.

Usage

  1. if ( !function_exists( 'wc_change_term_counts' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The terms. 
  5. $terms = array(); 
  6.  
  7. // The taxonomies. 
  8. $taxonomies = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_change_term_counts($terms, $taxonomies); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-term-functions.php  
  1. function wc_change_term_counts( $terms, $taxonomies ) { 
  2. if ( is_admin() || is_ajax() ) { 
  3. return $terms; 
  4.  
  5. if ( ! isset( $taxonomies[0] ) || ! in_array( $taxonomies[0], apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag' ) ) ) ) { 
  6. return $terms; 
  7.  
  8. $term_counts = $o_term_counts = get_transient( 'wc_term_counts' ); 
  9.  
  10. foreach ( $terms as &$term ) { 
  11. if ( is_object( $term ) ) { 
  12. $term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_woocommerce_term_meta( $term->term_id, 'product_count_' . $taxonomies[0] , true ); 
  13.  
  14. if ( '' !== $term_counts[ $term->term_id ] ) { 
  15. $term->count = absint( $term_counts[ $term->term_id ] ); 
  16.  
  17. // Update transient 
  18. if ( $term_counts != $o_term_counts ) { 
  19. set_transient( 'wc_term_counts', $term_counts, DAY_IN_SECONDS * 30 ); 
  20.  
  21. return $terms;