Create orders as 'auto-draft' by default in admin (#37643)

This commit is contained in:
Ron Rennick 2023-05-03 11:28:29 -03:00 committed by GitHub
commit 066e269da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 97 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
Create orders as 'auto-draft' by default in admin.

View File

@ -638,7 +638,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
}
// If the old status is set but unknown (e.g. draft) assume its pending for action usage.
if ( $old_status && ! in_array( 'wc-' . $old_status, $this->get_valid_statuses(), true ) && ! in_array( $old_status, $status_exceptions, true ) ) {
if ( $old_status && ( 'auto-draft' === $old_status || ( ! in_array( 'wc-' . $old_status, $this->get_valid_statuses(), true ) && ! in_array( $old_status, $status_exceptions, true ) ) ) ) {
$old_status = 'pending';
}
}

View File

@ -471,7 +471,7 @@ class ListTable extends WP_List_Table {
$view_counts[ $slug ] = $total_in_status;
}
if ( ( get_post_status_object( $slug ) )->show_in_admin_all_list ) {
if ( ( get_post_status_object( $slug ) )->show_in_admin_all_list && 'auto-draft' !== $slug ) {
$all_count += $total_in_status;
}
}
@ -550,8 +550,9 @@ class ListTable extends WP_List_Table {
array_merge(
wc_get_order_statuses(),
array(
'trash' => ( get_post_status_object( 'trash' ) )->label,
'draft' => ( get_post_status_object( 'draft' ) )->label,
'trash' => ( get_post_status_object( 'trash' ) )->label,
'draft' => ( get_post_status_object( 'draft' ) )->label,
'auto-draft' => ( get_post_status_object( 'auto-draft' ) )->label,
)
),
array_flip( get_post_stati( array( 'show_in_admin_status_list' => true ) ) )
@ -916,7 +917,7 @@ class ListTable extends WP_List_Table {
}
// Gracefully handle legacy statuses.
if ( in_array( $order->get_status(), array( 'trash', 'draft' ), true ) ) {
if ( in_array( $order->get_status(), array( 'trash', 'draft', 'auto-draft' ), true ) ) {
$status_name = ( get_post_status_object( $order->get_status() ) )->label;
} else {
$status_name = wc_get_order_status_name( $order->get_status() );

View File

@ -286,9 +286,14 @@ class PageController {
$this->order = new $order_class_name();
$this->order->set_object_read( false );
$this->order->set_status( 'pending' );
$this->order->set_status( 'auto-draft' );
$this->order->save();
// Schedule auto-draft cleanup. We re-use the WP event here on purpose.
if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );
}
$theorder = $this->order;
}

View File

@ -79,6 +79,8 @@ class DataSynchronizer implements BatchProcessorInterface {
self::add_action( 'deleted_post', array( $this, 'handle_deleted_post' ), 10, 2 );
self::add_action( 'woocommerce_new_order', array( $this, 'handle_updated_order' ), 100 );
self::add_action( 'woocommerce_update_order', array( $this, 'handle_updated_order' ), 100 );
self::add_action( 'wp_scheduled_auto_draft_delete', array( $this, 'delete_auto_draft_orders' ), 9 );
self::add_filter( 'woocommerce_feature_description_tip', array( $this, 'handle_feature_description_tip' ), 10, 3 );
}
@ -467,6 +469,45 @@ ORDER BY orders.id ASC
}
}
/**
* Handles deletion of auto-draft orders in sync with WP's own auto-draft deletion.
*
* @since 7.7.0
*
* @return void
*/
private function delete_auto_draft_orders() {
if ( ! $this->custom_orders_table_is_authoritative() ) {
return;
}
// Fetch auto-draft orders older than 1 week.
$to_delete = wc_get_orders(
array(
'date_query' => array(
array(
'column' => 'date_created',
'before' => '-1 week',
),
),
'orderby' => 'date',
'order' => 'ASC',
'status' => 'auto-draft',
)
);
foreach ( $to_delete as $order ) {
$order->delete( true );
}
/**
* Fires after schedueld deletion of auto-draft orders has been completed.
*
* @since 7.7.0
*/
do_action( 'woocommerce_scheduled_auto_draft_delete' );
}
/**
* Handle the 'woocommerce_feature_description_tip' filter.
*

View File

@ -28,6 +28,7 @@ class DataSynchronizerTests extends WC_Unit_Test_Case {
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
OrderHelper::delete_order_custom_tables(); // We need this since non-temporary tables won't drop automatically.
OrderHelper::create_order_custom_table_if_not_exist();
OrderHelper::toggle_cot( false );
$this->sut = wc_get_container()->get( DataSynchronizer::class );
$features_controller = wc_get_container()->get( Featurescontroller::class );
$features_controller->change_feature_enable( 'custom_order_tables', true );
@ -311,4 +312,43 @@ class DataSynchronizerTests extends WC_Unit_Test_Case {
$this->assertEquals( $order2->get_id(), $orders_to_migrate[0] );
$this->assertEquals( $max_id + 1, $orders_to_migrate[1] );
}
/**
* Tests that auto-draft orders older than 1 week are automatically deleted when WP does the same for posts.
*
* @return void
*/
public function test_auto_draft_deletion(): void {
OrderHelper::toggle_cot( true );
$order1 = new \WC_Order();
$order1->set_status( 'auto-draft' );
$order1->set_date_created( strtotime( '-10 days' ) );
$order1->save();
$order2 = new \WC_Order();
$order2->set_status( 'auto-draft' );
$order2->save();
$order3 = new \WC_Order();
$order3->set_status( 'processing' );
$order3->save();
// Run WP's auto-draft delete.
do_action( 'wp_scheduled_auto_draft_delete' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
$orders = wc_get_orders(
array(
'status' => 'all',
'limit' => -1,
'return' => 'ids',
)
);
// Confirm that only $order1 is deleted when the action runs but the other orders remain intact.
$this->assertContains( $order2->get_id(), $orders );
$this->assertContains( $order3->get_id(), $orders );
$this->assertNotContains( $order1->get_id(), $orders );
}
}