Commit c60232a83c9 for woocommerce
commit c60232a83c923ec03bca1069284cb2a26066643b
Author: Jorge A. Torres <jorge.torres@automattic.com>
Date: Tue Sep 29 00:06:22 2026 +0100
Fix untrash falling back to pending reported as failed (#68569)
diff --git a/plugins/woocommerce/changelog/fix-68518-untrash-invalid-previous-status b/plugins/woocommerce/changelog/fix-68518-untrash-invalid-previous-status
new file mode 100644
index 00000000000..0e107583997
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-68518-untrash-invalid-previous-status
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fixed order notes and trash metadata not being cleaned up when an HPOS order is restored from the trash and its previous status is no longer valid.
diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
index ace31dd00ef..1834b8d0a7e 100644
--- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
+++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
@@ -2890,7 +2890,7 @@ FROM $order_meta_table
)
);
- $previous_status = 'pending';
+ $previous_status = OrderInternalStatus::PENDING;
} elseif ( $previous_state_is_invalid ) {
// If we cannot restore to pending, we should probably stand back and let the merchant intervene some other way.
wc_get_logger()->warning(
diff --git a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php
index ad4decc7ad4..51c6add1411 100644
--- a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php
+++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php
@@ -618,6 +618,31 @@ class OrdersTableDataStoreTests extends \HposTestCase {
//phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders
}
+ /**
+ * @testdox When the order's previous status is no longer registered, untrash falls back to pending and still reports success.
+ */
+ public function test_cot_datastore_untrash_falls_back_to_pending_for_invalid_previous_status() {
+ $this->toggle_cot_feature_and_usage( true );
+
+ $order = $this->create_complex_cot_order();
+ $order->set_status( OrderStatus::ON_HOLD );
+ $order->save();
+
+ $this->sut->trash_order( $order );
+ $this->sut->read( $order );
+
+ // Simulate the plugin that registered the order's previous status being deactivated.
+ $order->update_meta_data( '_wp_trash_meta_status', 'wc-unregistered-status' );
+ $order->save_meta_data();
+
+ $this->sut->read( $order );
+ $result = $this->sut->untrash_order( $order );
+
+ $this->assertTrue( $result, 'untrash_order() should report success on the pending fallback path' );
+ $this->assertSame( OrderStatus::PENDING, $order->get_status() );
+ $this->assertEmpty( $order->get_meta( '_wp_trash_meta_status' ), 'Trash meta should still be cleaned up on the pending fallback path' );
+ }
+
/**
* @testdox Notes are still restored on untrash even without a comment-status record.
*/