Commit 37df5ccc73c for woocommerce
commit 37df5ccc73ceecda0af0e098ef7302b041aa7f34
Author: Sergey Mitroshin <sergeymitr@gmail.com>
Date: Sat Nov 28 21:23:34 2026 -0500
UCP: Use a cart link token in the cart continue_url (#71572)
Switch UCP cart handoff UR to the new cart link token.
diff --git a/plugins/woocommerce/changelog/update-ucp-cart-only-tokens b/plugins/woocommerce/changelog/update-ucp-cart-only-tokens
new file mode 100644
index 00000000000..a3314c510c6
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-ucp-cart-only-tokens
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Put a cart link token, not the cart id, in the UCP cart continue_url; unreleased routes.
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/Carts.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/Carts.php
index 3725360e3ef..e39f22e6974 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/Carts.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/Carts.php
@@ -64,8 +64,8 @@ class Carts extends AbstractCartRoute {
* Create a cart.
*
* The id is minted from the session the cart was loaded into, so it is the token
- * that addresses this cart from now on — and, inside `continue_url`, the token
- * that lets the buyer's browser pick the same cart up.
+ * that addresses this cart from now on. `continue_url` carries a link token for the
+ * same session instead, which only lets the buyer's browser copy the cart.
*
* @param \WP_REST_Request $request Request object.
*
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php
index f9b76bf2f85..9ff97cbb0b3 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php
@@ -224,14 +224,19 @@ class CartMapper {
/**
* Build the URL that hands the cart to the buyer's own browser.
*
- * `WC_Session_Handler` restores or clones the guest session named by the `session`
- * argument, so the shopper lands on checkout holding this cart.
+ * `WC_Session_Handler` clones the cart of the guest session named by the `session`
+ * argument. That is a link token, never the cart id: the URL is shared, and the id is a credential.
*
* @param string $cart_id Cart identifier.
* @return string
*/
public function continue_url( string $cart_id ): string {
- $continue_url = add_query_arg( 'session', $cart_id, wc_get_cart_url() );
+ $customer_id = (string) CartTokenUtils::get_cart_token_payload( $cart_id )['user_id'];
+ $continue_url = wc_get_cart_url();
+
+ if ( '' !== $customer_id ) {
+ $continue_url = add_query_arg( 'session', CartTokenUtils::get_cart_link_token( $customer_id ), $continue_url );
+ }
/**
* Filters the URL a UCP cart hands the buyer for completing the purchase.
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCart.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCart.php
index 7c89412b952..32dfa41ac52 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCart.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCart.php
@@ -429,17 +429,20 @@ class UcpCart extends UcpCartTestCase {
}
/**
- * The cart hands the buyer a URL that restores the same cart in a browser.
+ * @testdox The continue URL names the cart's session with a link token, which is not a cart token.
*/
- public function test_continue_url_carries_the_cart_token(): void {
+ public function test_continue_url_carries_a_link_token_not_the_cart_token(): void {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'customer' ) ) );
$data = $this->create_cart( array( $this->line_item( 0, 1 ) ) );
$query = array();
wp_parse_str( (string) wp_parse_url( $data['continue_url'], PHP_URL_QUERY ), $query );
- // WC_Session_Handler restores or clones the guest session named by this token.
- $this->assertSame( $data['id'], $query['session'] );
+ $this->assertSame(
+ CartTokenUtils::get_cart_token_payload( $data['id'] )['user_id'],
+ CartTokenUtils::get_cart_link_customer_id( $query['session'] )
+ );
+ $this->assertFalse( CartTokenUtils::validate_cart_token( $query['session'] ) );
$this->assertStringStartsWith( wc_get_cart_url(), $data['continue_url'] );
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
index 14d5fe3f081..596a0c39d85 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
@@ -724,7 +724,8 @@ class UcpCheckout extends ControllerTestCase {
$data = $this->request( 'POST', '/checkout-sessions/' . $created['id'] . '/complete' )->get_data();
$this->assertSame( 'requires_escalation', $data['status'] );
- $this->assertSame( add_query_arg( 'session', $created['id'], wc_get_cart_url() ), $data['continue_url'] );
+ $this->assertStringStartsWith( add_query_arg( 'session', '', wc_get_cart_url() ), $data['continue_url'] );
+ $this->assertStringNotContainsString( $created['id'], $data['continue_url'] );
$this->assertStringNotContainsString( 'Payment must be completed', implode( ' ', array_column( $data['messages'], 'content' ) ) );
$this->assertEmpty( WC()->session->get( 'store_api_draft_order' ) );