Commit 579a93a50b5 for woocommerce

commit 579a93a50b5f36b4a6ce431c1d4bbae65c8f9b0b
Author: Mike Jolley <mike.jolley@me.com>
Date:   Mon Nov 23 11:29:24 2026 +0000

    Fix stale line items on Store API orders resumed by extensions (#71216)

    * Fix Store API keeping stale line items when an extension sets the cart hash

    * Add changelog entry for Store API line item rebuild fix

diff --git a/plugins/woocommerce/changelog/fix-blocks-renewal-coupon-line-items b/plugins/woocommerce/changelog/fix-blocks-renewal-coupon-line-items
new file mode 100644
index 00000000000..e1c058aa41e
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-blocks-renewal-coupon-line-items
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Charge the discounted total when a coupon is applied before reloading the Checkout block for an existing unpaid order.
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
index d9685d33add..166dee3abcc 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
@@ -1315,8 +1315,15 @@ class OrderController {
 		$cart            = $cart_controller->get_cart_instance();
 		$cart_hashes     = $cart_controller->get_cart_hashes();

-		if ( $order->get_cart_hash() !== $cart_hashes['line_items'] ) {
-			$order->set_cart_hash( $cart_hashes['line_items'] );
+		// The cart hash covers the cart total too, and checkout only resumes an order whose cart hash matches the cart.
+		$order->set_cart_hash( $cart_hashes['line_items'] );
+
+		// Extensions such as Subscriptions also set the cart hash so checkout resumes their order, so it can't tell whether
+		// the line items were built from this cart. Hash the cart contents for that, like classic checkout does.
+		$line_items_hash = md5( (string) wp_json_encode( $cart->get_cart_for_session() ) );
+
+		if ( $order->get_meta( '_line_items_hash' ) !== $line_items_hash ) {
+			$order->update_meta_data( '_line_items_hash', $line_items_hash );
 			$order->remove_order_items( OrderItemType::LINE_ITEM );
 			wc()->checkout->create_order_line_items( $order, $cart );
 		}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
index c05a6466b87..8920476095e 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/OrderControllerTests.php
@@ -681,6 +681,68 @@ class OrderControllerTests extends \WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * @testdox update_order_from_cart() rebuilds line items from a changed cart even when an extension set the order's cart hash to match it.
+	 */
+	public function test_update_order_from_cart_rebuilds_line_items_when_an_extension_set_the_cart_hash(): void {
+		$product = WC_Helper_Product::create_simple_product( true, array( 'virtual' => true ) );
+		CouponHelper::create_coupon(
+			'ten-percent',
+			'publish',
+			array(
+				'discount_type' => 'percent',
+				'coupon_amount' => '10',
+			)
+		);
+		WC()->cart->empty_cart();
+		WC()->cart->add_to_cart( $product->get_id() );
+
+		$order = $this->sut->create_order_from_cart();
+		$order->save();
+		$this->assertSame( 10.0, (float) $order->get_total(), 'The order should start at the undiscounted product price.' );
+
+		WC()->cart->apply_coupon( 'ten-percent' );
+		WC()->cart->calculate_totals();
+		// Subscriptions sets the cart hash of the renewal order it resumes on every cart load, before checkout syncs the order.
+		$order->set_cart_hash( WC()->cart->get_cart_hash() );
+		$order->save();
+
+		$this->sut->update_order_from_cart( $order );
+
+		$line_items = $order->get_items();
+		$this->assertSame( 9.0, (float) reset( $line_items )->get_total(), 'The line item should be rebuilt with the coupon discount.' );
+		$this->assertSame( 9.0, (float) $order->get_total(), 'The order total should include the coupon discount.' );
+	}
+
+	/**
+	 * @testdox update_order_from_cart() keeps unchanged line items, but still updates the cart hash when only the cart total changes.
+	 */
+	public function test_update_order_from_cart_keeps_line_items_and_updates_cart_hash_when_only_the_total_changes(): void {
+		$product = WC_Helper_Product::create_simple_product( true, array( 'virtual' => true ) );
+		WC()->cart->empty_cart();
+		WC()->cart->add_to_cart( $product->get_id() );
+
+		$order = $this->sut->create_order_from_cart();
+		$order->save();
+		$line_item_ids = array_keys( $order->get_items() );
+
+		$add_fee = static function ( \WC_Cart $cart ) {
+			$cart->add_fee( 'Handling', 5 );
+		};
+		add_action( 'woocommerce_cart_calculate_fees', $add_fee );
+
+		try {
+			$this->sut->update_order_from_cart( $order );
+			$order->save();
+		} finally {
+			remove_action( 'woocommerce_cart_calculate_fees', $add_fee );
+		}
+
+		$this->assertSame( $line_item_ids, array_keys( $order->get_items() ), 'Line items should be kept when the cart contents did not change.' );
+		$this->assertSame( 15.0, (float) $order->get_total(), 'The order total should include the new fee.' );
+		$this->assertTrue( $order->has_cart_hash( WC()->cart->get_cart_hash() ), 'The cart hash should follow the cart total so checkout can resume the order.' );
+	}
+
 	/**
 	 * @testdox update_order_from_cart() keeps the shopper's session choice on the order even when the default payment method filter returns nothing.
 	 */