Extract recount terms logic into own function closes #25375

This commit is contained in:
roykho 2021-03-04 09:06:03 -08:00
parent 20c8cbee0c
commit cb0b92750b
No known key found for this signature in database
GPG Key ID: 7B36C0EA25795714
3 changed files with 32 additions and 16 deletions

View File

@ -64,6 +64,12 @@ class WC_Settings_Products extends WC_Settings_Page {
$settings = $this->get_settings( $current_section );
WC_Admin_Settings::save_fields( $settings );
/*
* Product->Inventory has a setting `Out of stock visibility`.
* Because of this, we need to recount the terms to keep them in-sync.
*/
wc_recount_all_terms();
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
}

View File

@ -504,22 +504,7 @@ class WC_REST_System_Status_Tools_V2_Controller extends WC_REST_Controller {
break;
case 'recount_terms':
$product_cats = get_terms(
'product_cat',
array(
'hide_empty' => false,
'fields' => 'id=>parent',
)
);
_wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false );
$product_tags = get_terms(
'product_tag',
array(
'hide_empty' => false,
'fields' => 'id=>parent',
)
);
_wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false );
wc_recount_all_terms();
$message = __( 'Terms successfully recounted', 'woocommerce' );
break;

View File

@ -636,3 +636,28 @@ function wc_get_product_visibility_term_ids() {
)
);
}
/**
* Recounts all terms.
*
* @since 5.2
* @return void
*/
function wc_recount_all_terms() {
$product_cats = get_terms(
'product_cat',
array(
'hide_empty' => false,
'fields' => 'id=>parent',
)
);
_wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), true, false );
$product_tags = get_terms(
'product_tag',
array(
'hide_empty' => false,
'fields' => 'id=>parent',
)
);
_wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), true, false );
}