Commit f1c54e15de6 for woocommerce

commit f1c54e15de60c2b7408cf5d541019efd770ebbbc
Author: Mike Jolley <mike.jolley@me.com>
Date:   Mon Nov 23 14:39:52 2026 +0000

    Fix purchased items coming back into a logged-in customer's cart (#70828)

    * Fix zero-total checkout leaving the saved cart behind

    * Add changelog entry for zero-total checkout cart fix

    * Update wc_empty_cart() to delete the saved cart

    wc_empty_cart() kept a logged-in customer's saved cart, so every caller
    that empties the cart after an order (the zero-total checkout, the order
    received shortcode, third-party gateways) left the items to be loaded
    back on the next request. Since #66090 the order received page no longer
    deletes the saved cart when the reloaded cart lost its coupons.

    - wc_empty_cart() now deletes the saved cart, documented in its docblock.
    - WC_Session_Handler::forget_session() resets the loaded cart directly
      with empty_cart( false ), keeping the saved cart for an expired or
      mismatched session, and no longer creates a cart during session setup.
    - The order received shortcode no longer empties the cart;
      wc_clear_cart_after_payment() decides, as it does for the checkout block.
    - process_order_without_payment() goes back to wc_empty_cart().

    * Update changelog entries for the saved cart changes

    * Compare cart items, not totals, for the order the session sent to the gateway

    wc_clear_cart_after_payment() decided whether the cart belonged to the
    order with the order's cart hash, which includes line totals and the cart
    total. If the session's totals were recalculated between checkout and the
    order received page (a price or coupon changed while the shopper was at
    the gateway), the order's own cart was kept.

    PaymentRecovery now records a fingerprint of the cart's item keys and
    quantities with ORDER_SENT_TO_GATEWAY. cart_belongs_to_order() compares
    that fingerprint for the session's own order and falls back to the cart
    hash for any other order, such as one paid from its payment link.

    * Merge changelog entries into one

    * Test the order received page against each cart clearing decision

    Replaces the single order received test with the tests from #70034. They
    cover matching and unrelated carts, invalid order keys, both
    woocommerce_should_clear_cart_after_payment overrides and awaiting-payment
    orders.

    Co-authored-by: Icyzeni <203257134+Icyzeni@users.noreply.github.com>
    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Put the checkout shortcode test file docblock before declare

    * Treat a resubmit after a price change as a repeat of the paid order

    get_session_order_moved_past_payment() matched the order by cart hash, which covers the total,
    so a price change between a failed request and the resubmit let checkout create a second order.
    It now uses cart_belongs_to_order(), the items-and-quantities check the cart clearing step uses.

    ---------

    Co-authored-by: Icyzeni <203257134+Icyzeni@users.noreply.github.com>
    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/fix-zero-total-checkout-restores-cart b/plugins/woocommerce/changelog/fix-zero-total-checkout-restores-cart
new file mode 100644
index 00000000000..27f2eb189f4
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-zero-total-checkout-restores-cart
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Stop purchased items coming back into the cart after checkout, such as after a free order with a 100% coupon.
diff --git a/plugins/woocommerce/includes/class-wc-cart-session.php b/plugins/woocommerce/includes/class-wc-cart-session.php
index 18642a0f997..412fff0583d 100644
--- a/plugins/woocommerce/includes/class-wc-cart-session.php
+++ b/plugins/woocommerce/includes/class-wc-cart-session.php
@@ -381,6 +381,7 @@ final class WC_Cart_Session {
 		$wc_session->set( 'removed_cart_contents', null );
 		$wc_session->set( 'order_awaiting_payment', null );
 		$wc_session->set( PaymentRecovery::ORDER_SENT_TO_GATEWAY, null );
+		$wc_session->set( PaymentRecovery::CART_SENT_TO_GATEWAY, null );
 		$wc_session->set( 'store_api_draft_order', null );
 		$wc_session->set( 'shipping_method_counts', null );
 		$wc_session->set( 'previous_shipping_methods', null );
diff --git a/plugins/woocommerce/includes/class-wc-session-handler.php b/plugins/woocommerce/includes/class-wc-session-handler.php
index 5a06c5ac0db..9394e513f2e 100644
--- a/plugins/woocommerce/includes/class-wc-session-handler.php
+++ b/plugins/woocommerce/includes/class-wc-session-handler.php
@@ -643,9 +643,9 @@ class WC_Session_Handler extends WC_Session {
 	public function forget_session() {
 		wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true );

-		if ( ! is_admin() ) {
-			include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
-			wc_empty_cart();
+		// Reset the loaded cart so this request can't write it back. Keep the saved cart, which rebuilds a logged-in customer's cart after an expired or mismatched session.
+		if ( ! is_admin() && WC()->cart instanceof WC_Cart ) {
+			WC()->cart->empty_cart( false );
 		}

 		$this->_data        = array();
diff --git a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php
index 32a94c0bc5b..32cf2bfb78b 100644
--- a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php
+++ b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php
@@ -343,9 +343,6 @@ class WC_Shortcode_Checkout {
 			$order->save();
 		}

-		// Empty current cart.
-		wc_empty_cart();
-
 		// If the specified order ID was invalid, we still render the default order received page (which will simply
 		// state that the order was received, but will not output any other details: this makes it harder to probe for
 		// valid order IDs than if we state that the order ID was not recognized).
diff --git a/plugins/woocommerce/includes/wc-cart-functions.php b/plugins/woocommerce/includes/wc-cart-functions.php
index 2c5f71bc6e9..9d36239230a 100644
--- a/plugins/woocommerce/includes/wc-cart-functions.php
+++ b/plugins/woocommerce/includes/wc-cart-functions.php
@@ -42,13 +42,19 @@ function wc_protected_product_add_to_cart( $passed, $product_id ) {
 add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );

 /**
- * Clears the cart session when called.
+ * Empties the cart once the shopper is done with it, for example after an order is placed.
+ *
+ * Also deletes a logged-in customer's saved (persistent) cart, so its items aren't loaded back on the next request.
+ * To reset the cart but keep the saved cart, call `WC()->cart->empty_cart( false )` instead, as `WC_Session_Handler::forget_session()` does.
+ * Creates the cart if it isn't loaded yet, which needs `WC()->session` to be set.
+ *
+ * @since 11.4.0 Deletes the saved cart too. Before 11.4.0 it was kept, and its items came back on the next request.
  */
 function wc_empty_cart() {
 	if ( ! isset( WC()->cart ) || '' === WC()->cart ) {
 		WC()->cart = new WC_Cart();
 	}
-	WC()->cart->empty_cart( false );
+	WC()->cart->empty_cart();
 }

 /**
@@ -230,7 +236,7 @@ function wc_clear_cart_after_payment() {

 	// If the order is different from the cart, don't clear the cart. This can happen if the user has multiple tabs open and completes a different order than the one in the cart.
 	if ( $should_clear_cart_after_payment && $order instanceof WC_Order && ! WC()->cart->is_empty() ) {
-		$should_clear_cart_after_payment = $order->has_cart_hash( WC()->cart->get_cart_hash() );
+		$should_clear_cart_after_payment = PaymentRecovery::cart_belongs_to_order( $order );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Checkout/PaymentRecovery.php b/plugins/woocommerce/src/Internal/Checkout/PaymentRecovery.php
index 9166bb9b2da..0732814ae97 100644
--- a/plugins/woocommerce/src/Internal/Checkout/PaymentRecovery.php
+++ b/plugins/woocommerce/src/Internal/Checkout/PaymentRecovery.php
@@ -9,6 +9,7 @@ namespace Automattic\WooCommerce\Internal\Checkout;

 use Automattic\WooCommerce\Enums\OrderStatus;
 use Throwable;
+use WC_Cart;
 use WC_Order;

 defined( 'ABSPATH' ) || exit;
@@ -36,6 +37,16 @@ final class PaymentRecovery {
 	 */
 	public const ORDER_SENT_TO_GATEWAY = 'order_sent_to_gateway';

+	/**
+	 * Session key holding a fingerprint of the cart's items and quantities when ORDER_SENT_TO_GATEWAY was set.
+	 *
+	 * It leaves out totals, so a coupon, price or tax that changes after checkout doesn't make the
+	 * order's own cart look like a different one. Emptying the cart clears it.
+	 *
+	 * @var string
+	 */
+	public const CART_SENT_TO_GATEWAY = 'cart_sent_to_gateway';
+
 	/**
 	 * Remembers the order a checkout is about to hand to the gateway.
 	 *
@@ -46,9 +57,54 @@ final class PaymentRecovery {
 	public static function remember_order_sent_to_gateway( int $order_id ): void {
 		if ( WC()->session ) {
 			WC()->session->set( self::ORDER_SENT_TO_GATEWAY, $order_id );
+			WC()->session->set( self::CART_SENT_TO_GATEWAY, WC()->cart instanceof WC_Cart ? self::get_cart_items_fingerprint( WC()->cart ) : null );
 		}
 	}

+	/**
+	 * Whether the cart is the one the order was placed from.
+	 *
+	 * For the order this session sent to the gateway, compares the items and quantities recorded at
+	 * checkout, so a coupon or price that changed since doesn't count as a different cart. Any other
+	 * order, such as one paid from its payment link, is compared by its cart hash.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @param WC_Order $order Order object.
+	 * @return bool
+	 */
+	public static function cart_belongs_to_order( WC_Order $order ): bool {
+		$cart = WC()->cart;
+
+		if ( ! $cart instanceof WC_Cart ) {
+			return false;
+		}
+
+		$session     = WC()->session;
+		$fingerprint = $session ? $session->get( self::CART_SENT_TO_GATEWAY ) : null;
+
+		if ( $session && is_string( $fingerprint ) && '' !== $fingerprint && absint( $session->get( self::ORDER_SENT_TO_GATEWAY ) ) === $order->get_id() ) {
+			return hash_equals( $fingerprint, self::get_cart_items_fingerprint( $cart ) );
+		}
+
+		return $order->has_cart_hash( $cart->get_cart_hash() );
+	}
+
+	/**
+	 * Fingerprint of the cart's items and quantities, without prices, coupons or taxes.
+	 *
+	 * Cart item keys already cover the product, variation, attributes and extension cart item data.
+	 *
+	 * @param WC_Cart $cart Cart object.
+	 * @return string
+	 */
+	private static function get_cart_items_fingerprint( WC_Cart $cart ): string {
+		$quantities = array_map( 'strval', wp_list_pluck( $cart->get_cart_contents(), 'quantity' ) );
+		ksort( $quantities );
+
+		return md5( (string) wp_json_encode( $quantities ) );
+	}
+
 	/**
 	 * The session's order, when it moved past payment and the cart still holds it.
 	 *
@@ -68,7 +124,7 @@ final class PaymentRecovery {
 		$order_id = absint( WC()->session->get( self::ORDER_SENT_TO_GATEWAY ) );
 		$order    = wc_get_order( $order_id ? $order_id : $fallback_order_id );

-		if ( ! $order instanceof WC_Order || ! $order->has_cart_hash( WC()->cart->get_cart_hash() ) ) {
+		if ( ! $order instanceof WC_Order || ! self::cart_belongs_to_order( $order ) ) {
 			return null;
 		}

@@ -183,7 +239,7 @@ final class PaymentRecovery {
 		 * second tab both reach here with an unrelated cart. Same guard as
 		 * wc_clear_cart_after_payment().
 		 */
-		if ( WC()->cart && $order->has_cart_hash( WC()->cart->get_cart_hash() ) ) {
+		if ( self::cart_belongs_to_order( $order ) ) {
 			WC()->cart->empty_cart();
 		}
 	}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php b/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php
index 5e70e823d3d..980b41aff80 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php
@@ -186,6 +186,34 @@ class WC_Tests_Session_Handler extends WC_Unit_Test_Case {
 		$this->assertNull( $this->get_session_from_db( $this->session_key ) );
 	}

+	/**
+	 * @testdox An expired session for a logged-in customer should reset the loaded cart but keep their saved cart, so the cart can be rebuilt.
+	 */
+	public function test_destroy_session_cookie_expired_keeps_logged_in_customers_saved_cart(): void {
+		$customer_id = WC_Helper_Customer::create_customer()->get_id();
+		wp_set_current_user( $customer_id );
+		$cart_item_key = WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id() );
+
+		$handler = $this
+			->getMockBuilder( WC_Session_Handler::class )
+			->setMethods( array( 'get_session_cookie' ) )
+			->getMock();
+
+		$handler
+			->method( 'get_session_cookie' )
+			->willReturn( array( (string) $customer_id, time() - 10000, time() - 1000, '' ) );
+
+		add_filter( 'woocommerce_set_cookie_enabled', '__return_false' );
+
+		$handler->init_session_cookie();
+
+		remove_filter( 'woocommerce_set_cookie_enabled', '__return_false' );
+
+		$saved_cart = get_user_meta( $customer_id, '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
+		$this->assertArrayHasKey( $cart_item_key, $saved_cart['cart'] ?? array(), 'The saved cart should survive the session reset.' );
+		$this->assertTrue( WC()->cart->is_empty(), 'The loaded cart should be reset with the session.' );
+	}
+
 	/**
 	 * @testdox Test that session from cookie is destroyed if user is logged out.
 	 */
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 7e240c1507c..d8a3a3526fe 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -2242,6 +2242,54 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
 		$this->assertTrue( WC()->cart->is_empty(), 'The next page load should empty the cart of an order a card gateway already charged, so a retry cannot place it again.' );
 	}

+	/**
+	 * @testdox Should clear the cart of the order this session sent to the gateway, even when its totals changed after checkout.
+	 */
+	public function test_clear_cart_after_payment_ignores_totals_for_the_order_sent_to_gateway(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$coupon  = WC_Helper_Coupon::create_coupon( 'sent-to-gateway' );
+		WC()->cart->add_to_cart( $product->get_id(), 1 );
+		WC()->cart->apply_coupon( $coupon->get_code() );
+		WC()->cart->calculate_totals();
+
+		$order = WC_Helper_Order::create_order( 1, $product, array( 'status' => OrderStatus::PENDING ) );
+		$order->set_cart_hash( WC()->cart->get_cart_hash() );
+		$order->save();
+
+		PaymentRecovery::remember_order_sent_to_gateway( $order->get_id() );
+		$order->payment_complete( 'txn_paid_offsite' );
+
+		// The shopper comes back from the gateway without the coupon, say because it hit its usage limit, so the totals no longer match the order.
+		WC()->cart->remove_coupon( $coupon->get_code() );
+		WC()->cart->calculate_totals();
+		$this->assertFalse( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The test needs a cart whose hash no longer matches the order.' );
+
+		wc_clear_cart_after_payment();
+
+		$this->assertTrue( WC()->cart->is_empty(), 'The order came from this cart, so a coupon dropped since checkout should not keep it.' );
+	}
+
+	/**
+	 * @testdox Should keep items added after checkout, even for the order this session sent to the gateway.
+	 */
+	public function test_clear_cart_after_payment_keeps_items_added_after_the_order_sent_to_gateway(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		WC()->cart->add_to_cart( $product->get_id(), 1 );
+
+		$order = WC_Helper_Order::create_order( 1, $product, array( 'status' => OrderStatus::PENDING ) );
+		$order->set_cart_hash( WC()->cart->get_cart_hash() );
+		$order->save();
+
+		PaymentRecovery::remember_order_sent_to_gateway( $order->get_id() );
+		$order->payment_complete( 'txn_paid_offsite' );
+
+		WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id(), 1 );
+
+		wc_clear_cart_after_payment();
+
+		$this->assertSame( 2, WC()->cart->get_cart_contents_count(), 'Items added in another tab after checkout are a new purchase.' );
+	}
+
 	/**
 	 * @testdox Should allow woocommerce_should_clear_cart_after_payment to override the final clear cart value.
 	 */
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php b/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php
index 408dc95682a..551ddaeafbe 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-checkout-test.php
@@ -358,6 +358,36 @@ class WC_Checkout_Test extends \WC_Unit_Test_Case {
 		$this->assertNotNull( $reloaded_order->get_date_paid(), 'The intentionally payment-free order should have a paid date.' );
 	}

+	/**
+	 * @testdox A logged in customer's cart stays empty after a zero-total order instead of coming back from the saved cart.
+	 */
+	public function test_process_order_without_payment_clears_logged_in_customers_saved_cart(): void {
+		wp_set_current_user( self::factory()->user->create( array( 'role' => 'customer' ) ) );
+		WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id() );
+
+		$order = wc_create_order();
+		$order->save();
+
+		add_filter(
+			'woocommerce_checkout_no_payment_needed_redirect',
+			function () {
+				throw new RuntimeException( 'Stop the test before checkout sends its redirect.' );
+			}
+		);
+
+		try {
+			$this->sut->process_order_without_payment( $order->get_id() );
+			$this->fail( 'Checkout should reach its no-payment redirect.' );
+		} catch ( RuntimeException $exception ) {
+			$this->assertSame( 'Stop the test before checkout sends its redirect.', $exception->getMessage() );
+		}
+
+		// Load the cart the way the order received page does on the next request.
+		WC()->cart->get_cart_from_session();
+
+		$this->assertTrue( WC()->cart->is_empty(), 'The purchased items should not be restored from the saved cart.' );
+	}
+
 	/**
 	 * @testdox A logged in customer's VAT exemption should survive checkout saving their account.
 	 */
diff --git a/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-checkout-test.php b/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-checkout-test.php
index 6c158cc5468..a64b3552c5d 100644
--- a/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-checkout-test.php
+++ b/plugins/woocommerce/tests/php/includes/shortcodes/class-wc-shortcode-checkout-test.php
@@ -317,6 +317,99 @@ class WC_Shortcode_Checkout_Test extends WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * @testdox The receipt should respect the cart clearing decision made before rendering.
+	 *
+	 * @testWith [true, true, null, 0]
+	 *           [false, true, null, 1]
+	 *           [true, false, null, 1]
+	 *           [false, false, null, 1]
+	 *           [true, true, false, 1]
+	 *           [false, true, true, 0]
+	 *
+	 * @param bool      $matching_cart Whether the order belongs to the current cart.
+	 * @param bool      $valid_key     Whether the receipt has a valid order key.
+	 * @param bool|null $override      Optional cart clearing filter override.
+	 * @param int       $expected      Expected cart quantity after rendering.
+	 */
+	public function test_order_received_respects_cart_clearing_decision( bool $matching_cart, bool $valid_key, ?bool $override, int $expected ): void {
+		global $wp;
+
+		$product = WC_Helper_Product::create_simple_product();
+		WC()->cart->add_to_cart( $product->get_id() );
+		WC()->cart->calculate_totals();
+
+		$order = WC_Helper_Order::create_order( 0 );
+		$order->set_status( OrderStatus::PROCESSING );
+		$order->set_cart_hash( $matching_cart ? WC()->cart->get_cart_hash() : 'previous-cart-hash' );
+		$order->save();
+
+		$wp->query_vars['order-received'] = $order->get_id();
+		$_GET['key']                      = $valid_key ? $order->get_order_key() : 'invalid-order-key';
+
+		if ( null !== $override ) {
+			add_filter(
+				'woocommerce_should_clear_cart_after_payment',
+				static function () use ( $override ) {
+					return $override;
+				}
+			);
+		}
+
+		// Cart clearing runs on template_redirect, before the checkout shortcode renders.
+		wc_clear_cart_after_payment();
+		$this->assertSame( $expected, WC()->cart->get_cart_contents_count(), 'The pre-render cart clearing decision should match the requested scenario.' );
+
+		ob_start();
+		try {
+			WC_Shortcode_Checkout::output( array() );
+		} finally {
+			ob_end_clean();
+		}
+
+		$this->assertSame( $expected, WC()->cart->get_cart_contents_count(), 'Rendering a receipt must not override the earlier cart clearing decision.' );
+	}
+
+	/**
+	 * @testdox The receipt should retain the awaiting-payment cart clearing behavior.
+	 *
+	 * @testWith ["processing", 0]
+	 *           ["pending", 1]
+	 *           ["failed", 1]
+	 *           ["cancelled", 1]
+	 *
+	 * @param string $status   Awaiting order status.
+	 * @param int    $expected Expected cart quantity after rendering.
+	 */
+	public function test_order_received_respects_awaiting_payment_cart( string $status, int $expected ): void {
+		global $wp;
+
+		$product = WC_Helper_Product::create_simple_product();
+		WC()->cart->add_to_cart( $product->get_id() );
+		WC()->cart->calculate_totals();
+
+		$order = WC_Helper_Order::create_order( 0 );
+		$order->set_status( $status );
+		$order->set_cart_hash( WC()->cart->get_cart_hash() );
+		$order->save();
+		WC()->session->set( 'order_awaiting_payment', $order->get_id() );
+
+		$wp->query_vars['order-received'] = $order->get_id();
+		$_GET['key']                      = 'invalid-order-key';
+
+		wc_clear_cart_after_payment();
+
+		ob_start();
+		try {
+			WC_Shortcode_Checkout::output( array() );
+		} finally {
+			ob_end_clean();
+		}
+
+		$this->assertSame( $expected, WC()->cart->get_cart_contents_count(), 'Only an awaiting order that went through should clear its matching cart.' );
+		$this->assertNull( WC()->session->get( 'order_awaiting_payment' ), 'The receipt should still reset the awaiting-payment session.' );
+	}
+
 	/**
 	 * @testdox The pay page should pre-select the method a merchant assigned to an admin-created order, keeping the others available.
 	 */
diff --git a/plugins/woocommerce/tests/php/includes/wc-cart-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-cart-functions-test.php
index 32ccc315bed..69c06986fef 100644
--- a/plugins/woocommerce/tests/php/includes/wc-cart-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/wc-cart-functions-test.php
@@ -87,6 +87,19 @@ class WC_Cart_Functions_Test extends WC_Unit_Test_Case {
 		$this->assertStringStartsWith( '<span class="woocommerce-Price-amount amount">-', $coupon_html, 'The minus sign should be part of the price markup, not prepended outside it.' );
 	}

+	/**
+	 * @testdox wc_empty_cart() should delete a logged-in customer's saved cart, so its items aren't loaded back.
+	 */
+	public function test_wc_empty_cart_deletes_saved_cart(): void {
+		wp_set_current_user( self::factory()->user->create( array( 'role' => 'customer' ) ) );
+		WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id() );
+
+		wc_empty_cart();
+		WC()->cart->get_cart_from_session();
+
+		$this->assertTrue( WC()->cart->is_empty(), 'The items should not be loaded back from the saved cart.' );
+	}
+
 	/**
 	 * Creates a cart with a coupon and renders its discount HTML.
 	 *
diff --git a/plugins/woocommerce/tests/php/src/Internal/Checkout/PaymentRecoveryTest.php b/plugins/woocommerce/tests/php/src/Internal/Checkout/PaymentRecoveryTest.php
index a08298c1ecf..79c45b08a65 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Checkout/PaymentRecoveryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Checkout/PaymentRecoveryTest.php
@@ -156,6 +156,22 @@ class PaymentRecoveryTest extends WC_Unit_Test_Case {
 		$this->assertNull( PaymentRecovery::get_session_order_moved_past_payment(), 'A changed cart is a new purchase, not a repeat of the old one.' );
 	}

+	/**
+	 * @testdox Should still find the order sent to the gateway after a price change, so a resubmit doesn't place it twice.
+	 */
+	public function test_session_order_moved_past_payment_ignores_a_price_change(): void {
+		$order = $this->order_for_the_current_cart( OrderStatus::ON_HOLD );
+		PaymentRecovery::remember_order_sent_to_gateway( $order->get_id() );
+
+		$cart_item = current( WC()->cart->get_cart() );
+		$cart_item['data']->set_price( 25 );
+		WC()->cart->calculate_totals();
+
+		$this->assertFalse( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The new price changes the cart hash.' );
+		$found = PaymentRecovery::get_session_order_moved_past_payment();
+		$this->assertSame( $order->get_id(), $found ? $found->get_id() : null, 'The same items are a repeat of the order, whatever they cost now.' );
+	}
+
 	/**
 	 * @testdox Should fall back to the order the caller names when nothing was remembered.
 	 */