Add/order model trash untrash (#38670)
This commit is contained in:
commit
a718a59166
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: add
|
||||
|
||||
Provide a data-store agnostic way of untrashing orders.
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.' );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue