Commit 9795570b466 for woocommerce

commit 9795570b466347394bf63b85b779c64352011f52
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date:   Thu Nov 19 14:50:57 2026 +0100

    Agent readiness: add check statuses to the WC Tracker snapshot (#71023)

diff --git a/plugins/woocommerce/changelog/add-agent-readiness-tracker-snapshot b/plugins/woocommerce/changelog/add-agent-readiness-tracker-snapshot
new file mode 100644
index 00000000000..40873e324ac
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-agent-readiness-tracker-snapshot
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Experimental agent readiness adds check statuses to the WC Tracker snapshot; behind a hidden flag, no merchant change.
diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php
index 2e45af8370b..4a8f32d56db 100644
--- a/plugins/woocommerce/includes/class-woocommerce.php
+++ b/plugins/woocommerce/includes/class-woocommerce.php
@@ -467,6 +467,7 @@ final class WooCommerce {
 		$container->get( Automattic\WooCommerce\Internal\AgentReadiness\AgenticCommerceSettings::class )->register();
 		$container->get( Automattic\WooCommerce\Internal\AgentReadiness\ReadinessCheckPage::class )->register();
 		$container->get( Automattic\WooCommerce\Internal\AgentReadiness\ProductsList::class )->register();
+		$container->get( Automattic\WooCommerce\Internal\AgentReadiness\Telemetry::class )->register();
 		$container->get( Automattic\WooCommerce\Internal\Admin\OrderTaxLookupMigrator::class )->register();
 		$container->get( Automattic\WooCommerce\Internal\ProductDownloads\DownloadableFileAttachments::class )->register();
 		$container->get( ProductUtil::class )->register();
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/README.md b/plugins/woocommerce/src/Internal/AgentReadiness/README.md
index e6d23ef1d1c..f3c39e6397e 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/README.md
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/README.md
@@ -221,6 +221,18 @@ Fix first, a card of its own, lists the required setup steps that fail, then the

 Tracks event: `agentic_commerce_ucp_toggled` (`enabled`), sent only when the store allows tracking.

+## Usage tracking
+
+With the flag on, the weekly WC Tracker snapshot (sent only by stores that allow usage tracking) has an `agent_readiness` block, from `Telemetry`:
+
+- `feature_enabled`: `yes` or `no`. With the flag off, the other fields stay empty.
+- `last_scan`: the day the stored report was built (`Y-m-d`), or empty before the first scan.
+- `truncated`: `yes` when the last scan stopped at the product limit, otherwise `no` (including before the first scan). On a truncated scan, product check statuses cover only the products that were checked.
+- `store_checks`: each store check id with `pass`, `problem` or `unknown` (couldn't check, or still waiting for a result). Setup steps are read when the snapshot is built, so they are there before the first scan too; output checks come from the stored report.
+- `product_checks`: each product check id with `problem` when any product fails it, otherwise `pass`.
+
+Check ids and statuses only: no URLs, product names, ids or counts.
+
 ## Updating rules

 The per-channel field and effect live in each check's `channels()`, and that is what runs until a rules file is stored. After that, `rules.json` is the list. The `woocommerce_agent_readiness_rules` filter still runs last; unknown channels and effects in what it returns are dropped.
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/Telemetry.php b/plugins/woocommerce/src/Internal/AgentReadiness/Telemetry.php
new file mode 100644
index 00000000000..5c5192edcaa
--- /dev/null
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/Telemetry.php
@@ -0,0 +1,115 @@
+<?php
+/**
+ * Agent readiness fields in the WC Tracker snapshot.
+ */
+
+declare( strict_types=1 );
+
+namespace Automattic\WooCommerce\Internal\AgentReadiness;
+
+use Automattic\WooCommerce\Internal\Features\FeaturesController;
+use Automattic\WooCommerce\Internal\RegisterHooksInterface;
+
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * Adds the readiness check to the weekly WC Tracker snapshot, which only stores that opted in to usage tracking send.
+ * Check ids and statuses only: no URLs, product names or store content.
+ */
+class Telemetry implements RegisterHooksInterface {
+
+	public const PASS    = 'pass';
+	public const PROBLEM = 'problem';
+	public const UNKNOWN = 'unknown';
+
+	/**
+	 * Feature flags.
+	 *
+	 * @var FeaturesController
+	 */
+	private FeaturesController $features_controller;
+
+	/**
+	 * Readiness report.
+	 *
+	 * @var AgentReadiness
+	 */
+	private AgentReadiness $agent_readiness;
+
+	/**
+	 * Initialize dependencies.
+	 *
+	 * @internal
+	 *
+	 * @param FeaturesController $features_controller Feature flags.
+	 * @param AgentReadiness     $agent_readiness     Readiness report.
+	 */
+	final public function init( FeaturesController $features_controller, AgentReadiness $agent_readiness ): void {
+		$this->features_controller = $features_controller;
+		$this->agent_readiness     = $agent_readiness;
+	}
+
+	/**
+	 * Register the tracker snapshot filter.
+	 */
+	public function register(): void {
+		add_filter( 'woocommerce_tracker_data', array( $this, 'add_snapshot_to_tracker_data' ) );
+	}
+
+	/**
+	 * Add the agent_readiness block to WC_Tracker's payload.
+	 *
+	 * @internal
+	 *
+	 * @param mixed $data Tracker data.
+	 * @return mixed
+	 */
+	public function add_snapshot_to_tracker_data( $data ) {
+		if ( is_array( $data ) ) {
+			$data['agent_readiness'] = $this->snapshot();
+		}
+		return $data;
+	}
+
+	/**
+	 * The feature flag, when the last scan ran, whether it stopped at the product limit, and each check's status.
+	 * The store conditions are read now; output and product checks are as the last scan left them.
+	 *
+	 * @return array{feature_enabled:string,last_scan:string,truncated:string,store_checks:array<string,string>,product_checks:array<string,string>}
+	 */
+	public function snapshot(): array {
+		$snapshot = array(
+			'feature_enabled' => 'no',
+			'last_scan'       => '',
+			'truncated'       => 'no',
+			'store_checks'    => array(),
+			'product_checks'  => array(),
+		);
+		if ( ! $this->features_controller->feature_is_enabled( AgenticCommerceSettings::FEATURE_ID ) ) {
+			return $snapshot;
+		}
+		$snapshot['feature_enabled'] = 'yes';
+
+		// Setup steps are read now, so they are known before the first scan too.
+		$report = $this->agent_readiness->get_served_report();
+		if ( null !== $report ) {
+			$scanned               = strtotime( (string) ( $report['generated_at'] ?? '' ) );
+			$snapshot['last_scan'] = false !== $scanned ? gmdate( 'Y-m-d', $scanned ) : '';
+			$snapshot['truncated'] = empty( $report['truncated'] ) ? 'no' : 'yes';
+		}
+
+		foreach ( (array) ( $report['store_checks'] ?? $this->agent_readiness->get_store_checks( null ) ) as $check ) {
+			if ( ! is_array( $check ) || ! isset( $check['id'] ) ) {
+				continue;
+			}
+			$ok = empty( $check['pending'] ) ? ( $check['ok'] ?? null ) : null;
+			$snapshot['store_checks'][ (string) $check['id'] ] = null === $ok ? self::UNKNOWN : ( $ok ? self::PASS : self::PROBLEM );
+		}
+		foreach ( (array) ( $report['checks'] ?? array() ) as $check ) {
+			if ( is_array( $check ) && isset( $check['id'] ) ) {
+				$snapshot['product_checks'][ (string) $check['id'] ] = (int) ( $check['count'] ?? 0 ) > 0 ? self::PROBLEM : self::PASS;
+			}
+		}
+		return $snapshot;
+	}
+}
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/TelemetryTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/TelemetryTest.php
new file mode 100644
index 00000000000..499636e2661
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/TelemetryTest.php
@@ -0,0 +1,195 @@
+<?php
+declare( strict_types=1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\AgentReadiness;
+
+use Automattic\WooCommerce\Internal\AgentReadiness\AgentReadiness;
+use Automattic\WooCommerce\Internal\AgentReadiness\Telemetry;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for the agent readiness fields in the WC Tracker snapshot.
+ */
+class TelemetryTest extends WC_Unit_Test_Case {
+
+	/**
+	 * System under test.
+	 *
+	 * @var Telemetry
+	 */
+	private Telemetry $sut;
+
+	/**
+	 * Set up.
+	 */
+	public function setUp(): void {
+		parent::setUp();
+		update_option( 'woocommerce_feature_agent_readiness_enabled', 'yes' );
+		delete_option( AgentReadiness::REPORT_OPTION );
+		$this->sut = wc_get_container()->get( Telemetry::class );
+	}
+
+	/**
+	 * Tear down.
+	 */
+	public function tearDown(): void {
+		delete_option( 'woocommerce_feature_agent_readiness_enabled' );
+		parent::tearDown();
+	}
+
+	/**
+	 * Store a report as the scan would.
+	 */
+	private function store_report(): void {
+		update_option(
+			AgentReadiness::REPORT_OPTION,
+			array(
+				'generated_at' => '2026-11-14T08:30:00+00:00',
+				'store_checks' => array(
+					array(
+						'id'      => 'robots_allows_agents',
+						'ok'      => true,
+						'name'    => 'AI assistants can read your store',
+						'fix_url' => 'https://example.com/wp-admin/options-reading.php',
+					),
+					array(
+						'id'   => 'product_schema',
+						'ok'   => false,
+						'name' => 'Product structured data',
+					),
+					array(
+						'id'   => 'llms_txt',
+						'ok'   => null,
+						'name' => 'llms.txt',
+					),
+					array(
+						'id'      => 'open_graph',
+						'ok'      => false,
+						'pending' => true,
+						'name'    => 'Open Graph',
+					),
+				),
+				'checks'       => array(
+					array(
+						'id'          => 'missing_brand',
+						'label'       => 'Add a brand',
+						'count'       => 3,
+						'product_ids' => array( 11, 12, 13 ),
+					),
+					array(
+						'id'          => 'missing_image',
+						'label'       => 'Add an image',
+						'count'       => 0,
+						'product_ids' => array(),
+					),
+				),
+			)
+		);
+	}
+
+	/**
+	 * @testdox The snapshot is added to the tracker data under agent_readiness.
+	 */
+	public function test_adds_the_block_to_the_tracker_data(): void {
+		global $wp_filter;
+		$registered = false;
+		foreach ( $wp_filter['woocommerce_tracker_data']->callbacks ?? array() as $callbacks ) {
+			foreach ( $callbacks as $callback ) {
+				$registered = $registered || ( is_array( $callback['function'] ) && $callback['function'][0] instanceof Telemetry );
+			}
+		}
+		$this->assertTrue( $registered, 'WooCommerce registers the snapshot filter on load' );
+
+		$data = $this->sut->add_snapshot_to_tracker_data( array( 'version' => '11.4.0' ) );
+
+		$this->assertSame( '11.4.0', $data['version'] );
+		$this->assertSame( 'yes', $data['agent_readiness']['feature_enabled'] );
+	}
+
+	/**
+	 * @testdox With the feature off, the snapshot says so and reads nothing else.
+	 */
+	public function test_feature_off(): void {
+		update_option( 'woocommerce_feature_agent_readiness_enabled', 'no' );
+		$this->store_report();
+
+		$snapshot = $this->sut->snapshot();
+
+		$this->assertSame( 'no', $snapshot['feature_enabled'] );
+		$this->assertSame( '', $snapshot['last_scan'] );
+		$this->assertSame( 'no', $snapshot['truncated'] );
+		$this->assertSame( array(), $snapshot['store_checks'] );
+		$this->assertSame( array(), $snapshot['product_checks'] );
+	}
+
+	/**
+	 * @testdox Before the first scan, the snapshot has the setup steps but no scan date or product checks.
+	 */
+	public function test_before_the_first_scan(): void {
+		$snapshot = $this->sut->snapshot();
+
+		$this->assertSame( 'yes', $snapshot['feature_enabled'] );
+		$this->assertSame( '', $snapshot['last_scan'] );
+		$this->assertSame( 'no', $snapshot['truncated'] );
+		$this->assertArrayHasKey( 'https', $snapshot['store_checks'] );
+		$this->assertArrayNotHasKey( 'product_schema', $snapshot['store_checks'], 'Output checks only run in the scan' );
+		$this->assertSame( array(), $snapshot['product_checks'] );
+	}
+
+	/**
+	 * @testdox Each check is pass, problem or unknown, with the scan's date and no store content.
+	 */
+	public function test_statuses_from_the_stored_report(): void {
+		$this->store_report();
+
+		$snapshot = $this->sut->snapshot();
+
+		$this->assertSame( '2026-11-14', $snapshot['last_scan'] );
+		$this->assertSame( 'no', $snapshot['truncated'] );
+		$this->assertSame( Telemetry::PASS, $snapshot['store_checks']['robots_allows_agents'] );
+		$this->assertSame( Telemetry::PROBLEM, $snapshot['store_checks']['product_schema'] );
+		$this->assertSame( Telemetry::UNKNOWN, $snapshot['store_checks']['llms_txt'], 'A check that could not run' );
+		$this->assertSame( Telemetry::UNKNOWN, $snapshot['store_checks']['open_graph'], 'A check still waiting for a result' );
+		$this->assertArrayHasKey( 'https', $snapshot['store_checks'], 'Setup steps are read now, not from the report' );
+		$this->assertSame( Telemetry::PROBLEM, $snapshot['product_checks']['missing_brand'] );
+		$this->assertSame( Telemetry::PASS, $snapshot['product_checks']['missing_image'] );
+
+		$sent = (string) wp_json_encode( $snapshot );
+		$this->assertStringNotContainsString( 'http', str_replace( '"https"', '', $sent ), 'No URLs' );
+		$this->assertStringNotContainsString( 'Add a brand', $sent, 'No check text' );
+		$this->assertStringNotContainsString( 'product_ids', $sent, 'No product ids' );
+	}
+
+	/**
+	 * @testdox A scan that stopped at the product limit is marked truncated in the snapshot.
+	 */
+	public function test_truncated_scan_is_flagged(): void {
+		$this->store_report();
+		$report              = get_option( AgentReadiness::REPORT_OPTION );
+		$report['truncated'] = true;
+		update_option( AgentReadiness::REPORT_OPTION, $report );
+
+		$snapshot = $this->sut->snapshot();
+
+		$this->assertSame( 'yes', $snapshot['truncated'] );
+	}
+
+	/**
+	 * @testdox Without the usage tracking opt-in, the tracker never builds the snapshot.
+	 */
+	public function test_nothing_is_built_without_the_tracking_opt_in(): void {
+		update_option( 'woocommerce_allow_tracking', 'no' );
+		$this->store_report();
+		$built = 0;
+		$spy   = function ( $data ) use ( &$built ) {
+			++$built;
+			return $data;
+		};
+		add_filter( 'woocommerce_tracker_data', $spy );
+
+		WC()->add_woocommerce_tracker_send_event_wrapper();
+
+		remove_filter( 'woocommerce_tracker_data', $spy );
+		$this->assertSame( 0, $built );
+	}
+}