Apply recount term per product with filter to product data store

This commit is contained in:
roykho 2021-03-04 13:54:09 -08:00
parent cb0b92750b
commit 71d8f760b0
No known key found for this signature in database
GPG Key ID: 7B36C0EA25795714
1 changed files with 35 additions and 0 deletions

View File

@ -710,6 +710,41 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
if ( $force || array_key_exists( 'shipping_class_id', $changes ) ) { if ( $force || array_key_exists( 'shipping_class_id', $changes ) ) {
wp_set_post_terms( $product->get_id(), array( $product->get_shipping_class_id( 'edit' ) ), 'product_shipping_class', false ); wp_set_post_terms( $product->get_id(), array( $product->get_shipping_class_id( 'edit' ) ), 'product_shipping_class', false );
} }
/**
* Filter to allow/prevent recounting of terms as it could be expensive.
* A likely scenario for this is when bulk importing products. We could
* then prevent it from recounting per product but instead recount it once
* when import is done. Of course this means the import logic has to support this.
*
* @since 5.2
* @param bool
*/
if ( apply_filters( 'woocommerce_product_update_recount_terms', '__return_true' ) ) {
$product_terms = get_the_terms( $product->get_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->get_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 );
}
}
} }
/** /**