Commit 64b88eea7f0 for woocommerce
commit 64b88eea7f06788aa810baf695306c38d7c122d0
Author: Thomas Roberts <5656702+opr@users.noreply.github.com>
Date: Fri Dec 4 17:01:26 2026 +0000
Fix Checkout block failing to load when the session changes during render (#71726)
* Fix Checkout block failing to load when the session changes during render
* Add changelog entry for checkout session-changed load fix
* Use phpstan-param instead of suppressing IncorrectTypeHint
* Remove redundant phpcs ignores from checkout session tests
* Reuse update request check in checkout route
diff --git a/plugins/woocommerce/changelog/fix-wooplug-8376-checkout-session-changed-on-load b/plugins/woocommerce/changelog/fix-wooplug-8376-checkout-session-changed-on-load
new file mode 100644
index 00000000000..1187b7ce726
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-wooplug-8376-checkout-session-changed-on-load
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Stop the Checkout block from intermittently showing a "Your cart changed during checkout" error when the page loads.
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
index 1842162a309..0b149e11f03 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
@@ -189,7 +189,10 @@ class Checkout extends AbstractCartRoute {
// Every method of this route reads and writes the draft order, so take it before anything reads it.
$this->lock_order_or_throw( absint( $this->get_draft_order_id() ) );
// The session was loaded before the lock. Refuse externally persisted changes rather than overwrite a newer draft and potentially complete the wrong total.
- $this->throw_if_persisted_session_changed();
+ // Reads are checked in get_route_response(), because the checkout page loads them while other requests may still be saving the session.
+ if ( $this->is_update_request( $request ) ) {
+ $this->throw_if_persisted_session_changed();
+ }
$response = $this->get_response_by_request_method( $request );
} catch ( InvalidCartException $error ) {
$response = $this->get_route_error_response_from_object( $error->getError(), $error->getCode(), $error->getAdditionalData() );
@@ -250,7 +253,10 @@ class Checkout extends AbstractCartRoute {
$this->order = $this->get_draft_order();
if ( $this->order ) {
- $this->create_or_update_draft_order( $request );
+ // If another request changed the session, return the order as stored rather than overwrite it with older data.
+ if ( ! $this->has_persisted_session_changed() ) {
+ $this->create_or_update_draft_order( $request );
+ }
return $this->prepare_item_for_response(
(object) [
@@ -1297,7 +1303,7 @@ class Checkout extends AbstractCartRoute {
* @throws RouteException If another request changed the persisted session.
*/
private function throw_if_persisted_session_changed(): void {
- if ( WC()->session && SessionConcurrencyGuard::has_changed( WC()->session ) ) {
+ if ( $this->has_persisted_session_changed() ) {
throw new RouteException(
'woocommerce_rest_checkout_session_changed',
esc_html__( 'Your cart changed during checkout. Please review it and try again.', 'woocommerce' ),
@@ -1306,6 +1312,15 @@ class Checkout extends AbstractCartRoute {
}
}
+ /**
+ * Whether another request changed the persisted session after this request loaded it.
+ *
+ * @return bool
+ */
+ private function has_persisted_session_changed(): bool {
+ return WC()->session && SessionConcurrencyGuard::has_changed( WC()->session );
+ }
+
/**
* Give this request exclusive use of an order, or confirm that it still has it, and refuse the request otherwise.
*
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 0946957f434..9cada45ea06 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
@@ -16,6 +16,7 @@ use Automattic\WooCommerce\StoreApi\Routes\V1\Checkout as CheckoutRoute;
use Automattic\WooCommerce\StoreApi\Routes\V1\CheckoutOrder as CheckoutOrderRoute;
use Automattic\WooCommerce\StoreApi\SchemaController;
use Automattic\WooCommerce\StoreApi\Utilities\OrderController;
+use Automattic\WooCommerce\StoreApi\Utilities\SessionConcurrencyGuard;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
use Automattic\WooCommerce\Enums\OrderStatus;
@@ -4905,6 +4906,70 @@ class Checkout extends \WP_Test_REST_TestCase {
$this->assertEquals( 'on-hold', $order->get_status(), 'Order status should be controlled by the payment gateway, not remain custom.' );
}
+ /**
+ * @testdox GET checkout succeeds when another request changed the session, while placing an order is still refused.
+ */
+ public function test_get_succeeds_when_persisted_session_changed(): void {
+ // Simulate another request saving the session after this one loaded it.
+ SessionConcurrencyGuard::capture( WC()->session, array( 'simulated' => 'stale' ) );
+
+ $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, $response->get_data()['order_id'] );
+
+ $response = $this->dispatch_store_api_request(
+ 'POST',
+ '/wc/store/v1/checkout',
+ array(
+ 'billing_address' => (object) $this->get_fallback_billing_address(),
+ 'shipping_address' => (object) $this->get_fallback_billing_address(),
+ 'payment_method' => WC_Gateway_BACS::ID,
+ )
+ );
+ $this->assertSame( 409, $response->get_status() );
+ $this->assertSame( 'woocommerce_rest_checkout_session_changed', $response->get_data()['code'] );
+ }
+
+ /**
+ * @testdox GET checkout returns the stored draft order without saving it when another request changed the session.
+ */
+ public function test_get_does_not_update_draft_order_when_persisted_session_changed(): void {
+ $order = new \WC_Order();
+ foreach ( wc()->cart->get_cart() as $cart_item ) {
+ $order->add_product( $cart_item['data'], $cart_item['quantity'] );
+ }
+ $order->set_status( OrderStatus::PENDING );
+ $order->set_cart_hash( wc()->cart->get_cart_hash() );
+ $order->calculate_totals();
+ $order->save();
+ wc()->session->set( 'store_api_draft_order', $order->get_id() );
+
+ $saves = 0;
+ $count = function ( $saved_order ) use ( &$saves, $order ) {
+ if ( $saved_order->get_id() === $order->get_id() ) {
+ ++$saves;
+ }
+ };
+ add_action( 'woocommerce_before_order_object_save', $count );
+
+ try {
+ $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(), $response->get_data()['order_id'], 'Setup: GET should reuse the pending order.' );
+ $this->assertGreaterThan( 0, $saves, 'Setup: GET should update the order when the session is unchanged.' );
+
+ $saves = 0;
+ SessionConcurrencyGuard::capture( WC()->session, array( 'simulated' => 'stale' ) );
+
+ $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(), $response->get_data()['order_id'] );
+ $this->assertSame( 0, $saves, 'GET must not overwrite the order with session data another request replaced.' );
+ } finally {
+ remove_action( 'woocommerce_before_order_object_save', $count );
+ }
+ }
+
/**
* GET /wc/store/v1/checkout against a populated cart must not create a draft order.
*