Delete orders using delete method in batches of 20 (https://github.com/woocommerce/woocommerce-blocks/pull/2574)

* Delete orders using delete method in batches of 20

* Batch size to variable

* Tweak conditional for unit tests

* Force delete of draft orders rather than trash
This commit is contained in:
Mike Jolley 2020-05-27 15:06:44 +01:00 committed by GitHub
parent 38a050bb01
commit 1d2f3e8d3c
1 changed files with 21 additions and 12 deletions

View File

@ -126,22 +126,31 @@ class Library {
} }
/** /**
* Delete draft orders older than a day. * Delete draft orders older than a day in batches of 20.
* *
* Ran on a daily cron schedule. * Ran on a daily cron schedule.
*/ */
public static function delete_expired_draft_orders() { public static function delete_expired_draft_orders() {
global $wpdb; $count = 0;
$batch_size = 20;
$wpdb->query( $orders = wc_get_orders(
" [
DELETE posts, term_relationships, postmeta 'date_modified' => '<=' . strtotime( '-1 DAY' ),
FROM $wpdb->posts posts 'limit' => $batch_size,
LEFT JOIN $wpdb->term_relationships term_relationships ON ( posts.ID = term_relationships.object_id ) 'status' => 'wc-checkout-draft',
LEFT JOIN $wpdb->postmeta postmeta ON ( posts.ID = postmeta.post_id ) 'type' => 'shop_order',
WHERE posts.post_status = 'wc-checkout-draft' ]
AND posts.post_modified <= ( NOW() - INTERVAL 1 DAY )
"
); );
if ( $orders ) {
foreach ( $orders as $order ) {
$order->delete( true );
$count ++;
}
}
if ( $batch_size === $count && function_exists( 'as_enqueue_async_action' ) ) {
as_enqueue_async_action( 'woocommerce_cleanup_draft_orders' );
}
} }
} }