Commit d544dbf4040 for woocommerce

commit d544dbf40403637d0278147b9310fa9b60bb678e
Author: Sergey Mitroshin <sergeymitr@gmail.com>
Date:   Sun Nov 22 20:54:56 2026 -0500

    UCP: Hand a checkout with nothing to pay to the store (#71300)

    `complete` refused a UCP checkout with nothing to pay (`payment_not_required`, unrecoverable), since the payment page takes no such order. It now returns `requires_escalation` with the cart's `continue_url`, so the buyer places the order on the store.

diff --git a/plugins/woocommerce/changelog/add-ucp-payless-order-handoff b/plugins/woocommerce/changelog/add-ucp-payless-order-handoff
new file mode 100644
index 00000000000..785b49b6764
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-ucp-payless-order-handoff
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Hand a UCP checkout with nothing to pay to the store with its cart instead of refusing it; unreleased routes.
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsComplete.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsComplete.php
index 1477ca737ae..685be7dc82b 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsComplete.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/CheckoutSessionsComplete.php
@@ -7,6 +7,7 @@ use Automattic\WooCommerce\Checkout\Helpers\ReserveStockException;
 use Automattic\WooCommerce\Enums\OrderStatus;
 use Automattic\WooCommerce\Internal\Orders\OrderAttributionController;
 use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
+use Automattic\WooCommerce\StoreApi\Utilities\Ucp\CartMapper;
 use Automattic\WooCommerce\StoreApi\Utilities\Ucp\CheckoutMapper;
 use Automattic\WooCommerce\StoreApi\Utilities\Ucp\Utils;

@@ -62,7 +63,7 @@ class CheckoutSessionsComplete extends AbstractCheckoutRoute {
 	 * order, the Store API draft order of this session, and `continue_url` is its payment page.
 	 * Stock is held for the order first, as the Store API checkout does, because the payment
 	 * page trusts a pending order's stock to be held and checks nothing itself.
-	 * A cart with nothing to pay is refused: these routes complete no order, and the payment page takes no such order.
+	 * A cart with nothing to pay makes no order, which the payment page would not take: `continue_url` is the cart's own, and the buyer places the order on the store.
 	 * A checkout that is not ready yet is returned as it is, with messages naming what is missing.
 	 * So is one already handed off and unchanged since, with a warning: its order is not written to again.
 	 *
@@ -112,15 +113,26 @@ class CheckoutSessionsComplete extends AbstractCheckoutRoute {
 			return $this->checkout_response( $checkout_id, $this->checkout_state() ?? $state, 200, $cart );
 		}

-		if ( ! $this->cart_controller->get_cart_instance()->needs_payment() ) {
-			return $this->ucp_error( 'payment_not_required', __( 'This checkout has nothing to pay, so it cannot be handed off for payment. It must be made on the store itself.', 'woocommerce' ), 'unrecoverable', 200 );
-		}
-
 		// Handed off already and unchanged. The buyer may be paying this order now, so it is not written to again.
 		if ( ! empty( $state['order_id'] ) ) {
 			return $this->already_handed_off_response( $checkout_id, $cart );
 		}

+		// Below the return above, so the store is never handed a cart whose order is still payable.
+		if ( ! $this->cart_controller->get_cart_instance()->needs_payment() ) {
+			$state = array_merge(
+				$state,
+				array(
+					'status'       => CheckoutMapper::STATUS_REQUIRES_ESCALATION,
+					'continue_url' => ( new CartMapper() )->continue_url( $checkout_id ),
+					'fingerprint'  => $fingerprint,
+				)
+			);
+			$this->save_checkout_state( $state );
+
+			return $this->checkout_response( $checkout_id, $state, 200, $cart );
+		}
+
 		if ( $order ) {
 			$this->order_controller->update_order_from_cart( $order );
 		} else {
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php
index 9c677873ac6..f9b76bf2f85 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CartMapper.php
@@ -230,7 +230,7 @@ class CartMapper {
 	 * @param string $cart_id Cart identifier.
 	 * @return string
 	 */
-	private function continue_url( string $cart_id ): string {
+	public function continue_url( string $cart_id ): string {
 		$continue_url = add_query_arg( 'session', $cart_id, wc_get_cart_url() );

 		/**
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php
index 0138ed92546..564c67c0d48 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/Ucp/CheckoutMapper.php
@@ -22,7 +22,7 @@ class CheckoutMapper {
 	 *
 	 * @param array                $cart Store API cart response, as nested arrays.
 	 * @param string               $checkout_id Checkout identifier (a Store API cart token).
-	 * @param array<string, mixed> $state Stored `status` and `continue_url`, and the `order` once paid.
+	 * @param array<string, mixed> $state Stored `status` and `continue_url`, the `order_id` of an order handed off for payment, and the `order` once paid.
 	 * @param array<string, mixed> $context Optional `messages` and `discount_codes`, as {@see CartMapper::build_cart()} takes them.
 	 * @return array
 	 */
@@ -33,7 +33,7 @@ class CheckoutMapper {
 		$missing  = $placed ? array() : $this->missing_requirements( $cart );
 		$messages = array_merge( $payload['messages'] ?? array(), $missing );

-		// A checkout carries its own `continue_url`, the payment page, only at escalation.
+		// A checkout carries a `continue_url` only at escalation: the payment page, or the cart's own when nothing is to pay.
 		unset( $payload['continue_url'] );

 		if ( '' === $status ) {
@@ -53,7 +53,10 @@ class CheckoutMapper {
 			$messages[]              = array(
 				'type'         => 'error',
 				'code'         => 'ESCALATION_REQUIRED',
-				'content'      => __( 'Payment must be completed by the buyer at continue_url.', 'woocommerce' ),
+				// Without an order the hand-off is the cart alone: the store checkout asks the buyer's details again.
+				'content'      => empty( $state['order_id'] )
+					? __( 'There is nothing to pay, so the buyer must place the order on the store, starting from the cart at continue_url.', 'woocommerce' )
+					: __( 'Payment must be completed by the buyer at continue_url.', 'woocommerce' ),
 				'severity'     => 'requires_buyer_review',
 				'path'         => '$',
 				'content_type' => 'plain',
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 54e9d68904d..93b68b38505 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCheckout.php
@@ -682,6 +682,7 @@ class UcpCheckout extends ControllerTestCase {
 		$this->assertSame( 200, $response->get_status() );
 		$this->assertSame( 'requires_escalation', $data['status'] );
 		$this->assertContains( 'ESCALATION_REQUIRED', array_column( $data['messages'], 'code' ) );
+		$this->assertContains( 'Payment must be completed by the buyer at continue_url.', array_column( $data['messages'], 'content' ) );

 		$order = wc_get_order( (int) WC()->session->get( 'store_api_draft_order' ) );
 		$this->assertInstanceOf( \WC_Order::class, $order );
@@ -703,9 +704,9 @@ class UcpCheckout extends ControllerTestCase {
 	}

 	/**
-	 * @testdox Complete refuses a checkout with nothing to pay, which the payment page would not take, and makes no order.
+	 * @testdox Complete hands a checkout with nothing to pay to the store with its cart, makes no order, and a later request reads the hand-off back.
 	 */
-	public function test_complete_refuses_a_free_checkout(): void {
+	public function test_complete_hands_a_free_checkout_to_the_store(): void {
 		$product = \WC_Helper_Product::create_simple_product(
 			true,
 			array(
@@ -722,8 +723,15 @@ class UcpCheckout extends ControllerTestCase {

 		$data = $this->request( 'POST', '/checkout-sessions/' . $created['id'] . '/complete' )->get_data();

-		$this->assertSame( 'payment_not_required', $data['messages'][0]['code'] );
+		$this->assertSame( 'requires_escalation', $data['status'] );
+		$this->assertSame( add_query_arg( 'session', $created['id'], wc_get_cart_url() ), $data['continue_url'] );
+		$this->assertStringNotContainsString( 'Payment must be completed', implode( ' ', array_column( $data['messages'], 'content' ) ) );
 		$this->assertEmpty( WC()->session->get( 'store_api_draft_order' ) );
+
+		$this->simulate_new_request( $created['id'] );
+		$read_back = $this->request( 'GET', '/checkout-sessions/' . $created['id'] )->get_data();
+		$this->assertSame( 'requires_escalation', $read_back['status'] );
+		$this->assertSame( $data['continue_url'], $read_back['continue_url'] );
 	}

 	/**