From dec3dda375788048fcddf5c66b318eb150e71d9d Mon Sep 17 00:00:00 2001 From: "Jorge A. Torres" Date: Mon, 10 Apr 2023 14:13:12 -0500 Subject: [PATCH] Periodically delete auto-draft orders older than a week --- .../DataStores/Orders/DataSynchronizer.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php b/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php index 193009492ec..d407ef946d4 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php @@ -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' ) ); + 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. *