Commit 29ed4d3b336 for woocommerce

commit 29ed4d3b336bfb2b28f473318140ff55d5cf38db
Author: Darren Ethier <darren@roughsmootheng.in>
Date:   Thu Dec 3 10:42:22 2026 -0500

    Stop classic cart links from chaining into endless unique URLs (#70948)

    * Stop classic cart links from chaining into endless unique URLs

    Loop add to cart links on simple products now build on the product
    permalink instead of the current request, so they no longer carry
    remove_item and _wpnonce from the page they render on.

    Remove and undo requests now always redirect after they are handled,
    falling back to the cart page when there is no referer. Remove or undo
    links with an invalid nonce are redirected to a clean URL when the
    request is for the cart page.

    Remove and undo links get rel="nofollow".

    * Add changelog entry for classic cart crawlable URL fix

    * Use a literal expected redirect in the referer removal test

    * Return to the requested page after a remove or undo without a referer

    Remove and undo links point at the page they are shown on, which can be a
    cart shortcode page other than the configured cart page. Falling back to
    wc_get_cart_url() sent those shoppers to a different page, and the classic
    cart script then swapped in markup it could not use, leaving the cart blank.

    * Assert the added-to-cart notice in the non-AJAX Product Button E2E test

    Without AJAX, the button now leads to the product page, so the old locator for the shop button's own link no longer applies.

    * Build the undo test request from a local array instead of reading $_GET

    * Normalise the requested path for cart item link redirects; cover invalid undo links

    Both redirect paths now read the request through one helper that starts the
    path with a single slash, so a request path like //example.net/cart/ stays on
    the site instead of falling back to the admin URL. Tests now cover an invalid
    undo link and restore $_SERVER after each test.

    * Test that add_to_cart_url() ignores the current request URI

    * Keep the contributed add_to_cart_url() test and drop the duplicate one

    * Only flag a removal in the redirect when an item was removed, and drop the flag after undo

    * Assert the full add to cart URL in the product permalink test

    * Assert the added-to-cart notice in the moved non-AJAX Product Button E2E test

    ---------

    Co-authored-by: shsajalchowdhury <sajal.marketer@gmail.com>
    Co-authored-by: Jorge Torres <jorge.torres@automattic.com>

diff --git a/plugins/woocommerce/changelog/fix-WOOPLUG-8235-classic-cart-crawlable-urls b/plugins/woocommerce/changelog/fix-WOOPLUG-8235-classic-cart-crawlable-urls
new file mode 100644
index 00000000000..8ef2739a6a2
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-WOOPLUG-8235-classic-cart-crawlable-urls
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Stop crawlers looping through endless unique classic cart URLs; product list add to cart links now point to the product page.
diff --git a/plugins/woocommerce/includes/class-wc-form-handler.php b/plugins/woocommerce/includes/class-wc-form-handler.php
index 0bf5fa9419f..671108aff87 100644
--- a/plugins/woocommerce/includes/class-wc-form-handler.php
+++ b/plugins/woocommerce/includes/class-wc-form-handler.php
@@ -1056,6 +1056,7 @@ class WC_Form_Handler {
 		} elseif ( ! empty( $_GET['remove_item'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {
 			$cart_item_key = sanitize_text_field( wp_unslash( $_GET['remove_item'] ) );
 			$cart_item     = WC()->cart->get_cart_item( $cart_item_key );
+			$redirect_url  = remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), self::get_cart_action_redirect_base() );

 			if ( $cart_item ) {
 				$removed = WC()->cart->remove_cart_item( $cart_item_key );
@@ -1081,19 +1082,19 @@ class WC_Form_Handler {
 				if ( $product && $product->is_in_stock() && $product->has_enough_stock( $cart_item['quantity'] ) ) {
 					/* Translators: %s Product title. */
 					$removed_notice  = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
-					$removed_notice .= ' <a href="' . esc_url( wc_get_cart_undo_url( $cart_item_key ) ) . '" class="restore-item">' . __( 'Undo?', 'woocommerce' ) . '</a>';
+					$removed_notice .= ' <a href="' . esc_url( wc_get_cart_undo_url( $cart_item_key ) ) . '" class="restore-item" rel="nofollow">' . __( 'Undo?', 'woocommerce' ) . '</a>';
 				} else {
 					/* Translators: %s Product title. */
 					$removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
 				}

 				wc_add_notice( $removed_notice, apply_filters( 'woocommerce_cart_item_removed_notice_type', 'success' ) );
+				$redirect_url = add_query_arg( 'removed_item', '1', $redirect_url );
 			}

-			if ( wp_get_referer() ) {
-				wp_safe_redirect( remove_query_arg( array( 'remove_item', 'add-to-cart', 'added-to-cart', 'order_again', '_wpnonce' ), add_query_arg( 'removed_item', '1', wp_get_referer() ) ) );
-				exit;
-			}
+			// Always redirect, so the action URL is never the page that renders (and never the base for links on it).
+			wp_safe_redirect( $redirect_url );
+			exit;
 		} elseif ( ! empty( $_GET['undo_item'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {

 			// Undo Cart Item.
@@ -1101,10 +1102,11 @@ class WC_Form_Handler {

 			WC()->cart->restore_cart_item( $cart_item_key );

-			if ( wp_get_referer() ) {
-				wp_safe_redirect( remove_query_arg( array( 'undo_item', '_wpnonce' ), wp_get_referer() ) );
-				exit;
-			}
+			wp_safe_redirect( remove_query_arg( array( 'undo_item', 'removed_item', '_wpnonce' ), self::get_cart_action_redirect_base() ) );
+			exit;
+		} elseif ( ! empty( $_GET['remove_item'] ) || ! empty( $_GET['undo_item'] ) ) {
+			// The nonce is invalid or expired. Other code may use these query args, so only redirect once the request is known to be for the cart page.
+			add_action( 'template_redirect', array( __CLASS__, 'redirect_invalid_cart_item_link' ) );
 		}

 		// Update Cart - checks apply_coupon too because they are in the same form.
@@ -1181,6 +1183,55 @@ class WC_Form_Handler {
 		}
 	}

+	/**
+	 * Get the URL to return to after a cart item is removed or restored: the referring page, or the requested page when there is no referer.
+	 *
+	 * Callers strip the action arguments from it. The requested page can be a cart shortcode page other than the configured cart page.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @return string
+	 */
+	private static function get_cart_action_redirect_base(): string {
+		$referer = wp_get_referer();
+
+		return $referer ? $referer : self::get_requested_path();
+	}
+
+	/**
+	 * Get the requested path and query string, starting with a single slash.
+	 *
+	 * A request path such as //example.com/cart/ would otherwise read as a URL on another host. wp_safe_redirect() would reject it, but send the shopper to the admin URL instead of the page they asked for.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @return string
+	 */
+	private static function get_requested_path(): string {
+		return '/' . ltrim( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ), '/\\' );
+	}
+
+	/**
+	 * Redirect a cart page request that carries a remove or undo link with an invalid or expired nonce to the same URL without the link's arguments.
+	 *
+	 * Hooked to template_redirect by update_cart_action() only when it rejects such a link.
+	 *
+	 * @internal
+	 *
+	 * @since 11.4.0
+	 */
+	public static function redirect_invalid_cart_item_link(): void {
+		$cart_page_id = wc_get_page_id( 'cart' );
+
+		// wc_get_page_id() returns -1 when no cart page is set.
+		if ( $cart_page_id < 1 || ! is_page( $cart_page_id ) ) {
+			return;
+		}
+
+		wp_safe_redirect( remove_query_arg( array( 'remove_item', 'undo_item', '_wpnonce', 'add-to-cart', 'added-to-cart' ), self::get_requested_path() ) );
+		exit;
+	}
+
 	/**
 	 * Place a previous order again.
 	 *
diff --git a/plugins/woocommerce/includes/class-wc-product-simple.php b/plugins/woocommerce/includes/class-wc-product-simple.php
index 26601a4d490..f28773f3279 100644
--- a/plugins/woocommerce/includes/class-wc-product-simple.php
+++ b/plugins/woocommerce/includes/class-wc-product-simple.php
@@ -47,13 +47,7 @@ class WC_Product_Simple extends WC_Product {
 			'quantity'    => $quantity > 1 ? $quantity : false,
 		);

-		$url = $this->is_purchasable() && $this->is_in_stock() ? remove_query_arg(
-			'added-to-cart',
-			add_query_arg(
-				$query_args,
-				( function_exists( 'is_feed' ) && is_feed() ) || ( function_exists( 'is_404' ) && is_404() ) ? $this->get_permalink() : false
-			)
-		) : $this->get_permalink();
+		$url = $this->is_purchasable() && $this->is_in_stock() ? add_query_arg( $query_args, $this->get_permalink() ) : $this->get_permalink();
 		return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
 	}

diff --git a/plugins/woocommerce/templates/cart/cart.php b/plugins/woocommerce/templates/cart/cart.php
index c80c1fd4341..2d8642a8cfb 100644
--- a/plugins/woocommerce/templates/cart/cart.php
+++ b/plugins/woocommerce/templates/cart/cart.php
@@ -12,7 +12,7 @@
  *
  * @see     https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates
- * @version 11.3.0
+ * @version 11.4.0
  */

 defined( 'ABSPATH' ) || exit;
@@ -72,7 +72,7 @@ do_action( 'woocommerce_before_cart' ); ?>
 								echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
 									'woocommerce_cart_item_remove_link',
 									sprintf(
-										'<a role="button" href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
+										'<a role="button" href="%s" class="remove" rel="nofollow" aria-label="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
 										esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
 										/* translators: %s is the product name */
 										esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), wp_strip_all_tags( $product_name ) ) ),
diff --git a/plugins/woocommerce/templates/cart/mini-cart.php b/plugins/woocommerce/templates/cart/mini-cart.php
index c218ff71b2e..73649a1b08f 100644
--- a/plugins/woocommerce/templates/cart/mini-cart.php
+++ b/plugins/woocommerce/templates/cart/mini-cart.php
@@ -14,7 +14,7 @@
  *
  * @see     https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates
- * @version 11.2.2
+ * @version 11.4.0
  */

 defined( 'ABSPATH' ) || exit;
@@ -58,7 +58,7 @@ do_action( 'woocommerce_before_mini_cart' ); ?>
 					echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
 						'woocommerce_cart_item_remove_link',
 						sprintf(
-							'<a role="button" href="%s" class="remove remove_from_cart_button" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s" data-success_message="%s">&times;</a>',
+							'<a role="button" href="%s" class="remove remove_from_cart_button" rel="nofollow" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s" data-success_message="%s">&times;</a>',
 							esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
 							/* translators: %s is the product name */
 							esc_attr( sprintf( __( 'Remove %s from cart', 'woocommerce' ), wp_strip_all_tags( $product_name ) ) ),
diff --git a/plugins/woocommerce/tests/e2e/tests/blocks/product-button/product-button-ajax-disabled.block_theme.spec.ts b/plugins/woocommerce/tests/e2e/tests/blocks/product-button/product-button-ajax-disabled.block_theme.spec.ts
index fc98c9f341e..488002f5061 100644
--- a/plugins/woocommerce/tests/e2e/tests/blocks/product-button/product-button-ajax-disabled.block_theme.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/blocks/product-button/product-button-ajax-disabled.block_theme.spec.ts
@@ -33,8 +33,9 @@ test.describe( `${ blockData.name } Block with AJAX add to cart disabled`, () =>

 		await block.click();

+		// Without AJAX, the button links to the product page, which adds the product.
 		await expect(
-			page.locator( `a[href*="cart=${ productId }"]` )
+			page.getByText( `“${ productName }” has been added to your cart.` )
 		).toBeVisible();

 		await frontendUtils.goToCheckout();
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/product/product-simple.php b/plugins/woocommerce/tests/legacy/unit-tests/product/product-simple.php
index ce80517278d..829f1a2cb4b 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/product/product-simple.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/product/product-simple.php
@@ -168,6 +168,27 @@ class WC_Tests_Product_Simple extends WC_Unit_Test_Case {
 		$this->assertFalse( $this->product->backorders_allowed() );
 	}

+	/**
+	 * Test add_to_cart_url() always returns product permalink based URL.
+	 */
+	public function test_add_to_cart_url_returns_product_permalink() {
+		// The test suite runs with plain permalinks.
+		$expected = home_url( '/?product=' . $this->product->get_slug() . '&add-to-cart=' . $this->product->get_id() );
+
+		$this->assertSame( $expected, $this->product->add_to_cart_url() );
+
+		$original_request_uri   = $_SERVER['REQUEST_URI'] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Saved only to restore it below.
+		$_SERVER['REQUEST_URI'] = '/shop/?utm_source=google&gclid=123';
+		$url                    = $this->product->add_to_cart_url();
+		if ( null === $original_request_uri ) {
+			unset( $_SERVER['REQUEST_URI'] );
+		} else {
+			$_SERVER['REQUEST_URI'] = $original_request_uri;
+		}
+
+		$this->assertSame( $expected, $url, 'The current request must not affect the URL.' );
+	}
+
 	/**
 	 * Test backorders_require_notification().
 	 *
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index 9fe1d202b9b..88d83d386fb 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -2813,7 +2813,16 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {

 		add_action( 'internal_woocommerce_cart_item_removed_from_user_request', $callback, 10, 2 );

-		WC_Form_Handler::update_cart_action();
+		$intercept_redirect = static function () {
+			throw new RuntimeException( 'redirect' );
+		};
+		add_filter( 'wp_redirect', $intercept_redirect );
+		try {
+			WC_Form_Handler::update_cart_action();
+		} catch ( RuntimeException $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- The handler redirects and exits after removing; the intercept stops it there.
+		} finally {
+			remove_filter( 'wp_redirect', $intercept_redirect );
+		}

 		$this->assertNotEmpty( $captured_args, 'The remove item action should have been fired' );
 		$this->assertSame( $cart_item_key, $captured_args['cart_item_key'] );
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
index 89c3a53b9ff..65d20bd6a86 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
@@ -37,6 +37,13 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
 	 */
 	private array $original_request = array();

+	/**
+	 * Original SERVER data.
+	 *
+	 * @var array<string,mixed>
+	 */
+	private array $original_server = array();
+
 	/**
 	 * Original WooCommerce session.
 	 *
@@ -60,6 +67,7 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
 		$this->original_get     = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
 		$this->original_post    = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
 		$this->original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+		$this->original_server  = $_SERVER;
 		$this->original_session = WC()->session;

 		if ( ! WC()->session ) {
@@ -79,6 +87,7 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
 		$_GET     = $this->original_get;
 		$_POST    = $this->original_post;
 		$_REQUEST = $this->original_request;
+		$_SERVER  = $this->original_server;

 		wp_set_current_user( 0 );
 		WC()->session = $this->original_session;
@@ -1383,6 +1392,177 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
 		$this->assertSame( $expected, WC_Form_Handler::remove_add_to_cart_from_canonical_redirect( $redirect_url, $requested_url ) );
 	}

+	/**
+	 * The request is for a cart shortcode page other than the configured cart page, as remove links point at the page they are shown on.
+	 *
+	 * @testdox update_cart_action() removes the item and redirects to a clean URL, with or without a referer ("$referer_path", "$request_path").
+	 *
+	 * @testWith ["", "/other-cart/", "/other-cart/?removed_item=1"]
+	 *           ["/shop/?remove_item=stale&add-to-cart=12", "/other-cart/", "/shop/?removed_item=1"]
+	 *           ["", "//example.net/other-cart/", "/example.net/other-cart/?removed_item=1"]
+	 *
+	 * @covers WC_Form_Handler::update_cart_action()
+	 *
+	 * @param string $referer_path  Referer path, or an empty string for no referer.
+	 * @param string $request_path  Path of the remove link that was followed.
+	 * @param string $expected_path Expected redirect path.
+	 */
+	public function test_update_cart_action_redirects_after_removing_item( string $referer_path, string $request_path, string $expected_path ): void {
+		$this->create_cart_page();
+		$product       = WC_Helper_Product::create_simple_product();
+		$cart_item_key = WC()->cart->add_to_cart( $product->get_id() );
+		$nonce         = wp_create_nonce( 'woocommerce-cart' );
+
+		unset( $_SERVER['HTTP_REFERER'], $_REQUEST['_wp_http_referer'] );
+		if ( '' !== $referer_path ) {
+			$_SERVER['HTTP_REFERER'] = home_url( $referer_path );
+		}
+		$_SERVER['REQUEST_URI'] = "{$request_path}?remove_item={$cart_item_key}&_wpnonce={$nonce}";
+		$_GET                   = array( 'remove_item' => $cart_item_key );
+		$_REQUEST               = array(
+			'remove_item' => $cart_item_key,
+			'_wpnonce'    => $nonce,
+		);
+
+		$redirect = $this->dispatch_update_cart_action_expecting_redirect();
+
+		// A referer is absolute, while the requested URI is relative to the host.
+		$expected = '' === $referer_path ? $expected_path : home_url( $expected_path );
+		$this->assertSame( $expected, $redirect, 'Removing an item should redirect to a URL without the remove link arguments.' );
+		$this->assertEmpty( WC()->cart->get_cart_item( $cart_item_key ), 'The item should be removed from the cart.' );
+		$this->assertStringContainsString( 'class="restore-item" rel="nofollow"', wc_get_notices( 'success' )[0]['notice'] ?? '', 'The undo link should tell crawlers not to follow it.' );
+	}
+
+	/**
+	 * @testdox update_cart_action() redirects a remove link for an item that is no longer in the cart without flagging a removal.
+	 *
+	 * @covers WC_Form_Handler::update_cart_action()
+	 */
+	public function test_update_cart_action_redirects_stale_remove_link_without_removed_flag(): void {
+		$this->create_cart_page();
+		$nonce = wp_create_nonce( 'woocommerce-cart' );
+
+		unset( $_SERVER['HTTP_REFERER'], $_REQUEST['_wp_http_referer'] );
+		$_SERVER['REQUEST_URI'] = "/other-cart/?remove_item=unknown&_wpnonce={$nonce}";
+		$_GET                   = array( 'remove_item' => 'unknown' );
+		$_REQUEST               = array(
+			'remove_item' => 'unknown',
+			'_wpnonce'    => $nonce,
+		);
+
+		$redirect = $this->dispatch_update_cart_action_expecting_redirect();
+
+		$this->assertSame( '/other-cart/', $redirect, 'A remove link for an unknown item should redirect to the page without the removed_item flag.' );
+		$this->assertSame( 0, wc_notice_count(), 'No notice should be queued when nothing was removed.' );
+	}
+
+	/**
+	 * @testdox update_cart_action() restores the item and redirects to a clean URL, with or without a referer ("$referer_path", "$expected_path").
+	 *
+	 * @testWith ["", "/other-cart/"]
+	 *           ["/cart/?removed_item=1", "/cart/"]
+	 *
+	 * @covers WC_Form_Handler::update_cart_action()
+	 *
+	 * @param string $referer_path  Referer path, or an empty string for no referer.
+	 * @param string $expected_path Expected redirect path.
+	 */
+	public function test_update_cart_action_redirects_after_undo( string $referer_path, string $expected_path ): void {
+		$this->create_cart_page();
+		$product       = WC_Helper_Product::create_simple_product();
+		$cart_item_key = WC()->cart->add_to_cart( $product->get_id() );
+		WC()->cart->remove_cart_item( $cart_item_key );
+
+		unset( $_SERVER['HTTP_REFERER'], $_REQUEST['_wp_http_referer'] );
+		if ( '' !== $referer_path ) {
+			$_SERVER['HTTP_REFERER'] = home_url( $referer_path );
+		}
+		$query                  = array(
+			'undo_item' => $cart_item_key,
+			'_wpnonce'  => wp_create_nonce( 'woocommerce-cart' ),
+		);
+		$_GET                   = $query;
+		$_REQUEST               = $query;
+		$_SERVER['REQUEST_URI'] = '/other-cart/?' . http_build_query( $query );
+
+		$redirect = $this->dispatch_update_cart_action_expecting_redirect();
+
+		// A referer is absolute, while the requested URI is relative to the host.
+		$expected = '' === $referer_path ? $expected_path : home_url( $expected_path );
+		$this->assertSame( $expected, $redirect, 'Undoing a removal should redirect to a URL without the undo link arguments or the removed_item flag.' );
+		$this->assertNotEmpty( WC()->cart->get_cart_item( $cart_item_key ), 'The item should be back in the cart.' );
+	}
+
+	/**
+	 * @testdox A $link_arg link with an invalid nonce is redirected to a clean URL on the cart page ($on_cart_page) and left alone elsewhere.
+	 *
+	 * @testWith ["remove_item", true]
+	 *           ["remove_item", false]
+	 *           ["undo_item", true]
+	 *
+	 * @covers WC_Form_Handler::update_cart_action()
+	 * @covers WC_Form_Handler::redirect_invalid_cart_item_link()
+	 *
+	 * @param string $link_arg     Query argument of the cart item link: remove_item or undo_item.
+	 * @param bool   $on_cart_page Whether the request is for the cart page.
+	 */
+	public function test_invalid_cart_item_link_is_redirected_only_on_cart_page( string $link_arg, bool $on_cart_page ): void {
+		$cart_page_url = $this->create_cart_page();
+		$page_url      = $on_cart_page ? $cart_page_url : get_permalink( self::factory()->post->create( array( 'post_type' => 'page' ) ) );
+
+		$this->go_to(
+			add_query_arg(
+				array(
+					$link_arg     => 'stale',
+					'_wpnonce'    => 'invalid',
+					'add-to-cart' => '12',
+				),
+				$page_url
+			)
+		);
+		$_REQUEST = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Mirrors PHP building the request from the query string.
+
+		// Only the handler under test should react to template_redirect; the hook snapshot is restored on teardown.
+		remove_all_actions( 'template_redirect' );
+		WC_Form_Handler::update_cart_action();
+
+		$redirect = null;
+		try {
+			do_action( 'template_redirect' );
+		} catch ( RuntimeException $e ) {
+			$redirect = $e->getMessage();
+		}
+
+		$this->assertSame( $on_cart_page ? wp_make_link_relative( $cart_page_url ) : null, $redirect, 'Only cart page requests should be redirected to a URL without the link arguments.' );
+	}
+
+	/**
+	 * Creates a page and sets it as the cart page.
+	 *
+	 * @return string Cart page URL.
+	 */
+	private function create_cart_page(): string {
+		$page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
+		update_option( 'woocommerce_cart_page_id', $page_id );
+
+		return (string) get_permalink( $page_id );
+	}
+
+	/**
+	 * Runs update_cart_action() and returns the URL it redirects to.
+	 *
+	 * @return string Redirect URL.
+	 */
+	private function dispatch_update_cart_action_expecting_redirect(): string {
+		try {
+			WC_Form_Handler::update_cart_action();
+		} catch ( RuntimeException $e ) {
+			return $e->getMessage();
+		}
+
+		$this->fail( 'Expected update_cart_action() to redirect.' );
+	}
+
 	/**
 	 * @testdox Payment authorization rejects other customers before any payment side effects.
 	 * @testWith ["owner", true]