Commit f0fdfc01268 for woocommerce

commit f0fdfc01268555d0fc8e718848a96907afea3e48
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date:   Fri Nov 20 22:55:47 2026 +0100

    Agent readiness: Connect to WordPress.com card on the AI tools Overview (#71218)

    * Agent readiness: Connect to WordPress.com card on the AI tools Overview

    Manage your store from your own agent sits above AI shopping. Its button posts to a
    nonce-checked admin-post action that registers the site if needed and sends the user to
    the Jetpack connection screen, which returns to the Overview. Connected stores get the
    success notice once, then the steps to add WordPress in an AI app.

    * Test that only users who can manage WooCommerce can start a connection

    * Use WooCommerce connection screen for AI tools

    * Fix AI tools connect to post its nonce and reject off-site redirects.

    * Remove the connect form after the card test, including when an assertion fails.

    ---------

    Co-authored-by: Bero <1199991+bgrgicak@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/add-ai-tools-agent-connect b/plugins/woocommerce/changelog/add-ai-tools-agent-connect
new file mode 100644
index 00000000000..fc96c487991
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-ai-tools-agent-connect
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Connect to WordPress.com card on the AI tools Overview, behind the agent_readiness feature flag; nothing ships to merchants.
+
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/components/agent-connect.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/components/agent-connect.tsx
new file mode 100644
index 00000000000..0de04bb4fed
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/components/agent-connect.tsx
@@ -0,0 +1,136 @@
+/**
+ * External dependencies
+ */
+import { Card, Text } from '@wordpress/ui';
+import { Button, Notice } from '@wordpress/components';
+import { useState } from '@wordpress/element';
+import { __ } from '@wordpress/i18n';
+
+/**
+ * Internal dependencies
+ */
+import { trackReadiness } from '../tracks';
+
+/** The connection card's data, from AgenticCommerceSettings::connect_view(). */
+export type ConnectView = {
+	connected: boolean;
+	/** After a connection attempt: connected, error, or empty. */
+	notice: '' | 'connected' | 'error';
+	/** admin-post.php. Empty once connected. */
+	url: string;
+	/** admin-post action. Empty once connected. */
+	action: string;
+	/** Nonce for that action, posted with it. Empty once connected. */
+	nonce: string;
+};
+
+/**
+ * Manage your store from your own agent: connects the store to WordPress.com, so AI apps can reach
+ * it through MCP, and says what to do next once it is connected.
+ */
+const RESULT_ARG = 'wc_ai_connect';
+
+/**
+ * Posts the connect action outside the settings form, which already wraps this card.
+ */
+function postConnect( connect: ConnectView ) {
+	trackReadiness( 'link_clicked', {
+		surface: 'overview',
+		area: 'agent_connect',
+	} );
+	const form = document.createElement( 'form' );
+	form.method = 'post';
+	form.action = connect.url;
+	for ( const [ name, value ] of [
+		[ 'action', connect.action ],
+		[ '_wpnonce', connect.nonce ],
+	] as const ) {
+		const input = document.createElement( 'input' );
+		input.type = 'hidden';
+		input.name = name;
+		input.value = value;
+		form.appendChild( input );
+	}
+	document.body.appendChild( form );
+	form.submit();
+}
+
+export const AgentConnect = ( { connect }: { connect: ConnectView } ) => {
+	const [ notice, setNotice ] = useState( connect.notice );
+	const dismissNotice = () => {
+		setNotice( '' );
+		const next = new URL( window.location.href );
+		next.searchParams.delete( RESULT_ARG );
+		window.history.replaceState( null, '', next );
+	};
+
+	return (
+		<Card.Root className="wc-sales-readiness-card">
+			<Card.Header>
+				<Card.Title className="wc-sales-readiness-card__title">
+					{ __(
+						'Manage your store from your own agent',
+						'woocommerce'
+					) }
+				</Card.Title>
+			</Card.Header>
+			<Card.Content>
+				{ notice === 'connected' && (
+					<Notice status="success" onRemove={ dismissNotice }>
+						<strong>
+							{ __( 'Connection successful', 'woocommerce' ) }
+						</strong>
+						<br />
+						{ __(
+							'Your store is connected to WordPress.com.',
+							'woocommerce'
+						) }
+					</Notice>
+				) }
+				{ notice === 'error' && (
+					<Notice status="error" onRemove={ dismissNotice }>
+						{ __(
+							'Your store couldn’t connect to WordPress.com. Try again.',
+							'woocommerce'
+						) }
+					</Notice>
+				) }
+				{ connect.connected ? (
+					<Text
+						variant="body-md"
+						className="wc-sales-readiness__muted"
+					>
+						{ __(
+							'Supported AI apps can now connect to this store through MCP. To get started, add WordPress in the AI app you want to use and select this store.',
+							'woocommerce'
+						) }
+					</Text>
+				) : (
+					<>
+						<Text
+							variant="body-md"
+							className="wc-sales-readiness__muted"
+						>
+							{ __(
+								'Connect this store to WordPress.com so supported AI apps can access your WooCommerce store.',
+								'woocommerce'
+							) }
+						</Text>
+						<div className="wc-sales-readiness-card__actions">
+							<Button
+								variant="primary"
+								type="button"
+								onClick={ () => postConnect( connect ) }
+							>
+								{ __(
+									'Connect to WordPress.com',
+									'woocommerce'
+								) }
+							</Button>
+						</div>
+					</>
+				) }
+			</Card.Content>
+		</Card.Root>
+	);
+};
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx
index 448f605e1d8..4edd6dc5dcd 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/overview.tsx
@@ -13,6 +13,7 @@ import './sales-readiness.scss';
 import type { ReadinessView } from './types';
 import { useReadinessView } from './use-readiness-view';
 import { FixFirstList } from './components/fix-first';
+import { AgentConnect, type ConnectView } from './components/agent-connect';
 import { ErrorNotice } from './components/error-notice';
 import { RunStatus } from './components/run-status';
 import { trackReadiness } from './tracks';
@@ -30,13 +31,15 @@ function everyCheckPassed( view: ReadinessView ): boolean {
 }

 /**
- * WooCommerce > Settings > AI tools, the Overview: the AI shopping card, then the Sales readiness
- * card with Fix first and a link to every check.
+ * WooCommerce > Settings > AI tools, the Overview: the WordPress.com connection card, the AI shopping
+ * card, then the Sales readiness card with Fix first and a link to every check.
  */
 export default function SalesReadinessOverview( {
 	initialView = null,
+	connect = null,
 }: {
 	initialView?: ReadinessView | null;
+	connect?: ConnectView | null;
 } ) {
 	const { view, error, reload, running, runCheck } = useReadinessView(
 		'overview',
@@ -44,10 +47,15 @@ export default function SalesReadinessOverview( {
 	);

 	if ( ! view ) {
-		return error ? (
-			<ErrorNotice message={ error } reload={ reload } />
-		) : (
-			<Spinner />
+		return (
+			<div className="wc-sales-readiness wc-sales-readiness--overview">
+				{ error ? (
+					<ErrorNotice message={ error } reload={ reload } />
+				) : (
+					<Spinner />
+				) }
+				{ connect && <AgentConnect connect={ connect } /> }
+			</div>
 		);
 	}
 	const { shopping, view_all: viewAll } = view.overview;
@@ -67,6 +75,7 @@ export default function SalesReadinessOverview( {
 	return (
 		<div className="wc-sales-readiness wc-sales-readiness--overview">
 			{ error && <ErrorNotice message={ error } reload={ reload } /> }
+			{ connect && <AgentConnect connect={ connect } /> }
 			<Card.Root className="wc-sales-readiness-card">
 				<Card.Header>
 					<div className="wc-sales-readiness-card__head">
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/test/agent-connect.test.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/test/agent-connect.test.tsx
new file mode 100644
index 00000000000..03b1603798e
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/test/agent-connect.test.tsx
@@ -0,0 +1,121 @@
+/**
+ * External dependencies
+ */
+import { fireEvent, render, screen } from '@testing-library/react';
+
+/**
+ * Internal dependencies
+ */
+import { AgentConnect } from '../components/agent-connect';
+import { trackReadiness } from '../tracks';
+
+jest.mock( '../tracks', () => ( { trackReadiness: jest.fn() } ) );
+
+const connect = {
+	connected: false as const,
+	notice: '' as const,
+	url: 'https://example.com/wp-admin/admin-post.php',
+	action: 'woocommerce_ai_tools_connect',
+	nonce: 'abc',
+};
+
+describe( 'AgentConnect', () => {
+	afterEach( () => {
+		jest.restoreAllMocks();
+		document.body.querySelector( 'form' )?.remove();
+	} );
+
+	it( 'posts Connect to WordPress.com to the connect action and tracks the click', () => {
+		const submit = jest
+			.spyOn( window.HTMLFormElement.prototype, 'submit' )
+			.mockImplementation( () => {} );
+		render( <AgentConnect connect={ connect } /> );
+
+		fireEvent.click(
+			screen.getByRole( 'button', { name: 'Connect to WordPress.com' } )
+		);
+		const form = document.body.querySelector( 'form' );
+		expect( form ).toHaveAttribute( 'method', 'post' );
+		expect( form ).toHaveAttribute( 'action', connect.url );
+		expect( form?.querySelector( 'input[name="action"]' ) ).toHaveValue(
+			connect.action
+		);
+		expect( form?.querySelector( 'input[name="_wpnonce"]' ) ).toHaveValue(
+			connect.nonce
+		);
+		expect( submit ).toHaveBeenCalled();
+		expect( trackReadiness ).toHaveBeenCalledWith( 'link_clicked', {
+			surface: 'overview',
+			area: 'agent_connect',
+		} );
+		expect(
+			screen.queryByText( 'add WordPress in the AI app', {
+				exact: false,
+			} )
+		).not.toBeInTheDocument();
+	} );
+
+	it( 'shows the success notice once, then the next step, without the button', () => {
+		const connected = {
+			connected: true as const,
+			notice: 'connected' as const,
+			url: '',
+			action: '',
+			nonce: '',
+		};
+		window.history.replaceState(
+			null,
+			'',
+			'?page=wc-settings&wc_ai_connect=connected'
+		);
+		const { unmount } = render( <AgentConnect connect={ connected } /> );
+
+		expect(
+			screen.getByText( 'Connection successful' )
+		).toBeInTheDocument();
+		expect(
+			screen.getByText( 'add WordPress in the AI app', {
+				exact: false,
+			} )
+		).toBeInTheDocument();
+		expect(
+			screen.queryByRole( 'button', { name: 'Connect to WordPress.com' } )
+		).not.toBeInTheDocument();
+
+		fireEvent.click( screen.getByRole( 'button', { name: 'Close' } ) );
+		expect(
+			screen.queryByText( 'Connection successful' )
+		).not.toBeInTheDocument();
+		expect(
+			screen.getByText( 'add WordPress in the AI app', {
+				exact: false,
+			} )
+		).toBeInTheDocument();
+		expect( window.location.search ).toBe( '?page=wc-settings' );
+
+		unmount();
+		render( <AgentConnect connect={ { ...connected, notice: '' } } /> );
+		expect(
+			screen.queryByText( 'Connection successful' )
+		).not.toBeInTheDocument();
+		expect(
+			screen.getByText( 'add WordPress in the AI app', {
+				exact: false,
+			} )
+		).toBeInTheDocument();
+	} );
+
+	it( 'keeps the button next to the error when connecting failed', () => {
+		render( <AgentConnect connect={ { ...connect, notice: 'error' } } /> );
+
+		expect(
+			screen.getByText(
+				'Your store couldn’t connect to WordPress.com. Try again.',
+				{ selector: '.components-notice__content' }
+			)
+		).toBeInTheDocument();
+		expect(
+			screen.getByRole( 'button', { name: 'Connect to WordPress.com' } )
+		).toBeInTheDocument();
+	} );
+} );
diff --git a/plugins/woocommerce/client/admin/client/sales-readiness/test/tracks.test.tsx b/plugins/woocommerce/client/admin/client/sales-readiness/test/tracks.test.tsx
index 40c6c5e015d..14bdbd3ee13 100644
--- a/plugins/woocommerce/client/admin/client/sales-readiness/test/tracks.test.tsx
+++ b/plugins/woocommerce/client/admin/client/sales-readiness/test/tracks.test.tsx
@@ -437,6 +437,29 @@ describe( 'Sales readiness tracking', () => {
 	} );
 } );

+describe( 'AI tools overview connection card', () => {
+	beforeEach( () => mockedApiFetch.mockReset() );
+
+	it( 'offers Connect while the readiness view is still loading', () => {
+		mockedApiFetch.mockReturnValue( new Promise( () => {} ) );
+		render(
+			<SalesReadinessOverview
+				connect={ {
+					connected: false,
+					notice: '',
+					url: 'https://example.com/wp-admin/admin-post.php',
+					action: 'woocommerce_ai_tools_connect',
+					nonce: 'abc',
+				} }
+			/>
+		);
+
+		expect(
+			screen.getByRole( 'button', { name: 'Connect to WordPress.com' } )
+		).toBeInTheDocument();
+	} );
+} );
+
 describe( 'Sales readiness page', () => {
 	beforeEach( () => mockedApiFetch.mockReset() );

diff --git a/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/index.tsx b/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/index.tsx
index 0889e2b8bfb..fd13d63fbb2 100644
--- a/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/index.tsx
+++ b/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/index.tsx
@@ -12,6 +12,9 @@ import { isRTL } from '@wordpress/i18n';
  * Internal dependencies
  */
 import './settings-ui.scss';
+import { readJsonDataset } from './read-json-dataset';
+import type { ConnectView } from '~/sales-readiness/components/agent-connect';
+import type { ReadinessView } from '~/sales-readiness/types';
 import { isFeatureEnabled } from '~/utils/features';
 import {
 	SettingsPaymentsBacsWrapper,
@@ -86,15 +89,18 @@ const SalesReadinessOverview = lazy(
 const renderSalesReadinessOverview = () => {
 	const root = document.getElementById( 'wc-agent-readiness-overview' );
 	if ( root ) {
-		let initialView = null;
-		try {
-			initialView = JSON.parse( root.dataset.view ?? 'null' );
-		} catch {
-			// The Overview loads the view itself.
-		}
+		// Read each attribute on its own. One try would hide the connection card
+		// when the readiness view does not parse; that view can reload, the card cannot.
+		const initialView = readJsonDataset< ReadinessView >(
+			root.dataset.view
+		);
+		const connect = readJsonDataset< ConnectView >( root.dataset.connect );
 		createRoot( root ).render(
 			<Suspense fallback={ null }>
-				<SalesReadinessOverview initialView={ initialView } />
+				<SalesReadinessOverview
+					initialView={ initialView }
+					connect={ connect }
+				/>
 			</Suspense>
 		);
 	}
diff --git a/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/read-json-dataset.ts b/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/read-json-dataset.ts
new file mode 100644
index 00000000000..e90a61c174b
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/read-json-dataset.ts
@@ -0,0 +1,15 @@
+/**
+ * JSON from a data attribute. A missing or unreadable value is null, and this
+ * never throws, so one bad payload cannot stop the next one from being read.
+ *
+ * @param value Attribute value, missing when the attribute is absent.
+ */
+export function readJsonDataset< T = unknown >(
+	value: string | undefined
+): T | null {
+	try {
+		return JSON.parse( value ?? 'null' ) as T;
+	} catch {
+		return null;
+	}
+}
diff --git a/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/test/read-json-dataset.test.ts b/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/test/read-json-dataset.test.ts
new file mode 100644
index 00000000000..ece09808a8d
--- /dev/null
+++ b/plugins/woocommerce/client/admin/client/wp-admin-scripts/settings-embed/test/read-json-dataset.test.ts
@@ -0,0 +1,24 @@
+/**
+ * Internal dependencies
+ */
+import { readJsonDataset } from '../read-json-dataset';
+
+describe( 'readJsonDataset', () => {
+	it( 'returns null for a missing attribute', () => {
+		expect( readJsonDataset( undefined ) ).toBeNull();
+	} );
+
+	it( 'returns null when the attribute is empty, which is what a failed encode leaves', () => {
+		expect( readJsonDataset( '' ) ).toBeNull();
+	} );
+
+	it( 'returns null for JSON that does not parse, instead of throwing', () => {
+		expect( readJsonDataset( '{bad' ) ).toBeNull();
+	} );
+
+	it( 'returns the parsed value', () => {
+		expect( readJsonDataset( '{"connected":false}' ) ).toEqual( {
+			connected: false,
+		} );
+	} );
+} );
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettings.php b/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettings.php
index 7ae36fba847..1ebef816b18 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettings.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettings.php
@@ -4,6 +4,7 @@ declare( strict_types = 1 );
 namespace Automattic\WooCommerce\Internal\AgentReadiness;

 use Automattic\WooCommerce\Internal\Features\FeaturesController;
+use Automattic\WooCommerce\Internal\Jetpack\JetpackConnection;
 use Automattic\WooCommerce\Internal\RegisterHooksInterface;

 defined( 'ABSPATH' ) || exit;
@@ -25,6 +26,16 @@ class AgenticCommerceSettings implements RegisterHooksInterface {
 	 */
 	public const SHOPPING_SECTION = 'shopping';

+	/**
+	 * The admin-post action, and its nonce action, behind Connect to WordPress.com on the Overview.
+	 */
+	public const CONNECT_ACTION = 'woocommerce_ai_tools_connect';
+
+	/**
+	 * Query argument the Overview gets back after connecting: connected or error.
+	 */
+	public const CONNECT_RESULT = 'wc_ai_connect';
+
 	public const PRIVACY_PAGE_OPTION = 'wp_page_for_privacy_policy';

 	public const TERMS_PAGE_OPTION = 'woocommerce_terms_page_id';
@@ -111,12 +122,111 @@ class AgenticCommerceSettings implements RegisterHooksInterface {
 			return;
 		}
 		add_filter( 'woocommerce_get_settings_pages', array( $this, 'handle_woocommerce_get_settings_pages' ) );
+		add_action( 'admin_post_' . self::CONNECT_ACTION, array( $this, 'handle_connect' ) );
 		add_filter( 'admin_body_class', array( $this, 'handle_admin_body_class' ), 20 );
 		// After WooCommerce's rating request (priority 1) and version (20).
 		add_filter( 'admin_footer_text', array( $this, 'handle_admin_footer' ), 99 );
 		add_filter( 'update_footer', array( $this, 'handle_admin_footer' ), 99 );
 	}

+	/**
+	 * Connect to WordPress.com: registers the site if needed, then sends the user to the Jetpack
+	 * connection screen, which returns to the Overview.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @internal
+	 */
+	public function handle_connect(): void {
+		if ( ! current_user_can( 'manage_woocommerce' ) ) {
+			wp_die( esc_html__( 'Sorry, you are not allowed to connect this store.', 'woocommerce' ), 403 );
+		}
+		check_admin_referer( self::CONNECT_ACTION );
+		// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- wordpress_com_https_url() allows only https on wordpress.com.
+		wp_redirect( self::connect_redirect() );
+		exit;
+	}
+
+	/**
+	 * Where Connect to WordPress.com goes: the authorization URL, or back to the Overview with an error
+	 * when the site can't be registered or that URL is not https on WordPress.com.
+	 *
+	 * @since 11.4.0
+	 */
+	public static function connect_redirect(): string {
+		self::ensure_jetpack_activation_source();
+		$result    = JetpackConnection::get_authorization_url( add_query_arg( self::CONNECT_RESULT, 'connected', self::url() ), 'woocommerce-core-profiler' );
+		$candidate = ! empty( $result['success'] ) && is_string( $result['url'] ?? null ) ? $result['url'] : '';
+		$url       = self::wordpress_com_https_url( $candidate );
+		return '' !== $url ? $url : add_query_arg( self::CONNECT_RESULT, 'error', self::url() );
+	}
+
+	/**
+	 * Jetpack unpacks this option with list(). An unset option is false, which PHP 8.5 rejects.
+	 *
+	 * @since 11.4.0
+	 */
+	private static function ensure_jetpack_activation_source(): void {
+		$source = get_option( 'jetpack_activation_source' );
+		if ( is_array( $source ) && isset( $source[0], $source[1] ) ) {
+			return;
+		}
+		update_option( 'jetpack_activation_source', array( 'woocommerce', 'ai-tools' ) );
+	}
+
+	/**
+	 * An https WordPress.com URL with a lowercased scheme, or an empty string.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @param string $url Candidate redirect.
+	 * @return string
+	 */
+	public static function wordpress_com_https_url( string $url ): string {
+		$parts = wp_parse_url( $url );
+		if ( ! is_array( $parts ) || isset( $parts['user'] ) || isset( $parts['pass'] ) ) {
+			return '';
+		}
+		$raw_scheme = isset( $parts['scheme'] ) ? $parts['scheme'] : '';
+		$scheme     = strtolower( $raw_scheme );
+		$host       = strtolower( (string) ( $parts['host'] ?? '' ) );
+		$suffix     = '.wordpress.com';
+		$on_wp      = 'wordpress.com' === $host || ( strlen( $host ) > strlen( $suffix ) && substr( $host, -strlen( $suffix ) ) === $suffix );
+		if ( 'https' !== $scheme || ! $on_wp ) {
+			return '';
+		}
+		return 'https' . substr( $url, strlen( $raw_scheme ) );
+	}
+
+	/**
+	 * The Overview's connection card: whether this store and user are connected to WordPress.com, the
+	 * notice to show after a connection attempt, and the POST that starts one.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @return array{connected: bool, notice: string, url: string, action: string, nonce: string}
+	 */
+	public static function connect_view(): array {
+		$manager = JetpackConnection::get_manager();
+		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks the notice.
+		$result    = isset( $_GET[ self::CONNECT_RESULT ] ) && is_string( $_GET[ self::CONNECT_RESULT ] ) ? sanitize_key( wp_unslash( $_GET[ self::CONNECT_RESULT ] ) ) : '';
+		$connected = $manager->is_connected() && $manager->is_user_connected();
+		if ( 'connected' === $result ) {
+			$notice = $connected ? 'connected' : 'error';
+		} elseif ( 'error' === $result && ! $connected ) {
+			$notice = 'error';
+		} else {
+			$notice = '';
+		}
+		return array(
+			'connected' => $connected,
+			'notice'    => $notice,
+			'url'       => $connected ? '' : admin_url( 'admin-post.php' ),
+			'action'    => $connected ? '' : self::CONNECT_ACTION,
+			'nonce'     => $connected ? '' : wp_create_nonce( self::CONNECT_ACTION ),
+		);
+	}
+
 	/**
 	 * No admin footer on the AI tools tab and on Sales readiness, as in the design: the rating request
 	 * and the version would sit under, or over, the last card.
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php b/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php
index 2086de36bd3..6826f28a45a 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/AgenticCommerceSettingsPage.php
@@ -8,8 +8,8 @@ use Automattic\WooCommerce\StoreApi\Utilities\Ucp\Utils as UcpUtils;
 defined( 'ABSPATH' ) || exit;

 /**
- * WooCommerce > Settings > AI tools. It opens on the Overview, a React view with the AI shopping card and the
- * Sales readiness card; Manage opens AI shopping, a React view with the UCP switch, Product visibility, the
+ * WooCommerce > Settings > AI tools. It opens on the Overview, a React view with the WordPress.com connection
+ * card, the AI shopping card and the Sales readiness card; Manage opens AI shopping, a React view with the UCP switch, Product visibility, the
  * Setup checklist, the store policy fields and the connection URL. settings-embed mounts both, from data
  * this class builds. The UCP switch saves on its own through wc/v3/agent-readiness/ucp; the store policy
  * fields save with Save, which posts this settings form. Only created by AgenticCommerceSettings while the
@@ -179,8 +179,9 @@ class AgenticCommerceSettingsPage extends \WC_Settings_Page {
 			// Cards only; nothing here saves with Save changes.
 			$GLOBALS['hide_save_button'] = true;
 			printf(
-				'<div class="wc-agentic-commerce"><div id="wc-agent-readiness-overview" class="wc-agent-readiness-overview" data-view="%s"></div></div>',
-				esc_attr( self::view_json( $this->readiness_page->view() ) )
+				'<div class="wc-agentic-commerce"><div id="wc-agent-readiness-overview" class="wc-agent-readiness-overview" data-view="%s" data-connect="%s"></div></div>',
+				esc_attr( self::view_json( $this->readiness_page->view() ) ),
+				esc_attr( self::view_json( AgenticCommerceSettings::connect_view() ) )
 			);
 			return;
 		}
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
index f97d2d97a2f..00ca9f6b3d0 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
@@ -136,6 +136,145 @@ class AgenticCommerceSettingsTest extends WC_Unit_Test_Case {
 		$this->assertSame( admin_url( 'admin.php?page=wc-admin&path=/sales-readiness' ), ReadinessCheckPage::url() );
 	}

+	/**
+	 * @testdox Until the store is connected, the connection card posts the nonce-checked connect action.
+	 */
+	public function test_connect_view_posts_the_connect_action(): void {
+		remove_all_actions( 'admin_post_' . AgenticCommerceSettings::CONNECT_ACTION );
+		$this->sut->handle_init();
+		$this->assertNotFalse( has_action( 'admin_post_' . AgenticCommerceSettings::CONNECT_ACTION ), 'The tab registers the connect action' );
+
+		$view = AgenticCommerceSettings::connect_view();
+		$this->assertFalse( $view['connected'], 'The test site is not connected to WordPress.com' );
+		$this->assertSame( '', $view['notice'] );
+		$this->assertSame( admin_url( 'admin-post.php' ), $view['url'] );
+		$this->assertSame( AgenticCommerceSettings::CONNECT_ACTION, $view['action'] );
+		$this->assertStringNotContainsString( '_wpnonce', $view['url'] );
+		$this->assertSame( 1, wp_verify_nonce( $view['nonce'], AgenticCommerceSettings::CONNECT_ACTION ) );
+
+		$processor = new \WP_HTML_Tag_Processor( $this->render_page( '' ) );
+		$this->assertTrue(
+			$processor->next_tag(
+				array(
+					'tag_name'   => 'div',
+					'class_name' => 'wc-agent-readiness-overview',
+				)
+			),
+			'The Overview gets the card data'
+		);
+		$connect = json_decode( (string) $processor->get_attribute( 'data-connect' ), true );
+		$this->assertSame( $view, $connect );
+	}
+
+	/**
+	 * @testdox Coming back from a failed attempt, or a success flag on an unconnected store, shows the error.
+	 *
+	 * @testWith ["error", "error"]
+	 *           ["connected", "error"]
+	 *           ["other", ""]
+	 *
+	 * @param string $result What the URL says.
+	 * @param string $notice The notice the card shows.
+	 */
+	public function test_connect_view_notice( string $result, string $notice ): void {
+		$_GET[ AgenticCommerceSettings::CONNECT_RESULT ] = $result;
+		try {
+			$this->assertSame( $notice, AgenticCommerceSettings::connect_view()['notice'] );
+		} finally {
+			unset( $_GET[ AgenticCommerceSettings::CONNECT_RESULT ] );
+		}
+	}
+
+	/**
+	 * @testdox Only users who can manage WooCommerce can start a connection.
+	 */
+	public function test_connect_needs_manage_woocommerce(): void {
+		wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
+		$_REQUEST['_wpnonce'] = wp_create_nonce( AgenticCommerceSettings::CONNECT_ACTION );
+		try {
+			$this->sut->handle_connect();
+			$this->fail( 'The connect action stops users who cannot manage WooCommerce' );
+		} catch ( \WPDieException $e ) {
+			$this->assertStringContainsString( 'not allowed to connect', $e->getMessage() );
+		} finally {
+			unset( $_REQUEST['_wpnonce'] );
+		}
+	}
+
+	/**
+	 * @testdox When the site can't register with WordPress.com, connecting returns to the Overview with an error.
+	 */
+	public function test_connect_redirect_returns_with_an_error_when_registration_fails(): void {
+		delete_option( 'jetpack_activation_source' );
+		$this->assertSame(
+			add_query_arg( AgenticCommerceSettings::CONNECT_RESULT, 'error', AgenticCommerceSettings::url() ),
+			AgenticCommerceSettings::connect_redirect()
+		);
+		$this->assertSame( array( 'woocommerce', 'ai-tools' ), get_option( 'jetpack_activation_source' ) );
+	}
+
+	/**
+	 * @testdox An activation source Jetpack already stored is left as it is.
+	 */
+	public function test_connect_redirect_keeps_an_existing_activation_source(): void {
+		update_option( 'jetpack_activation_source', array( 'woocommerce', 'onboarding' ) );
+		AgenticCommerceSettings::connect_redirect();
+		$this->assertSame( array( 'woocommerce', 'onboarding' ), get_option( 'jetpack_activation_source' ) );
+	}
+
+	/**
+	 * @testdox A source that is not a pair is replaced, because Jetpack unpacks the first two items.
+	 */
+	public function test_connect_redirect_replaces_an_activation_source_that_is_not_a_pair(): void {
+		update_option( 'jetpack_activation_source', array( 'woocommerce' ) );
+		AgenticCommerceSettings::connect_redirect();
+		$this->assertSame( array( 'woocommerce', 'ai-tools' ), get_option( 'jetpack_activation_source' ) );
+	}
+
+	/**
+	 * @testdox Only an https WordPress.com URL can be the connection redirect, and its scheme is lowercased.
+	 *
+	 * @testWith ["https://jetpack.wordpress.com/jetpack.authorize/1/?x=1", "https://jetpack.wordpress.com/jetpack.authorize/1/?x=1"]
+	 *           ["HTTPS://jetpack.wordpress.com/path", "https://jetpack.wordpress.com/path"]
+	 *           ["https://wordpress.com/jetpack/connect", "https://wordpress.com/jetpack/connect"]
+	 *           ["http://jetpack.wordpress.com/path", ""]
+	 *           ["https://evil.example/phish", ""]
+	 *           ["https://wordpress.com.evil.example/", ""]
+	 *           ["https://user@jetpack.wordpress.com/path", ""]
+	 *           ["", ""]
+	 *
+	 * @param string $url      Candidate redirect.
+	 * @param string $expected URL that may be followed.
+	 */
+	public function test_wordpress_com_https_url( string $url, string $expected ): void {
+		$this->assertSame( $expected, AgenticCommerceSettings::wordpress_com_https_url( $url ) );
+	}
+
+	/**
+	 * @testdox An authorization URL that leaves WordPress.com sends the merchant back to the Overview with an error.
+	 */
+	public function test_connect_redirect_rejects_an_offsite_authorization_url(): void {
+		delete_option( 'jetpack_activation_source' );
+		$property = new \ReflectionProperty( \Automattic\Jetpack\Connection\Manager::class, 'is_connected' );
+		$property->setAccessible( true );
+		$property->setValue( null, true );
+		add_filter(
+			'jetpack_build_authorize_url',
+			static function () {
+				return 'https://evil.example/phish';
+			}
+		);
+		try {
+			$this->assertSame(
+				add_query_arg( AgenticCommerceSettings::CONNECT_RESULT, 'error', AgenticCommerceSettings::url() ),
+				AgenticCommerceSettings::connect_redirect()
+			);
+		} finally {
+			remove_all_filters( 'jetpack_build_authorize_url' );
+			$property->setValue( null, null );
+		}
+	}
+
 	/**
 	 * @testdox Before the first scan the page asks for one instead of showing counts; dropdowns list published pages only.
 	 */