Commit 370cf70bcf2 for woocommerce
commit 370cf70bcf27a4f2b224b6c7d84cf97a88e81c9e
Author: Indian-boult <64060864+Indian-boult@users.noreply.github.com>
Date: Mon Nov 23 22:39:54 2026 +0530
Refresh Analytics reports when Week Starts On or the excluded statuses change (#71282)
Invalidate the reports cache on add, update and delete of start_of_week and woocommerce_excluded_report_order_statuses, next to the existing woocommerce_date_type hooks. Both options are read at query time and can change outside the Analytics settings screen.
diff --git a/plugins/woocommerce/changelog/fix-71121-analytics-week-start-cache b/plugins/woocommerce/changelog/fix-71121-analytics-week-start-cache
new file mode 100644
index 00000000000..88a5b4284ca
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-71121-analytics-week-start-cache
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Refresh Analytics reports as soon as Week Starts On or the excluded order statuses change, instead of showing cached results.
diff --git a/plugins/woocommerce/src/Admin/ReportsSync.php b/plugins/woocommerce/src/Admin/ReportsSync.php
index 9b96649b0b3..14081cef1f0 100644
--- a/plugins/woocommerce/src/Admin/ReportsSync.php
+++ b/plugins/woocommerce/src/Admin/ReportsSync.php
@@ -33,6 +33,13 @@ class ReportsSync {
// The very first save of the option takes the add_option path, so hook both.
add_action( 'add_option_woocommerce_date_type', array( ReportsCache::class, 'invalidate' ) );
add_action( 'update_option_woocommerce_date_type', array( ReportsCache::class, 'invalidate' ) );
+ // Week Starts On and the excluded statuses are read at query time, and can change outside the
+ // Analytics settings screen (General settings, WP-CLI, update_option), so hook every write path.
+ foreach ( array( 'start_of_week', 'woocommerce_excluded_report_order_statuses' ) as $option ) {
+ add_action( "add_option_{$option}", array( ReportsCache::class, 'invalidate' ) );
+ add_action( "update_option_{$option}", array( ReportsCache::class, 'invalidate' ) );
+ add_action( "delete_option_{$option}", array( ReportsCache::class, 'invalidate' ) );
+ }
add_action( 'trashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
add_action( 'untrashed_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
add_action( 'delete_post', array( __CLASS__, 'maybe_clear_stock_count_cache_for_post' ) );
diff --git a/plugins/woocommerce/tests/php/src/Admin/ReportsSyncTest.php b/plugins/woocommerce/tests/php/src/Admin/ReportsSyncTest.php
index c09b5071b1d..c5c37ae7b1b 100644
--- a/plugins/woocommerce/tests/php/src/Admin/ReportsSyncTest.php
+++ b/plugins/woocommerce/tests/php/src/Admin/ReportsSyncTest.php
@@ -3,6 +3,7 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Admin;
+use Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
use Automattic\WooCommerce\Admin\ReportsSync;
use Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler;
use WC_Unit_Test_Case;
@@ -88,4 +89,31 @@ class ReportsSyncTest extends WC_Unit_Test_Case {
$this->assertSame( array( 11 ), $failed['ids'], 'Stored failed IDs must survive a windowed import' );
$this->assertSame( 3, $failed['overflow'] );
}
+
+ /**
+ * @testdox Adding, updating or deleting $option drops cached report results.
+ *
+ * @testWith ["start_of_week", "1", "0"]
+ * ["woocommerce_excluded_report_order_statuses", ["pending", "failed", "cancelled"], ["pending", "failed", "cancelled", "on-hold"]]
+ *
+ * @param string $option Option that shapes report results.
+ * @param mixed $old_value Value before the change.
+ * @param mixed $new_value Value after the change.
+ */
+ public function test_changing_report_option_invalidates_report_cache( string $option, $old_value, $new_value ): void {
+ ReportsSync::init();
+ update_option( $option, $old_value );
+
+ ReportsCache::set( 'wc_reports_sync_test', 'cached' );
+ update_option( $option, $new_value );
+ $this->assertFalse( ReportsCache::get( 'wc_reports_sync_test' ), 'Updating the option should drop cached reports.' );
+
+ ReportsCache::set( 'wc_reports_sync_test', 'cached' );
+ delete_option( $option );
+ $this->assertFalse( ReportsCache::get( 'wc_reports_sync_test' ), 'Deleting the option should drop cached reports.' );
+
+ ReportsCache::set( 'wc_reports_sync_test', 'cached' );
+ add_option( $option, $old_value );
+ $this->assertFalse( ReportsCache::get( 'wc_reports_sync_test' ), 'Adding the option should drop cached reports.' );
+ }
}