Commit 7a9b7b0c420 for woocommerce

commit 7a9b7b0c420d19ee4e39cd93f99df30373b65d6d
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date:   Wed Nov 25 00:22:20 2026 +0100

    Agent readiness: don't fetch the rules file from WooCommerce.com with marketplace suggestions turned off (#71356)

    * Agent readiness: don't fetch the rules file from WooCommerce.com with marketplace suggestions turned off

    With woocommerce_show_marketplace_suggestions set to no, the daily fetch
    of the published rules file is skipped and the stored or bundled checks
    apply, as for the other WooCommerce.com fetches. A URL set with the
    woocommerce_agent_readiness_rules_url filter is still fetched.

    Fixes WOOAIRR-734.

    * Agent readiness: remove the stored rules file when its fetch is turned off

    With marketplace suggestions off, or an empty rules URL, a rules file stored
    earlier kept running. A stored file is the list of checks, so the store got
    neither new published rules nor checks added in later releases. The next scan
    now removes the copy and the bundled checks run.

diff --git a/plugins/woocommerce/changelog/fix-agent-readiness-rules-suggestions-opt-out b/plugins/woocommerce/changelog/fix-agent-readiness-rules-suggestions-opt-out
new file mode 100644
index 00000000000..39e608025af
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-agent-readiness-rules-suggestions-opt-out
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Skip the AI shopping health rules fetch when marketplace suggestions are off; unreleased feature.
+
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/README.md b/plugins/woocommerce/src/Internal/AgentReadiness/README.md
index ba5b8607df1..600fa437f16 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/README.md
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/README.md
@@ -174,7 +174,7 @@ array(

 ## Rules file

-Channel specs change faster than WooCommerce releases, so each background scan first fetches a published rules file (`RemoteRules`). Only a GET of the file is made; the request's user agent includes the site URL, as other WooCommerce.com polls do. The `woocommerce_agent_readiness_rules_url` filter changes the URL; an empty URL turns the fetch off. With no stored file, or a failed fetch, the bundled checks run.
+Channel specs change faster than WooCommerce releases, so each background scan first fetches a published rules file (`RemoteRules`). Only a GET of the file is made; the request's user agent includes the site URL, as other WooCommerce.com polls do. The `woocommerce_agent_readiness_rules_url` filter changes the URL; an empty URL turns the fetch off. With marketplace suggestions turned off (`woocommerce_show_marketplace_suggestions` set to `no`), the WooCommerce.com file isn't fetched, as for the other WooCommerce.com polls; a URL set by the filter is still fetched. With the fetch turned off either way, the next scan removes a copy stored earlier and the bundled checks run, so the store gets the checks of its WooCommerce version instead of a file that can no longer update. With no stored file the bundled checks run; a failed fetch keeps the last good copy.

 The file has a `version` (the date of its last change, `Y-m-d`), a `checks` object and an optional `store_checks` object. `store_checks` changes store checks by id: `"required": true` or `false` sets whether a setup step or output check is shown as Required (badge, Fix first, Setup checklist), and `"off": true` stops it running and leaves it out of the report. An id it leaves out keeps its bundled behaviour (`StoreConditions::SHOWN_REQUIRED`). Shown as Required still doesn't keep AI shopping from being turned on; only `launched`, `https` and `ucp_discovery` do, and those are always Required and can't be turned off. The ids in `checks` are the checks that run, and each one has to match a check shipped in code. An id the file leaves out does not run. An id code does not know, or an unknown channel, is dropped. A check whose entry has a known channel with a missing field, an effect other than `not_listable` or `gaps`, or a bad `effect_from` runs with its bundled channels, so a typo never turns a check off. With no stored file, or a file that names nothing this code can run, the bundled checks run. A failed or malformed fetch keeps the last good copy (option `woocommerce_agent_readiness_remote_rules`).

diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/RemoteRules.php b/plugins/woocommerce/src/Internal/AgentReadiness/RemoteRules.php
index 8a39d03b6fb..fe3c1a8e584 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/RemoteRules.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/RemoteRules.php
@@ -27,6 +27,10 @@ defined( 'ABSPATH' ) || exit;
  * scan that is asked for. Only a GET is made; the request's user agent
  * includes the site URL, as other WooCommerce.com polls do. A failed fetch
  * keeps the last good copy, and with no copy the bundled checks apply.
+ * With marketplace suggestions turned off (woocommerce_show_marketplace_suggestions), the
+ * WooCommerce.com file is not fetched, as for the other WooCommerce.com polls. A URL set by the
+ * filter is still fetched. With the fetch turned off, by that setting or an empty URL, a copy
+ * stored earlier is removed at the next scan and the bundled checks apply.
  *
  * The file is fetched from PUBLISHED_URL, the same host as the payment gateway
  * suggestions; the woocommerce_agent_readiness_rules_url filter changes it.
@@ -48,7 +52,7 @@ class RemoteRules {
 	/**
 	 * Fetch the rules file and store it when at least one check entry is valid
 	 * (see is_valid_check()) and, when known ids are given, is one of them.
-	 * Anything less keeps the last good copy.
+	 * Anything less keeps the last good copy. With the fetch turned off, the copy is removed.
 	 *
 	 * @param array<int, string>|null $known_ids Check ids the code can run; null accepts any id.
 	 * @return bool Whether a new copy was stored.
@@ -62,7 +66,11 @@ class RemoteRules {
 		 * @param string $url Rules file URL.
 		 */
 		$url = apply_filters( 'woocommerce_agent_readiness_rules_url', self::PUBLISHED_URL );
-		if ( ! is_string( $url ) || '' === $url ) {
+		// The opt-out is about reaching WooCommerce.com. A URL a developer set is still fetched.
+		$opted_out = self::PUBLISHED_URL === $url && 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' );
+		if ( ! is_string( $url ) || '' === $url || $opted_out ) {
+			// A copy stored earlier could never update, and would hide checks added in later releases.
+			delete_option( self::OPTION );
 			return false;
 		}

diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/RemoteRulesTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/RemoteRulesTest.php
index 135c5163669..223c5d8a535 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/RemoteRulesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/RemoteRulesTest.php
@@ -32,6 +32,18 @@ class RemoteRulesTest extends WC_Unit_Test_Case {
 	 */
 	private array $requests = array();

+	/**
+	 * A rules file that names one check, so every other bundled check is left out.
+	 */
+	private const BRAND_ONLY = array(
+		'missing_brand' => array(
+			'google' => array(
+				'field'  => 'brand',
+				'effect' => 'not_listable',
+			),
+		),
+	);
+
 	/**
 	 * Body and status the next request answers with.
 	 *
@@ -57,6 +69,7 @@ class RemoteRulesTest extends WC_Unit_Test_Case {
 	 */
 	public function tearDown(): void {
 		delete_option( RemoteRules::OPTION );
+		delete_option( 'woocommerce_show_marketplace_suggestions' );
 		parent::tearDown();
 	}

@@ -127,16 +140,57 @@ class RemoteRulesTest extends WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox With no URL set nothing is fetched and the bundled rules apply.
+	 * @testdox With no URL set nothing is fetched, a copy stored earlier is removed and the bundled rules apply.
 	 */
 	public function test_no_url_makes_no_request(): void {
+		$this->serve( self::BRAND_ONLY );
+		$this->assertTrue( $this->sut->refresh(), 'A copy is stored first' );
+		$this->assertArrayNotHasKey( 'missing_images', $this->agent_readiness()->get_channel_rules(), 'The stored file is the list' );
+		$this->requests = array();
+		remove_all_filters( 'woocommerce_agent_readiness_rules_url' );
 		add_filter( 'woocommerce_agent_readiness_rules_url', '__return_empty_string' );
+
 		$this->assertFalse( $this->sut->refresh() );
 		$this->assertSame( array(), $this->requests );
 		$this->assertSame( array(), $this->sut->channel_rules() );
 		$this->assertArrayHasKey( 'missing_images', $this->agent_readiness()->get_channel_rules(), 'With no file, the bundled checks run' );
 	}

+	/**
+	 * @testdox With marketplace suggestions turned off the WooCommerce.com file isn't fetched, and a copy stored earlier is removed so the bundled checks run; a URL set by the filter is still fetched.
+	 */
+	public function test_suggestions_opt_out_skips_the_published_file(): void {
+		$this->serve( self::BRAND_ONLY );
+		$this->assertTrue( $this->sut->refresh(), 'A copy is stored first' );
+		$this->assertArrayNotHasKey( 'missing_images', $this->agent_readiness()->get_channel_rules(), 'The stored file is the list' );
+		remove_all_filters( 'woocommerce_agent_readiness_rules_url' );
+		$this->requests = array();
+		update_option( 'woocommerce_show_marketplace_suggestions', 'no' );
+
+		$this->assertFalse( $this->sut->refresh() );
+		$this->assertSame( array(), $this->requests, 'Nothing is fetched from WooCommerce.com' );
+		$this->assertFalse( $this->sut->has_stored_file(), 'The stored copy is removed' );
+		$this->assertSame( '', $this->sut->version() );
+		$this->assertArrayHasKey( 'missing_images', $this->agent_readiness()->get_channel_rules(), 'A bundled check the old file left out runs again' );
+
+		$this->serve( self::BRAND_ONLY );
+		$this->assertTrue( $this->sut->refresh(), 'A URL set by the filter is still fetched' );
+		$this->assertCount( 1, $this->requests );
+	}
+
+	/**
+	 * @testdox A file from a URL set by the filter stays stored with marketplace suggestions turned off.
+	 */
+	public function test_suggestions_opt_out_keeps_a_file_from_a_filtered_url(): void {
+		update_option( 'woocommerce_show_marketplace_suggestions', 'no' );
+		$this->serve( self::BRAND_ONLY );
+		$this->assertTrue( $this->sut->refresh() );
+		$this->answer['code'] = 500;
+
+		$this->assertFalse( $this->sut->refresh(), 'The fetch fails' );
+		$this->assertTrue( $this->sut->has_stored_file(), 'The last good copy stays' );
+	}
+
 	/**
 	 * @testdox A check no channel asks for, such as image alt text, runs when the file names it with an empty channel map, and not when the file leaves it out.
 	 */