wc_recount_after_stock_change

Recount terms after the stock amount changes.

Description

wc_recount_after_stock_change( (int) $product_id ); 

Parameters (1)

0. $product_id (int)
The product id.

Usage

  1. if ( !function_exists( 'wc_recount_after_stock_change' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The product id. 
  5. $product_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_recount_after_stock_change($product_id); 
  9.  

Defined (1)

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

/includes/wc-term-functions.php  
  1. function wc_recount_after_stock_change( $product_id ) { 
  2. if ( 'yes' !== get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  3. return; 
  4.  
  5. $product_terms = get_the_terms( $product_id, 'product_cat' ); 
  6.  
  7. if ( $product_terms ) { 
  8. $product_cats = array(); 
  9.  
  10. foreach ( $product_terms as $term ) { 
  11. $product_cats[ $term->term_id ] = $term->parent; 
  12.  
  13. _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false ); 
  14.  
  15. $product_terms = get_the_terms( $product_id, 'product_tag' ); 
  16.  
  17. if ( $product_terms ) { 
  18. $product_tags = array(); 
  19.  
  20. foreach ( $product_terms as $term ) { 
  21. $product_tags[ $term->term_id ] = $term->parent; 
  22.  
  23. _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false );