wc_recount_after_stock_change()
Recount terms after the stock amount changes.
No Hooks.
Returns
null. Nothing (null).
Usage
wc_recount_after_stock_change( $product_id );
- $product_id(int) (required)
- Product ID.
wc_recount_after_stock_change() wc recount after stock change code WC 10.6.2
function wc_recount_after_stock_change( $product_id ) {
if ( 'yes' !== get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
return;
}
if ( wp_defer_term_counting() ) {
// When deferring term counts, we're using the built in handling of `wp_update_term_count()` to deal with the deferring
// and, though, this will cause both the standard and stock based counts to be rerun, it is still more efficient
// in cases where deferred term counting was warranted.
$product_terms = get_the_terms( $product_id, 'product_cat' );
if ( is_array( $product_terms ) ) {
wp_update_term_count( array_column( $product_terms, 'term_taxonomy_id' ), 'product_cat' );
}
$product_terms = get_the_terms( $product_id, 'product_tag' );
if ( is_array( $product_terms ) ) {
wp_update_term_count( array_column( $product_terms, 'term_taxonomy_id' ), 'product_tag' );
}
} else {
_wc_recount_terms_by_product( $product_id );
}
}