_wc_recount_terms_by_product()
Recounts product category, tag, and brand terms by product.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
void. Nothing (null).
Usage
_wc_recount_terms_by_product( $product_id );
- $product_id(int)
- The ID of the product.
Default:''
Changelog
| Since 5.2 | Introduced. |
_wc_recount_terms_by_product() wc recount terms by product code WC 11.2.1
function _wc_recount_terms_by_product( $product_id = '' ) {
if ( empty( $product_id ) ) {
return;
}
$product_terms = get_the_terms( $product_id, 'product_cat' );
if ( $product_terms ) {
$product_cats = array();
foreach ( $product_terms as $term ) {
$product_cats[ $term->term_id ] = $term->parent;
}
_wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false );
}
$product_terms = get_the_terms( $product_id, 'product_tag' );
if ( $product_terms ) {
$product_tags = array();
foreach ( $product_terms as $term ) {
$product_tags[ $term->term_id ] = $term->parent;
}
_wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false );
}
$product_terms = get_the_terms( $product_id, 'product_brand' );
if ( is_array( $product_terms ) ) {
$product_brands = array();
foreach ( $product_terms as $term ) {
$product_brands[ $term->term_id ] = $term->parent;
}
$product_brand_taxonomy = get_taxonomy( 'product_brand' );
if ( $product_brand_taxonomy instanceof WP_Taxonomy ) {
_wc_term_recount( $product_brands, $product_brand_taxonomy, false, false );
}
}
}