Commit 95c0a07fc32 for woocommerce

commit 95c0a07fc32093eebce9ba583d8e2e08ffcd31f4
Author: Lazizbek Ergashev <lazerg2@gmail.com>
Date:   Mon Nov 30 10:15:21 2026 +0500

    Unschedule the recurring Analytics order import when Analytics is disabled (#71530)

    * Unschedule the recurring Analytics order import when Analytics is disabled

    * Unschedule the order import as soon as Analytics is turned off

    * Also unschedule the order import when Analytics is first saved as off

    * Keep the daily cleanup test independent of the Analytics toggle listeners

    * Schedule the order import again when Analytics is turned back on

    * Move the Analytics toggle listener tests to OrdersSchedulerTest

    * Don't schedule the order import when a legacy filter disables Analytics

diff --git a/plugins/woocommerce/changelog/71503-unschedule-order-import-when-analytics-disabled b/plugins/woocommerce/changelog/71503-unschedule-order-import-when-analytics-disabled
new file mode 100644
index 00000000000..c9df96b35e5
--- /dev/null
+++ b/plugins/woocommerce/changelog/71503-unschedule-order-import-when-analytics-disabled
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Unschedule the Analytics order import when Analytics is turned off, so it no longer logs failed actions.
diff --git a/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php b/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php
index 68242695354..32d2e451f13 100644
--- a/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php
+++ b/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php
@@ -26,6 +26,7 @@ use Automattic\WooCommerce\Admin\ReportsSync;
 use Automattic\WooCommerce\Internal\Admin\CategoryLookup;
 use Automattic\WooCommerce\Internal\Admin\Events;
 use Automattic\WooCommerce\Internal\Admin\Onboarding\Onboarding;
+use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
 use Automattic\WooCommerce\Internal\Admin\Screens\AdminScreens;

 /**
@@ -188,6 +189,10 @@ class FeaturePlugin {
 		} else {
 			// Already generated exports still need serving and expiring.
 			ReportExporter::init_export_files();
+			// Nothing handles the order import batches now, so stop the recurring one.
+			add_action( 'action_scheduler_ensure_recurring_actions', array( OrdersScheduler::class, 'unschedule_recurring_batch_processor' ) );
+			// Queue it again right away when Analytics is turned back on, instead of waiting for the daily check.
+			add_action( 'update_option_' . Analytics::TOGGLE_OPTION_NAME, array( OrdersScheduler::class, 'handle_analytics_option_change' ), 10, 2 );
 		}

 		// Admin note providers.
diff --git a/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php b/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php
index 697cbf8a985..57466396762 100644
--- a/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php
+++ b/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php
@@ -14,7 +14,9 @@ use Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore as OrderDataStore;
 use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
 use Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
 use Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore as TaxesDataStore;
+use Automattic\WooCommerce\Admin\Features\Features;
 use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Internal\Admin\Analytics;
 use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
 use Automattic\WooCommerce\Internal\Utilities\ActionSchedulerUtil;
 use Automattic\WooCommerce\Utilities\OrderUtil;
@@ -147,6 +149,10 @@ class OrdersScheduler extends ImportScheduler {
 		add_action( 'update_option_' . self::SCHEDULED_IMPORT_OPTION, array( __CLASS__, 'handle_scheduled_import_option_change' ), 10, 2 );
 		add_action( 'delete_option', array( __CLASS__, 'handle_scheduled_import_option_before_delete' ), 10, 1 );

+		// Stop the recurring import as soon as Analytics is turned off, nothing handles it after that.
+		add_action( 'update_option_' . Analytics::TOGGLE_OPTION_NAME, array( __CLASS__, 'handle_analytics_option_change' ), 10, 2 );
+		add_action( 'add_option_' . Analytics::TOGGLE_OPTION_NAME, array( __CLASS__, 'handle_analytics_option_added' ), 10, 2 );
+
 		OrdersStatsDataStore::init();
 		CouponsDataStore::init();
 		ProductsDataStore::init();
@@ -569,6 +575,49 @@ AND status NOT IN ( 'wc-auto-draft', 'trash', 'auto-draft' )
 		ActionSchedulerUtil::schedule_recurring( time(), self::get_import_interval(), $action_hook, array(), static::$group ?? '', true );
 	}

+	/**
+	 * Unschedule the recurring batch processor for order imports.
+	 *
+	 * @internal
+	 */
+	public static function unschedule_recurring_batch_processor(): void {
+		$action_hook = self::get_action( self::PROCESS_PENDING_ORDERS_BATCH_ACTION );
+		if ( null !== $action_hook ) {
+			as_unschedule_all_actions( $action_hook, array(), static::$group ?? '' );
+		}
+	}
+
+	/**
+	 * Unschedule the recurring batch processor when Analytics is disabled, and schedule it again when it is enabled.
+	 *
+	 * @internal
+	 * @param mixed $old_value The old value of the option.
+	 * @param mixed $new_value The new value of the option.
+	 * @return void
+	 */
+	public static function handle_analytics_option_change( $old_value, $new_value ): void {
+		if ( 'no' === $new_value ) {
+			self::unschedule_recurring_batch_processor();
+		} elseif ( 'yes' === $new_value && ! Features::is_analytics_disabled_by_legacy_filters() && self::is_scheduled_import_enabled() ) {
+			self::schedule_recurring_batch_processor();
+		}
+	}
+
+	/**
+	 * Unschedule the recurring batch processor when Analytics is first saved as disabled.
+	 *
+	 * A missing option means Analytics is on, so saving 'no' adds the option and skips the update hook.
+	 *
+	 * @internal
+	 * @param string $option The option name.
+	 * @param mixed  $value  The option value.
+	 * @return void
+	 */
+	public static function handle_analytics_option_added( $option, $value ): void {
+		unset( $option );
+		self::handle_analytics_option_change( null, $value );
+	}
+
 	/**
 	 * Handle changes to the scheduled import option.
 	 *
@@ -586,11 +635,7 @@ AND status NOT IN ( 'wc-auto-draft', 'trash', 'auto-draft' )
 	public static function handle_scheduled_import_option_change( $old_value, $new_value ) {
 		// If switching from scheduled to immediate import.
 		if ( 'yes' === $old_value && 'no' === $new_value ) {
-			// Unschedule the recurring batch processor.
-			$action_hook = self::get_action( self::PROCESS_PENDING_ORDERS_BATCH_ACTION );
-			if ( null !== $action_hook ) {
-				as_unschedule_all_actions( $action_hook, array(), static::$group ?? '' );
-			}
+			self::unschedule_recurring_batch_processor();

 			// Schedule an immediate catchup batch to process all orders up to now.
 			// This ensures no orders are missed during the transition.
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php
index 7996a249d2e..2aaf4ee8a2d 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/FeaturePluginTest.php
@@ -6,6 +6,7 @@ namespace Automattic\WooCommerce\Tests\Internal\Admin;
 use Automattic\WooCommerce\Admin\ReportExporter;
 use Automattic\WooCommerce\Internal\Admin\Analytics;
 use Automattic\WooCommerce\Internal\Admin\FeaturePlugin;
+use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
 use Automattic\WooCommerce\Internal\Features\FeaturesController;
 use WC_Unit_Test_Case;

@@ -168,6 +169,67 @@ class FeaturePluginTest extends WC_Unit_Test_Case {
 		$this->assertFalse( has_action( $scheduler_hook, $scheduler_callback ), 'Export scheduling should stay disabled.' );
 	}

+	/**
+	 * @testdox The recurring order import is unscheduled when Analytics is disabled.
+	 */
+	public function test_recurring_order_import_is_unscheduled_when_analytics_is_disabled(): void {
+		$batch_hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
+
+		$this->assertIsString( $batch_hook, 'The batch import hook should be defined.' );
+
+		remove_action( 'action_scheduler_ensure_recurring_actions', array( OrdersScheduler::class, 'schedule_recurring_batch_processor' ) );
+		OrdersScheduler::schedule_recurring_batch_processor();
+		$this->assertTrue( as_has_scheduled_action( $batch_hook, array(), OrdersScheduler::$group ), 'The batch import should be scheduled before Analytics is disabled.' );
+
+		// Simulate a site where Analytics was turned off earlier, so only the daily check can clean up.
+		remove_action( 'update_option_' . Analytics::TOGGLE_OPTION_NAME, array( OrdersScheduler::class, 'handle_analytics_option_change' ) );
+		remove_action( 'add_option_' . Analytics::TOGGLE_OPTION_NAME, array( OrdersScheduler::class, 'handle_analytics_option_added' ) );
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'no' );
+
+		FeaturePlugin::instance()->includes();
+		$this->assertTrue( as_has_scheduled_action( $batch_hook, array(), OrdersScheduler::$group ), 'The batch import should stay scheduled until the daily check runs.' );
+		do_action( 'action_scheduler_ensure_recurring_actions' );
+
+		$this->assertFalse( as_has_scheduled_action( $batch_hook, array(), OrdersScheduler::$group ), 'The batch import has no callback without Analytics, so it should not stay scheduled.' );
+	}
+
+	/**
+	 * @testdox The recurring order import is scheduled right away when Analytics is turned back on.
+	 */
+	public function test_recurring_order_import_is_scheduled_when_analytics_is_turned_back_on(): void {
+		$batch_hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
+
+		$this->assertIsString( $batch_hook, 'The batch import hook should be defined.' );
+
+		update_option( OrdersScheduler::SCHEDULED_IMPORT_OPTION, 'yes' );
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'no' );
+		remove_action( 'update_option_' . Analytics::TOGGLE_OPTION_NAME, array( OrdersScheduler::class, 'handle_analytics_option_change' ) );
+		FeaturePlugin::instance()->includes();
+		as_unschedule_all_actions( $batch_hook, array(), OrdersScheduler::$group );
+
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'yes' );
+
+		$this->assertTrue( as_has_scheduled_action( $batch_hook, array(), OrdersScheduler::$group ), 'Turning Analytics back on should not wait for the daily check.' );
+	}
+
+	/**
+	 * @testdox Turning Analytics back on does not queue the order import while a legacy filter keeps it disabled.
+	 */
+	public function test_recurring_order_import_is_not_scheduled_when_analytics_is_turned_back_on_but_disabled_by_legacy_filter(): void {
+		$batch_hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
+
+		$this->assertIsString( $batch_hook, 'The batch import hook should be defined.' );
+
+		update_option( OrdersScheduler::SCHEDULED_IMPORT_OPTION, 'yes' );
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'no' );
+		add_filter( 'woocommerce_admin_disabled', '__return_true', PHP_INT_MAX );
+		as_unschedule_all_actions( $batch_hook, array(), OrdersScheduler::$group );
+
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'yes' );
+
+		$this->assertFalse( as_has_scheduled_action( $batch_hook, array(), OrdersScheduler::$group ), 'Nothing handles the order import while a legacy filter disables Analytics.' );
+	}
+
 	/**
 	 * Disable Analytics in the legacy feature list.
 	 *
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Schedulers/OrdersSchedulerTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Schedulers/OrdersSchedulerTest.php
index 09b2e9e4e82..d12d5c1de65 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Schedulers/OrdersSchedulerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Schedulers/OrdersSchedulerTest.php
@@ -8,6 +8,7 @@ use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersSta
 use Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore as TaxesDataStore;
 use Automattic\WooCommerce\Enums\OrderInternalStatus;
 use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Internal\Admin\Analytics;
 use Automattic\WooCommerce\RestApi\UnitTests\LoggerSpyTrait;
 use Automattic\WooCommerce\Utilities\OrderUtil;
 use WC_Unit_Test_Case;
@@ -664,6 +665,39 @@ class OrdersSchedulerTest extends WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * @testdox The recurring order import is unscheduled right away when Analytics is turned off.
+	 */
+	public function test_recurring_order_import_is_unscheduled_when_analytics_is_turned_off(): void {
+		$this->clear_scheduled_batch_processor();
+
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'yes' );
+		OrdersScheduler::init();
+		OrdersScheduler::schedule_recurring_batch_processor();
+		$this->assertTrue( $this->is_batch_processor_scheduled(), 'The batch import should be scheduled while Analytics is on.' );
+
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'no' );
+
+		$this->assertFalse( $this->is_batch_processor_scheduled(), 'Turning Analytics off should not wait for the daily check.' );
+	}
+
+	/**
+	 * @testdox The recurring order import is unscheduled right away when Analytics is turned off for the first time.
+	 */
+	public function test_recurring_order_import_is_unscheduled_when_analytics_is_first_saved_as_off(): void {
+		$this->clear_scheduled_batch_processor();
+
+		// A missing option means Analytics is on, and saving it adds the option instead of updating it.
+		delete_option( Analytics::TOGGLE_OPTION_NAME );
+		OrdersScheduler::init();
+		OrdersScheduler::schedule_recurring_batch_processor();
+		$this->assertTrue( $this->is_batch_processor_scheduled(), 'The batch import should be scheduled while Analytics is on.' );
+
+		update_option( Analytics::TOGGLE_OPTION_NAME, 'no' );
+
+		$this->assertFalse( $this->is_batch_processor_scheduled(), 'Turning Analytics off the first time should not wait for the daily check.' );
+	}
+
 	/**
 	 * @testdox Should identify order with _wcpay_mode test as a test order.
 	 */