diff --git a/includes/admin/settings/class-wc-settings-products.php b/includes/admin/settings/class-wc-settings-products.php index d5da136cb93..e696651b604 100644 --- a/includes/admin/settings/class-wc-settings-products.php +++ b/includes/admin/settings/class-wc-settings-products.php @@ -435,9 +435,11 @@ class WC_Settings_Products extends WC_Settings_Page { public function save() { $this->save_settings_for_current_section(); - // Any time we update the product settings, we should flush the term count cache. - $tools_controller = WC()->get_instance_of( WC_REST_System_Status_Tools_Controller::class ); - $tools_controller->execute_tool( 'recount_terms' ); + /* + * Product->Inventory has a setting `Out of stock visibility`. + * Because of this, we need to recount the terms to keep them in-sync. + */ + WC()->call_function( 'wc_recount_all_terms' ); $this->do_update_options_action(); } diff --git a/tests/php/includes/settings/class-wc-settings-products-test.php b/tests/php/includes/settings/class-wc-settings-products-test.php index eaf88245279..1388b532418 100644 --- a/tests/php/includes/settings/class-wc-settings-products-test.php +++ b/tests/php/includes/settings/class-wc-settings-products-test.php @@ -148,25 +148,19 @@ class WC_Settings_Products_Test extends WC_Settings_Unit_Test_Case { * @testdox 'save' flushes the term count cache. */ public function test_save_does_recount_terms() { - // phpcs:disable Squiz.Commenting - $mock_tools_controller = new class() { - public $tool_executed; + $wc_recount_all_terms_called = false; - public function execute_tool( $tool ) { - $this->tool_executed = $tool; - } - }; - // phpcs:enable Squiz.Commenting - - $this->register_legacy_proxy_class_mocks( + $this->register_legacy_proxy_function_mocks( array( - 'WC_REST_System_Status_Tools_Controller' => $mock_tools_controller, + 'wc_recount_all_terms' => function() use ( &$wc_recount_all_terms_called ) { + $wc_recount_all_terms_called = true; + }, ) ); $sut = new WC_Settings_Products(); $sut->save(); - $this->assertEquals( 'recount_terms', $mock_tools_controller->tool_executed ); + $this->assertTrue( $wc_recount_all_terms_called ); } }