Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 9fcb497

Browse files
mikejolleynerrad
authored andcommitted
Delete orders using delete method in batches of 20 (#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
1 parent 1d477af commit 9fcb497

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/Library.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,31 @@ public static function append_draft_order_post_status( $statuses ) {
126126
}
127127

128128
/**
129-
* Delete draft orders older than a day.
129+
* Delete draft orders older than a day in batches of 20.
130130
*
131131
* Ran on a daily cron schedule.
132132
*/
133133
public static function delete_expired_draft_orders() {
134-
global $wpdb;
135-
136-
$wpdb->query(
137-
"
138-
DELETE posts, term_relationships, postmeta
139-
FROM $wpdb->posts posts
140-
LEFT JOIN $wpdb->term_relationships term_relationships ON ( posts.ID = term_relationships.object_id )
141-
LEFT JOIN $wpdb->postmeta postmeta ON ( posts.ID = postmeta.post_id )
142-
WHERE posts.post_status = 'wc-checkout-draft'
143-
AND posts.post_modified <= ( NOW() - INTERVAL 1 DAY )
144-
"
134+
$count = 0;
135+
$batch_size = 20;
136+
$orders = wc_get_orders(
137+
[
138+
'date_modified' => '<=' . strtotime( '-1 DAY' ),
139+
'limit' => $batch_size,
140+
'status' => 'wc-checkout-draft',
141+
'type' => 'shop_order',
142+
]
145143
);
144+
145+
if ( $orders ) {
146+
foreach ( $orders as $order ) {
147+
$order->delete( true );
148+
$count ++;
149+
}
150+
}
151+
152+
if ( $batch_size === $count && function_exists( 'as_enqueue_async_action' ) ) {
153+
as_enqueue_async_action( 'woocommerce_cleanup_draft_orders' );
154+
}
146155
}
147156
}

0 commit comments

Comments
 (0)