Commit 5567874858b for woocommerce

commit 5567874858b5df7431594a99ba5ff1cbb55bc6fc
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 12:16:17 2026 +0100

    Prevent Store API order reuse after a destination change (#71260)

    * Prevent Store API order reuse after a destination change

    Forward-port of Automattic/woocommerce#520 before the #584 follow-up was merged into it.

    * Test placed order reuse through GET /checkout now that cart routes no longer sync

    ---------

    Co-authored-by: Justin Palmer <228780+layoutd@users.noreply.github.com>

diff --git a/plugins/woocommerce/src/StoreApi/Utilities/DraftOrderTrait.php b/plugins/woocommerce/src/StoreApi/Utilities/DraftOrderTrait.php
index df63c70e73d..406ec8d150a 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/DraftOrderTrait.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/DraftOrderTrait.php
@@ -60,8 +60,9 @@ trait DraftOrderTrait {
 	}

 	/**
-	 * Whether the passed argument is a draft order or an order that is
-	 * pending/failed and the cart hasn't changed.
+	 * Whether the argument can serve as the session's draft order: a checkout-draft, or a pending/failed order
+	 * the shopper can still pay as placed (its cart hash matches, a priced order that needs shipping still
+	 * ships to the customer's destination, and its tax location has not moved).
 	 *
 	 * @param \WC_Order $order_object Order object to check.
 	 * @return boolean Whether the order is valid as a draft order.
@@ -76,19 +77,195 @@ trait DraftOrderTrait {
 			return true;
 		}

-		// Pending and failed orders can be retried if the cart hasn't changed.
-		if ( $order_object->needs_payment() && $order_object->has_cart_hash( wc()->cart->get_cart_hash() ) ) {
+		// Pending and failed orders can be retried if the cart hasn't changed. The cart hash covers the cart
+		// lines and total only, not the shipping destination the order's shipping lines were rated for nor the
+		// location its tax is worked out for, so both are checked separately to stop a placed order being
+		// re-addressed without going through checkout again. As on the pay-for-order page, an order that has no
+		// address yet and did not come from the Store API may take its first one.
+		if (
+			$order_object->needs_payment()
+			&& $order_object->has_cart_hash( wc()->cart->get_cart_hash() )
+			&& (
+				$this->order_awaits_first_address( $order_object )
+				|| ( $this->order_ships_to_customer_destination( $order_object ) && $this->order_taxed_at_customer_location( $order_object ) )
+			)
+		) {
 			return true;
 		}

-		// needs_payment() is always false for a zero total, so zero-total orders are checked separately.
-		if ( $this->is_zero_total_order_awaiting_payment( $order_object ) && $order_object->has_cart_hash( wc()->cart->get_cart_hash() ) ) {
+		// needs_payment() is false for a zero total by default, so those orders are checked separately. No destination
+		// or tax-location check here: pay-for-order refuses them (it needs needs_payment()), and checkout recalculates
+		// the order it reuses, so a renewal order stays in place when the address changes at checkout.
+		if (
+			! $order_object->needs_payment()
+			&& $this->is_zero_total_order_awaiting_payment( $order_object )
+			&& $order_object->has_cart_hash( wc()->cart->get_cart_hash() )
+		) {
 			return true;
 		}

 		return false;
 	}

+	/**
+	 * Whether the order's shipping lines are still rated for the session customer's destination, which the cart
+	 * hash does not cover. Compared through the pay-for-order guard's helper, reading a state the session filled
+	 * in by default as empty; the taxable location is checked by order_taxed_at_customer_location().
+	 *
+	 * @since 11.3.1
+	 *
+	 * @param \WC_Order $order_object Order object to check.
+	 * @return boolean True when the destinations match or the cart needs no shipping; false without a customer,
+	 *                 or when only one of the order and the customer has a shipping country.
+	 */
+	protected function order_ships_to_customer_destination( \WC_Order $order_object ) {
+		$customer = wc()->customer;
+
+		if ( ! $customer ) {
+			return false;
+		}
+
+		if ( ! wc()->cart->needs_shipping() ) {
+			return true;
+		}
+
+		// Shipping is rated for the shipping address, or the billing address when there is none.
+		$destination = static function ( $address_holder ): array {
+			$type = $address_holder->get_shipping_country() ? 'shipping' : 'billing';
+
+			return array(
+				'country'  => $address_holder->{"get_{$type}_country"}(),
+				'state'    => $address_holder->{"get_{$type}_state"}(),
+				'postcode' => $address_holder->{"get_{$type}_postcode"}(),
+				'city'     => $address_holder->{"get_{$type}_city"}(),
+			);
+		};
+
+		// When one side has a shipping country and the other does not, the destinations above would be read from
+		// different address types. Clearing the shipping country is itself a move away from the destination the
+		// order's shipping lines were rated for, so the order is not reused.
+		if ( (bool) $order_object->get_shipping_country() !== (bool) $customer->get_shipping_country() ) {
+			return false;
+		}
+
+		return $this->locations_match( $destination( $order_object ), $destination( $customer ) );
+	}
+
+	/**
+	 * Whether the order would still be taxed for the same location once it takes the session customer's addresses,
+	 * which the cart hash does not cover either. The sync re-prices tax from the order's own addresses, so the old
+	 * tax is never charged, but the new address skips the checks checkout makes (allowed countries, state codes).
+	 * Refusing reuse sends the change through checkout instead. Same before-and-after comparison as the
+	 * pay-for-order guard, OrderController::validate_location_change(), except that a state the session filled in
+	 * by default is read as empty; its exemption for an order awaiting its first address is applied by
+	 * is_valid_draft_order().
+	 *
+	 * @since 11.3.1
+	 *
+	 * @param \WC_Order $order_object Order object to check.
+	 * @return boolean True when the tax location is unchanged or the store collects no tax; false without a customer,
+	 *                 or when either filtered tax location is not an array.
+	 */
+	protected function order_taxed_at_customer_location( \WC_Order $order_object ) {
+		$customer = wc()->customer;
+
+		if ( ! $customer ) {
+			return false;
+		}
+
+		if ( ! wc_tax_enabled() ) {
+			return true;
+		}
+
+		// A copy carrying the customer's addresses, which is what the sync would write. Nothing is saved.
+		$readdressed = clone $order_object;
+		$props       = array();
+
+		foreach ( array( 'billing', 'shipping' ) as $type ) {
+			foreach ( array( 'country', 'state', 'postcode', 'city' ) as $field ) {
+				$props[ "{$type}_{$field}" ] = $customer->{"get_{$type}_{$field}"}();
+			}
+		}
+
+		$readdressed->set_props( $props );
+
+		// get_taxable_location() is filtered (woocommerce_order_get_tax_location), so a callback can return a
+		// non-array. Don't reuse rather than pass a bad value to the array-typed locations_match().
+		$order_location       = $order_object->get_taxable_location();
+		$readdressed_location = $readdressed->get_taxable_location();
+
+		if ( ! is_array( $order_location ) || ! is_array( $readdressed_location ) ) {
+			return false;
+		}
+
+		return $this->locations_match( $order_location, $readdressed_location );
+	}
+
+	/**
+	 * Whether two locations match, reading a state the customer session filled in by default as empty.
+	 *
+	 * @param array $order_location    Location the order was priced for.
+	 * @param array $customer_location Location the session customer would give it.
+	 * @return bool
+	 */
+	private function locations_match( array $order_location, array $customer_location ): bool {
+		if ( ! OrderController::locations_differ( $order_location, $customer_location ) ) {
+			return true;
+		}
+
+		return ! OrderController::locations_differ( $this->without_default_state( $order_location ), $this->without_default_state( $customer_location ) );
+	}
+
+	/**
+	 * Returns a location without the state the customer session fills in by default.
+	 *
+	 * The session gives a customer with no billing state the default customer location's state, whatever their
+	 * country (WC_Customer_Data_Store_Session::set_defaults()), and saves it. A shopper who left an optional state
+	 * empty has it on later requests while the order they placed has none. On another country, where it is not
+	 * one of that country's states, such a state is read as empty so the same address still compares as equal.
+	 *
+	 * @param array $location Location with country and state keys.
+	 * @return array
+	 */
+	private function without_default_state( array $location ): array {
+		$country = strtoupper( (string) ( $location['country'] ?? '' ) );
+		$state   = strtoupper( (string) ( $location['state'] ?? '' ) );
+
+		if ( '' === $state ) {
+			return $location;
+		}
+
+		$states = WC()->countries->get_states( $country );
+
+		if ( is_array( $states ) && isset( $states[ $state ] ) ) {
+			return $location;
+		}
+
+		$is_default_state = static function ( $candidate ) use ( $country, $state ): bool {
+			return is_array( $candidate ) && strtoupper( (string) ( $candidate['state'] ?? '' ) ) === $state && strtoupper( (string) ( $candidate['country'] ?? '' ) ) !== $country;
+		};
+
+		// The default location is the shop base unless geolocation or a filter changes it. The base is checked first
+		// because it needs no lookup, and because a state saved on an earlier request can outlive the default.
+		if ( $is_default_state( wc_get_base_location() ) || $is_default_state( wc_get_customer_default_location() ) ) {
+			$location['state'] = '';
+		}
+
+		return $location;
+	}
+
+	/**
+	 * Whether the order has no address yet and was not created through the Store API, e.g. one a merchant made for
+	 * the shopper to fill in. It is taxed at the shop's base address and has no shipping zone yet, so, as in
+	 * OrderController::validate_location_change(), its first address is allowed.
+	 *
+	 * @param \WC_Order $order_object Order object to check.
+	 * @return bool
+	 */
+	private function order_awaits_first_address( \WC_Order $order_object ): bool {
+		return ! $order_object->get_shipping_country() && ! $order_object->get_billing_country() && 'store-api' !== $order_object->get_created_via();
+	}
+
 	/**
 	 * Whether a zero-total order is still awaiting payment, such as a fully discounted renewal order.
 	 *
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
index a02576c0a04..d9685d33add 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
@@ -567,12 +567,16 @@ class OrderController {
 	 * Checks whether two locations match a different shipping zone or tax rate.
 	 *
 	 * Shipping zones and tax rates ignore case and postcode spacing, so a change in only those does not count.
+	 * Shared with DraftOrderTrait so both re-pricing guards agree on what counts as a destination change.
+	 *
+	 * @since 11.3.0
+	 * @since 11.3.1 Made public.
 	 *
 	 * @param array $old_location Location before the change.
 	 * @param array $new_location Location after the change.
 	 * @return bool
 	 */
-	private static function locations_differ( array $old_location, array $new_location ): bool {
+	public static function locations_differ( array $old_location, array $new_location ): bool {
 		foreach ( self::LOCATION_FIELDS as $field ) {
 			if ( self::normalize_location_field( $field, $old_location ) !== self::normalize_location_field( $field, $new_location ) ) {
 				return true;
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
index b763dd77aff..e3bf79616a0 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
@@ -5676,6 +5676,612 @@ class Checkout extends \WP_Test_REST_TestCase {
 		$this->assertNotSame( $order->get_id(), (int) $response->get_data()['order_id'], 'An order the filter says needs no payment must not be reused.' );
 	}

+	/**
+	 * @testdox Checkout does not reuse a pending order from the session when the shipping destination moves but the total does not.
+	 * @testWith [{"city": "Other City"}]
+	 *           [{"city": "London", "state": "", "postcode": "SW1A 1AA", "country": "GB"}]
+	 *
+	 * @param array $destination Shipping address fields the checkout request changes.
+	 */
+	public function test_post_does_not_reuse_pending_order_from_session_when_shipping_destination_changes( array $destination ): void {
+		update_option( 'woocommerce_calc_taxes', 'no' );
+		$order          = $this->create_pending_order_for_us_customer();
+		$original_total = $order->get_total();
+
+		$this->assertTrue( $order->needs_payment(), 'Baseline: the placed order must still need payment.' );
+		$this->assertTrue( $this->get_draft_order_checker()->is_draft( $order ), 'Baseline: the order is the session draft while its destination matches the customer.' );
+		$this->assertSame( $order->get_id(), (int) WC()->session->get( 'store_api_draft_order' ), 'Baseline: the session must point at the placed order.' );
+
+		$request = $this->build_valid_post_request();
+		$posted  = array_merge( $request->get_param( 'shipping_address' ), $destination );
+		$request->set_param( 'shipping_address', $posted );
+
+		$response = rest_get_server()->dispatch( $request );
+
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$new_order = wc_get_order( (int) $response->get_data()['order_id'] );
+		$this->assertNotSame( $order->get_id(), $new_order->get_id(), 'Checkout must place a new order rather than readdress the placed one.' );
+		$this->assertSame( $original_total, $new_order->get_total(), 'The total must not change, so only the destination check can stop reuse.' );
+		$this->assertSame( array( $posted['country'], $posted['city'] ), array( $new_order->get_shipping_country(), $new_order->get_shipping_city() ), 'The new order should ship to the posted destination.' );
+
+		$order = wc_get_order( $order->get_id() );
+		$this->assertTrue( $order->has_status( OrderStatus::PENDING ), 'The placed order should stay pending.' );
+		$this->assertSame( array( 'US', 'Test City' ), array( $order->get_shipping_country(), $order->get_shipping_city() ), 'The placed order must keep the destination it was rated for.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a placed pending order once the customer moves to a destination in another shipping zone.
+	 */
+	public function test_placed_order_is_not_reused_when_shipping_destination_changes(): void {
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+
+		$this->assertTrue( $order->needs_payment(), 'Baseline: the placed order must still need payment.' );
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its destination matches the customer.' );
+
+		// Move the customer without recalculating, so the cart hash stays the same.
+		$destination = $this->get_different_destination();
+		WC()->customer->set_shipping_city( $destination['city'] );
+		WC()->customer->set_shipping_state( $destination['state'] );
+		WC()->customer->set_shipping_postcode( $destination['postcode'] );
+		WC()->customer->set_shipping_country( $destination['country'] );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the destination check can change the outcome.' );
+		$this->assertFalse( $checker->is_draft( $order ), 'A placed order whose destination moved to another zone must not be reused as the session draft.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order when a change cannot alter the shipping rate: the street line, the city's letter case, the postcode spacing or the billing address.
+	 */
+	public function test_placed_order_is_reused_when_rate_location_is_unchanged(): void {
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+
+		// Apply the changes one at a time so a failure points at the one that wrongly forced a fresh order.
+		WC()->customer->set_shipping_address_1( '456 Other Ave' );
+		$this->assertTrue( $checker->is_draft( $order ), 'A changed street line cannot alter the rate, so the order must still be reused.' );
+
+		WC()->customer->set_shipping_city( 'TEST CITY' );
+		$this->assertTrue( $checker->is_draft( $order ), 'A change of city letter case must not force a fresh order.' );
+
+		WC()->customer->set_shipping_postcode( '940 16' );
+		$this->assertTrue( $checker->is_draft( $order ), 'A change of postcode spacing must not force a fresh order.' );
+
+		WC()->customer->set_billing_country( 'GB' );
+		$this->assertTrue( $checker->is_draft( $order ), 'A billing-only change must not force a fresh order while tax follows the shipping address.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order when the cart needs no shipping, whatever the destination.
+	 */
+	public function test_placed_order_is_reused_when_cart_needs_no_shipping(): void {
+		wc_empty_cart();
+		WC()->cart->add_to_cart( $this->products[2]->get_id(), 1 );
+		$this->assertFalse( WC()->cart->needs_shipping(), 'Test cart is expected to not need shipping.' );
+
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+
+		WC()->customer->set_shipping_country( 'GB' );
+
+		$this->assertTrue( $checker->is_draft( $order ), 'A cart with nothing to ship has no destination to protect, so the order must still be reused.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a placed pending order when only the shipping $field changes.
+	 * @testWith ["country", "GB"]
+	 *           ["state", "NY"]
+	 *           ["postcode", "10001"]
+	 *           ["city", "Other City"]
+	 *
+	 * @param string $field Destination field to change.
+	 * @param string $value New value for that field.
+	 */
+	public function test_placed_order_is_not_reused_when_one_destination_field_changes( string $field, string $value ): void {
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its destination matches the customer.' );
+
+		WC()->customer->{"set_shipping_{$field}"}( $value );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the destination check can change the outcome.' );
+		$this->assertFalse( $checker->is_draft( $order ), "A placed order must not be reused once only its shipping {$field} changes." );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() takes the billing address as the destination of a placed order without a shipping address, and stops reusing it when that moves.
+	 */
+	public function test_placed_order_without_shipping_address_is_checked_against_billing(): void {
+		$checker = $this->get_draft_order_checker();
+
+		WC()->customer->set_shipping_country( '' );
+		WC()->customer->set_billing_city( 'Test City' );
+		WC()->customer->set_billing_state( 'CA' );
+		WC()->customer->set_billing_postcode( '94016' );
+		WC()->customer->set_billing_country( 'US' );
+		$order = $this->create_session_order_from_cart( OrderStatus::PENDING );
+
+		$this->assertSame( '', $order->get_shipping_country(), 'Baseline: the order must have no shipping address, so its billing address is the destination.' );
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its billing destination matches the customer.' );
+
+		WC()->customer->set_billing_state( 'NY' );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the destination check can change the outcome.' );
+		$this->assertFalse( $checker->is_draft( $order ), 'Moving the billing address must stop reuse when it is the only destination.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a zero-total pending order when the customer moves, since only checkout, which recalculates it, can place it.
+	 */
+	public function test_zero_total_order_is_reused_when_shipping_destination_changes(): void {
+		$checker = $this->get_draft_order_checker();
+		$this->apply_full_discount_coupon();
+		$order = $this->create_pending_order_for_us_customer();
+
+		$this->assertSame( 0.0, (float) $order->get_total(), 'The coupon must take the order to a zero total, which is the case this test covers.' );
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the zero-total order is the session draft while its destination matches the customer.' );
+
+		$destination = $this->get_different_destination();
+		WC()->customer->set_shipping_city( $destination['city'] );
+		WC()->customer->set_shipping_state( $destination['state'] );
+		WC()->customer->set_shipping_postcode( $destination['postcode'] );
+		WC()->customer->set_shipping_country( $destination['country'] );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the destination check could change the outcome.' );
+		$this->assertTrue( $checker->is_draft( $order ), 'A zero-total order, such as a renewal, must stay the session draft when the address changes.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a zero-total order that a filter makes payable once the customer moves, as it would a priced order.
+	 */
+	public function test_zero_total_order_made_payable_is_not_reused_when_shipping_destination_changes(): void {
+		add_filter( 'woocommerce_order_needs_payment', '__return_true' );
+		$checker = $this->get_draft_order_checker();
+		$this->apply_full_discount_coupon();
+		$order = $this->create_pending_order_for_us_customer();
+
+		$this->assertSame( 0.0, (float) $order->get_total(), 'The coupon must take the order to a zero total, which is the case this test covers.' );
+		$this->assertTrue( $order->needs_payment(), 'Baseline: the filter must make the zero-total order payable through pay-for-order.' );
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its destination matches the customer.' );
+
+		WC()->customer->set_shipping_country( 'GB' );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the destination check can change the outcome.' );
+		$this->assertFalse( $checker->is_draft( $order ), 'A zero-total order that can be paid must get the same destination check as a priced one.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() lets a placed order created via $created_via with billing country $billing_country take a first address only if it has none and did not come from the Store API.
+	 * @testWith ["admin", "", true]
+	 *           ["store-api", "", false]
+	 *           ["admin", "US", false]
+	 *
+	 * @param string $created_via     Where the order was created.
+	 * @param string $billing_country Billing country left on the order; any country means it already has an address.
+	 * @param bool   $reused          Whether the order should still be reused.
+	 */
+	public function test_order_without_address_takes_its_first_one_unless_from_the_store_api( string $created_via, string $billing_country, bool $reused ): void {
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+
+		$address = array();
+		foreach ( array( 'billing', 'shipping' ) as $type ) {
+			foreach ( array( 'country', 'state', 'postcode', 'city', 'address_1' ) as $field ) {
+				$address[ "{$type}_{$field}" ] = '';
+			}
+		}
+		$order->set_props( $address );
+		$order->set_billing_country( $billing_country );
+		$order->set_created_via( $created_via );
+		$order->save();
+
+		$this->assertSame( $billing_country, $order->get_billing_country() . $order->get_shipping_country(), 'Baseline: the order must carry only the billing country under test.' );
+		$this->assertTrue( WC()->cart->needs_shipping(), 'Baseline: the cart needs shipping, so the destination check applies.' );
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must match so only the address checks decide.' );
+		$this->assertSame( $reused, $checker->is_draft( $order ), $reused ? 'An order made outside the Store API with no address may take its first one, as on the pay-for-order page.' : 'A Store API order always has an address, so one without must not be reused for a new destination.' );
+	}
+
+	/**
+	 * @testdox GET checkout keeps an order made outside the Store API that awaits its first address as the session draft, and gives it the customer's address.
+	 */
+	public function test_get_checkout_reuses_an_order_awaiting_its_first_address(): void {
+		$order = $this->create_pending_order_for_us_customer();
+
+		$address = array();
+		foreach ( array( 'billing', 'shipping' ) as $type ) {
+			foreach ( array( 'country', 'state', 'postcode', 'city', 'address_1' ) as $field ) {
+				$address[ "{$type}_{$field}" ] = '';
+			}
+		}
+		$order->set_props( $address );
+		$order->set_created_via( 'admin' );
+		$order->save();
+
+		$response = $this->dispatch_store_api_request( 'POST', '/wc/store/v1/cart/update-customer', array( 'shipping_address' => (object) $this->get_fallback_shipping_address() ) );
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'Baseline: the cart total must not change, so only the address checks decide reuse.' );
+
+		$response = $this->dispatch_store_api_request( 'GET', '/wc/store/v1/checkout' );
+
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$this->assertSame( $order->get_id(), (int) $response->get_data()['order_id'], 'An order made outside the Store API with no address must stay the session draft, as on the pay-for-order page.' );
+		$order = wc_get_order( $order->get_id() );
+		$this->assertSame( array( 'GB', 'Oxford' ), array( $order->get_shipping_country(), $order->get_shipping_city() ), 'GET checkout should give the reused order the shipping address the customer entered.' );
+	}
+
+	/**
+	 * @testdox GET checkout does not reuse a placed pending order once the customer moves to another destination, and leaves its address and total alone.
+	 */
+	public function test_get_checkout_does_not_reuse_a_placed_order_after_a_destination_change(): void {
+		update_option( 'woocommerce_calc_taxes', 'no' );
+		$order = $this->create_pending_order_for_us_customer();
+		$total = $order->get_total();
+
+		$response = $this->dispatch_store_api_request( 'POST', '/wc/store/v1/cart/update-customer', array( 'shipping_address' => (object) $this->get_fallback_shipping_address() ) );
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'Baseline: the cart total must not change, so only the destination check can stop reuse.' );
+
+		$response = $this->dispatch_store_api_request( 'GET', '/wc/store/v1/checkout' );
+
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$this->assertSame( 0, (int) $response->get_data()['order_id'], 'GET checkout must answer with a draft response, not the placed order.' );
+		$order = wc_get_order( $order->get_id() );
+		$this->assertSame( 'US', $order->get_shipping_country(), 'The placed order must keep the destination it was rated for.' );
+		$this->assertSame( $total, $order->get_total(), 'The placed order\'s total must not change.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a placed pending order once the billing address moves to another tax location on a store that taxes on billing, but not for a change that cannot alter the rate.
+	 */
+	public function test_placed_order_is_not_reused_when_billing_tax_location_changes(): void {
+		$this->set_taxes_based_on( 'billing' );
+		$this->give_customer_us_billing_address();
+
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its tax location matches the customer.' );
+
+		WC()->customer->set_billing_address_1( '456 Other Ave' );
+		$this->assertTrue( $checker->is_draft( $order ), 'A changed billing street line cannot alter the tax, so the order must still be reused.' );
+
+		// Move the billing address without recalculating, so the cart hash stays the same.
+		WC()->customer->set_billing_state( 'NY' );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the tax location check can change the outcome.' );
+		$this->assertFalse( $checker->is_draft( $order ), 'A placed order whose billing tax location moved must not be reused as the session draft.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order when the billing address moves but tax follows the shipping address.
+	 */
+	public function test_placed_order_is_reused_when_billing_moves_and_tax_follows_shipping(): void {
+		$this->set_taxes_based_on( 'shipping' );
+		$this->give_customer_us_billing_address();
+
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+
+		WC()->customer->set_billing_state( 'NY' );
+
+		$this->assertTrue( $checker->is_draft( $order ), 'A billing change cannot alter tax worked out on the shipping address, so the order must still be reused.' );
+	}
+
+	/**
+	 * @testdox GET checkout does not reuse a placed pending order once the customer's billing address moves to another tax location on a store that taxes on billing.
+	 */
+	public function test_get_checkout_does_not_reuse_a_placed_order_after_a_billing_tax_location_change(): void {
+		$this->set_taxes_based_on( 'billing' );
+		$this->give_customer_us_billing_address();
+		$order = $this->create_pending_order_for_us_customer();
+
+		// Move the customer's billing address the way a My Account address save does: without touching the cart.
+		WC()->customer->set_billing_state( 'NY' );
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'Baseline: the cart hash must still match, so only the tax location check can stop reuse.' );
+
+		$response = $this->dispatch_store_api_request( 'GET', '/wc/store/v1/checkout' );
+
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$this->assertSame( 0, (int) $response->get_data()['order_id'], 'GET checkout must answer with a draft response, not the placed order.' );
+		$order = wc_get_order( $order->get_id() );
+		$this->assertSame( 'CA', $order->get_billing_state(), 'The placed order must keep the billing address it was taxed for.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a placed pending order when the customer clears only the shipping country.
+	 */
+	public function test_placed_order_is_not_reused_when_shipping_country_is_cleared(): void {
+		$checker = $this->get_draft_order_checker();
+
+		// Billing matches the shipping country, so on the unfixed code the order's US shipping would wrongly match
+		// the customer's US billing once the shipping country is cleared.
+		$this->give_customer_us_billing_address();
+		$order = $this->create_pending_order_for_us_customer();
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its destination matches the customer.' );
+
+		// Clear only the shipping country, leaving billing in place. The order's shipping lines are still rated for
+		// the old country, so reuse must stop rather than fall back to the matching billing destination.
+		WC()->customer->set_shipping_country( '' );
+
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash must stay the same so only the destination check can change the outcome.' );
+		$this->assertFalse( $checker->is_draft( $order ), 'Clearing the shipping country must stop reuse; billing is not a fallback once a shipping address was set.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() does not reuse a placed pending order when the tax location filter returns a non-array for the $side.
+	 * @testWith ["order"]
+	 *           ["readdressed copy"]
+	 *
+	 * @param string $side Which of the two compared tax locations the filter replaces with a non-array.
+	 */
+	public function test_placed_order_is_not_reused_when_tax_location_is_not_an_array( string $side ): void {
+		$this->set_taxes_based_on( 'shipping' );
+
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_us_customer();
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its tax location matches the customer.' );
+
+		// Runs after core's own callback on this filter. The order is compared by identity; the readdressed copy is a clone.
+		add_filter(
+			'woocommerce_order_get_tax_location',
+			static function ( $location, $filtered_order ) use ( $order, $side ) {
+				$is_order = $filtered_order === $order;
+				$replace  = 'order' === $side ? $is_order : ! $is_order;
+
+				return $replace ? 'not-an-array' : $location;
+			},
+			PHP_INT_MAX,
+			2
+		);
+
+		$this->assertFalse( $checker->is_draft( $order ), "A tax location that is not an array on the {$side} must stop reuse rather than reach locations_differ()." );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order in $country when the session fills the customer's empty billing state with the default location's state on the next request.
+	 * @testWith ["GB", "SW1A 1AA"]
+	 *           ["FR", "75001"]
+	 *
+	 * @param string $country  Country of the customer and the order: one with free-text states, one with none.
+	 * @param string $postcode Postcode of the customer and the order.
+	 */
+	public function test_placed_order_is_reused_when_session_fills_empty_billing_state( string $country, string $postcode ): void {
+		$this->set_taxes_based_on( 'billing' );
+		$order = $this->create_pending_order_for_customer_without_state( $country, $postcode );
+
+		$this->reload_customer_from_session();
+
+		$this->assertSame( 'CA', WC()->customer->get_billing_state(), 'Baseline: the session must fill the empty billing state with the shop state.' );
+		$this->assertSame( '', $order->get_billing_state(), 'Baseline: the placed order must have no billing state.' );
+		$this->assertTrue( $this->get_draft_order_checker()->is_draft( $order ), 'A state the session filled in is not a move to another tax location, so the order must still be reused.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order when the default state reaches the shipping address of a customer who left the state empty.
+	 */
+	public function test_placed_order_is_reused_when_default_state_reaches_shipping_address(): void {
+		update_option( 'woocommerce_calc_taxes', 'no' );
+		$order = $this->create_pending_order_for_customer_without_state( 'GB', 'SW1A 1AA' );
+
+		$this->reload_customer_from_session();
+		// A client that sends the billing address as the shipping address passes the filled-in state along.
+		WC()->customer->set_shipping_state( WC()->customer->get_billing_state() );
+
+		$this->assertSame( 'CA', WC()->customer->get_shipping_state(), 'Baseline: the customer must carry the shop state as the shipping state.' );
+		$this->assertTrue( $this->get_draft_order_checker()->is_draft( $order ), 'A state the session filled in is not a move to another destination, so the order must still be reused.' );
+	}
+
+	/**
+	 * @testdox Checkout reuses a placed pending order when the retry sends back the billing state the session filled in.
+	 */
+	public function test_post_reuses_pending_order_when_request_returns_session_default_state(): void {
+		$this->set_taxes_based_on( 'billing' );
+		$order = $this->create_pending_order_for_customer_without_state( 'GB', 'SW1A 1AA' );
+
+		$this->reload_customer_from_session();
+
+		$cart          = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/cart' ) )->get_data();
+		$billing_state = ( (array) $cart['billing_address'] )['state'];
+		$this->assertSame( 'CA', $billing_state, 'Baseline: the cart response must hand the client the filled-in billing state.' );
+
+		$address = array(
+			'address_1' => '1 High St',
+			'city'      => 'Test City',
+			'postcode'  => 'SW1A 1AA',
+			'country'   => 'GB',
+		);
+		$request = $this->build_valid_post_request();
+		$request->set_param( 'billing_address', array_merge( $request->get_param( 'billing_address' ), $address, array( 'state' => $billing_state ) ) );
+		$request->set_param( 'shipping_address', array_merge( $request->get_param( 'shipping_address' ), $address, array( 'state' => '' ) ) );
+
+		$response = rest_get_server()->dispatch( $request );
+
+		$this->assertSame( 200, $response->get_status(), print_r( $response->get_data(), true ) );
+		$this->assertSame( $order->get_id(), (int) $response->get_data()['order_id'], 'A retry from the same address must pay the placed order rather than place a second one.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order that took the session's default state on an earlier retry when the customer sends none.
+	 */
+	public function test_placed_order_is_reused_when_only_the_order_has_the_default_state(): void {
+		$this->set_taxes_based_on( 'billing' );
+		$order = $this->create_pending_order_for_customer_without_state( 'GB', 'SW1A 1AA' );
+		$order->set_billing_state( 'CA' );
+		$order->save();
+
+		$this->assertSame( '', WC()->customer->get_billing_state(), 'Baseline: the customer must have no billing state.' );
+		$this->assertTrue( $this->get_draft_order_checker()->is_draft( $order ), 'The default state is read as empty on the order too, so the order must still be reused.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order when the default location changes after the session filled in the state.
+	 */
+	public function test_placed_order_is_reused_when_default_location_changes_after_fill(): void {
+		$this->set_taxes_based_on( 'billing' );
+		$order = $this->create_pending_order_for_customer_without_state( 'GB', 'SW1A 1AA' );
+
+		$this->reload_customer_from_session();
+		// As when geolocation resolves the visitor on a later request.
+		add_filter(
+			'woocommerce_customer_default_location_array',
+			static fn() => array(
+				'country' => 'GB',
+				'state'   => '',
+			)
+		);
+
+		$this->assertSame( 'CA', WC()->customer->get_billing_state(), 'Baseline: the customer must keep the state filled in from the shop base.' );
+		$this->assertTrue( $this->get_draft_order_checker()->is_draft( $order ), 'A state filled in from the shop base is still read as empty once the default location is another one.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() keeps reusing a placed pending order when the filled-in state comes from a filtered default location.
+	 */
+	public function test_placed_order_is_reused_when_filled_state_comes_from_filtered_default(): void {
+		add_filter( 'woocommerce_customer_default_location', static fn() => 'US:NY' );
+		$this->set_taxes_based_on( 'billing' );
+		$order = $this->create_pending_order_for_customer_without_state( 'GB', 'SW1A 1AA' );
+
+		$this->reload_customer_from_session();
+
+		$this->assertSame( 'NY', WC()->customer->get_billing_state(), 'Baseline: the session must fill the empty billing state from the filtered default.' );
+		$this->assertTrue( $this->get_draft_order_checker()->is_draft( $order ), 'A state filled in from the default location is read as empty when it is not the shop base state either.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a placed pending order with no billing state when the customer in $country gets the state $state on a shop based in $base.
+	 * @testWith ["US:CA", "GB", "SW1A 1AA", "KENT"]
+	 *           ["US:WA", "AU", "2000", "WA"]
+	 *           ["GB:KENT", "GB", "SW1A 1AA", "KENT"]
+	 *
+	 * @param string $base     Shop base location, which is the default customer location.
+	 * @param string $country  Country of the customer and the order.
+	 * @param string $postcode Postcode of the customer and the order.
+	 * @param string $state    Billing state the customer gets after the order is placed.
+	 */
+	public function test_placed_order_is_not_reused_when_state_is_not_a_session_default( string $base, string $country, string $postcode, string $state ): void {
+		$this->set_taxes_based_on( 'billing' );
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_customer_without_state( $country, $postcode, $base );
+
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: the order is the session draft while its tax location matches the customer.' );
+
+		WC()->customer->set_billing_state( $state );
+
+		$this->assertFalse( $checker->is_draft( $order ), 'Only the default state on another country, where it is not one of its states, is read as empty; any other state is a move.' );
+	}
+
+	/**
+	 * @testdox is_valid_draft_order() stops reusing a placed pending order when the customer gets the shop state and their country code is stored in lowercase.
+	 */
+	public function test_placed_order_is_not_reused_when_country_differs_from_base_only_in_case(): void {
+		$this->set_taxes_based_on( 'billing' );
+		$checker = $this->get_draft_order_checker();
+		$order   = $this->create_pending_order_for_customer_without_state( 'NZ', '1010', 'NZ:AUK' );
+
+		WC()->customer->set_billing_country( 'nz' );
+		$this->assertTrue( $checker->is_draft( $order ), 'Baseline: a country code that differs only in case is the same tax location.' );
+
+		WC()->customer->set_billing_state( 'AUK' );
+
+		$this->assertFalse( $checker->is_draft( $order ), 'The shop state on the shop country is a real state whatever the case of the country code, so it is a move.' );
+	}
+
+	/**
+	 * Gives the customer a US billing address matching the shipping address create_pending_order_for_us_customer() sets.
+	 */
+	private function give_customer_us_billing_address(): void {
+		WC()->customer->set_billing_address_1( '123 Test St' );
+		WC()->customer->set_billing_city( 'Test City' );
+		WC()->customer->set_billing_state( 'CA' );
+		WC()->customer->set_billing_postcode( '94016' );
+		WC()->customer->set_billing_country( 'US' );
+	}
+
+	/**
+	 * Dispatches a Store API request with a valid nonce.
+	 *
+	 * @param string $method HTTP method.
+	 * @param string $route  Route path.
+	 * @param array  $body   Body parameters.
+	 * @return \WP_REST_Response
+	 */
+	private function dispatch_store_api_request( string $method, string $route, array $body = array() ): \WP_REST_Response {
+		$request = new \WP_REST_Request( $method, $route );
+		$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
+		$request->set_body_params( $body );
+
+		return rest_get_server()->dispatch( $request );
+	}
+
+	/**
+	 * Returns an object that exposes DraftOrderTrait::is_valid_draft_order() as is_draft().
+	 *
+	 * @return object
+	 */
+	private function get_draft_order_checker() {
+		return new class() {
+			use \Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait;
+
+			/**
+			 * Expose the protected check for the test.
+			 *
+			 * @param \WC_Order $order_object Order to check.
+			 * @return bool
+			 */
+			public function is_draft( $order_object ): bool {
+				return $this->is_valid_draft_order( $order_object );
+			}
+		};
+	}
+
+	/**
+	 * Gives the customer a US shipping address and places a pending order for it from the cart.
+	 *
+	 * @return \WC_Order
+	 */
+	private function create_pending_order_for_us_customer(): \WC_Order {
+		WC()->customer->set_shipping_address_1( '123 Test St' );
+		WC()->customer->set_shipping_city( 'Test City' );
+		WC()->customer->set_shipping_state( 'CA' );
+		WC()->customer->set_shipping_postcode( '94016' );
+		WC()->customer->set_shipping_country( 'US' );
+
+		return $this->create_session_order_from_cart( OrderStatus::PENDING );
+	}
+
+	/**
+	 * Gives the customer a billing and shipping address in the given country with no state and places a pending
+	 * order for it from the cart, on a shop whose default customer location is its base address.
+	 *
+	 * @param string $country  Country code.
+	 * @param string $postcode Postcode.
+	 * @param string $base     Shop base location.
+	 * @return \WC_Order
+	 */
+	private function create_pending_order_for_customer_without_state( string $country, string $postcode, string $base = 'US:CA' ): \WC_Order {
+		update_option( 'woocommerce_default_country', $base );
+		update_option( 'woocommerce_default_customer_address', 'base' );
+
+		foreach ( array( 'billing', 'shipping' ) as $type ) {
+			WC()->customer->{"set_{$type}_address_1"}( '1 High St' );
+			WC()->customer->{"set_{$type}_city"}( 'Test City' );
+			WC()->customer->{"set_{$type}_state"}( '' );
+			WC()->customer->{"set_{$type}_postcode"}( $postcode );
+			WC()->customer->{"set_{$type}_country"}( $country );
+		}
+
+		return $this->create_session_order_from_cart( OrderStatus::PENDING );
+	}
+
+	/**
+	 * Saves the customer to the session and reads it back, as the next request does.
+	 */
+	private function reload_customer_from_session(): void {
+		WC()->customer->save();
+		WC()->customer = new \WC_Customer( get_current_user_id(), true );
+	}
+
 	/**
 	 * Apply a 100% coupon to the cart, taking its total to zero.
 	 */