_wc_term_recount

Function for recounting product terms, ignoring hidden products.

Description

_wc_term_recount( (array) $terms, (string) $taxonomy, (constant) $callback = true, (bool) $terms_are_term_taxonomy_ids = true ); 

Parameters (4)

0. $terms (array)
The terms.
1. $taxonomy (string)
The taxonomy.
2. $callback — Optional. (constant) => true
The callback.
3. $terms_are_term_taxonomy_ids — Optional. (bool) => true
The terms are term taxonomy ids.

Usage

  1. if ( !function_exists( '_wc_term_recount' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The terms. 
  5. $terms = array(); 
  6.  
  7. // The taxonomy. 
  8. $taxonomy = ''; 
  9.  
  10. // The callback. 
  11. $callback = true; 
  12.  
  13. // The terms are term taxonomy ids. 
  14. $terms_are_term_taxonomy_ids = true; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = _wc_term_recount($terms, $taxonomy, $callback, $terms_are_term_taxonomy_ids); 
  18.  

Defined (1)

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

/includes/wc-term-functions.php  
  1. function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_taxonomy_ids = true ) { 
  2. global $wpdb; 
  3.  
  4. // Standard callback. 
  5. if ( $callback ) { 
  6. _update_post_term_count( $terms, $taxonomy ); 
  7.  
  8. $exclude_term_ids = array(); 
  9. $product_visibility_term_ids = wc_get_product_visibility_term_ids(); 
  10.  
  11. if ( $product_visibility_term_ids['exclude-from-catalog'] ) { 
  12. $exclude_term_ids[] = $product_visibility_term_ids['exclude-from-catalog']; 
  13.  
  14. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) { 
  15. $exclude_term_ids[] = $product_visibility_term_ids['outofstock']; 
  16.  
  17. $query = array( 
  18. 'fields' => " 
  19. SELECT COUNT( DISTINCT ID ) FROM {$wpdb->posts} p 
  20. ",  
  21. 'join' => '',  
  22. 'where' => " 
  23. WHERE 1=1 
  24. AND p.post_status = 'publish' 
  25. AND p.post_type = 'product' 
  26.  
  27. ",  
  28. ); 
  29.  
  30. if ( count( $exclude_term_ids ) ) { 
  31. $query['join'] .= " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ', ', array_map( 'absint', $exclude_term_ids ) ) . " ) ) AS exclude_join ON exclude_join.object_id = p.ID"; 
  32. $query['where'] .= " AND exclude_join.object_id IS NULL"; 
  33.  
  34. // Pre-process term taxonomy ids. 
  35. if ( ! $terms_are_term_taxonomy_ids ) { 
  36. // We passed in an array of TERMS in format id=>parent. 
  37. $terms = array_filter( (array) array_keys( $terms ) ); 
  38. } else { 
  39. // If we have term taxonomy IDs we need to get the term ID. 
  40. $term_taxonomy_ids = $terms; 
  41. $terms = array(); 
  42. foreach ( $term_taxonomy_ids as $term_taxonomy_id ) { 
  43. $term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy->name ); 
  44. $terms[] = $term->term_id; 
  45.  
  46. // Exit if we have no terms to count. 
  47. if ( empty( $terms ) ) { 
  48. return; 
  49.  
  50. // Ancestors need counting. 
  51. if ( is_taxonomy_hierarchical( $taxonomy->name ) ) { 
  52. foreach ( $terms as $term_id ) { 
  53. $terms = array_merge( $terms, get_ancestors( $term_id, $taxonomy->name ) ); 
  54.  
  55. // Unique terms only. 
  56. $terms = array_unique( $terms ); 
  57.  
  58. // Count the terms. 
  59. foreach ( $terms as $term_id ) { 
  60. $terms_to_count = array( absint( $term_id ) ); 
  61.  
  62. if ( is_taxonomy_hierarchical( $taxonomy->name ) ) { 
  63. // We need to get the $term's hierarchy so we can count its children too 
  64. if ( ( $children = get_term_children( $term_id, $taxonomy->name ) ) && ! is_wp_error( $children ) ) { 
  65. $terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) ); 
  66.  
  67. // Generate term query 
  68. $term_query = $query; 
  69. $term_query['join'] .= " INNER JOIN ( SELECT object_id FROM {$wpdb->term_relationships} INNER JOIN {$wpdb->term_taxonomy} using( term_taxonomy_id ) WHERE term_id IN ( " . implode( ', ', array_map( 'absint', $terms_to_count ) ) . " ) ) AS include_join ON include_join.object_id = p.ID"; 
  70.  
  71. // Get the count 
  72. $count = $wpdb->get_var( implode( ' ', $term_query ) ); 
  73.  
  74. // Update the count 
  75. update_woocommerce_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) ); 
  76.  
  77. delete_transient( 'wc_term_counts' );