diff --git a/plugins/woocommerce/changelog/add-order-model-trash-untrash b/plugins/woocommerce/changelog/add-order-model-trash-untrash new file mode 100644 index 00000000000..0955b34092f --- /dev/null +++ b/plugins/woocommerce/changelog/add-order-model-trash-untrash @@ -0,0 +1,4 @@ +Significance: patch +Type: add + +Provide a data-store agnostic way of untrashing orders. diff --git a/plugins/woocommerce/includes/class-wc-order.php b/plugins/woocommerce/includes/class-wc-order.php index f29711ae372..6a1874bcee9 100644 --- a/plugins/woocommerce/includes/class-wc-order.php +++ b/plugins/woocommerce/includes/class-wc-order.php @@ -2286,4 +2286,13 @@ class WC_Order extends WC_Abstract_Order { public function is_created_via( $modus ) { return apply_filters( 'woocommerce_order_is_created_via', $modus === $this->get_created_via(), $this, $modus ); } + + /** + * Attempts to restore the specified order back to its original status (after having been trashed). + * + * @return bool If the operation was successful. + */ + public function untrash(): bool { + return (bool) $this->data_store->untrash_order( $this ); + } } diff --git a/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php index ab18e688128..17dc35b2df2 100644 --- a/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php +++ b/plugins/woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php @@ -1181,4 +1181,20 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement ); WC_Order::prime_raw_meta_data_cache( $raw_meta_data_collection, 'orders' ); } + + /** + * Attempts to restore the specified order back to its original status (after having been trashed). + * + * @param WC_Order $order The order to be untrashed. + * + * @return bool If the operation was successful. + */ + public function untrash_order( WC_Order $order ): bool { + if ( ! wp_untrash_post( $order->get_id() ) ) { + return false; + } + + $order->set_status( get_post_field( 'post_status', $order->get_id() ) ); + return (bool) $order->save(); + } } diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php index 4fb6bd7132c..1db49993c71 100644 --- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php +++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-order-data-store-cpt-test.php @@ -275,4 +275,22 @@ class WC_Order_Data_Store_CPT_Test extends WC_Unit_Test_Case { } } + /** + * Test the untrashing an order works as expected when done in an agnostic way (ie, not depending directly on + * functions such as `wp_untrash_post()`. + * + * @return void + */ + public function test_untrash(): void { + $order = WC_Helper_Order::create_order(); + $order_id = $order->get_id(); + $original_status = $order->get_status(); + + $order->delete(); + $this->assertEquals( 'trash', $order->get_status(), 'The order was successfully trashed.' ); + + $order = wc_get_order( $order_id ); + $this->assertTrue( $order->untrash(), 'The order was restored from the trash.' ); + $this->assertEquals( $original_status, $order->get_status(), 'The original order status is restored following untrash.' ); + } }