Automattic\WooCommerce\Blocks\Domain\Services

DraftOrders::delete_expired_draft_orders()publicWC 1.0

Delete draft orders older than a day in batches of 20.

Ran on a daily cron schedule.

Method of the class: DraftOrders{}

No Hooks.

Return

null. Nothing (null).

Usage

$DraftOrders = new DraftOrders();
$DraftOrders->delete_expired_draft_orders();

DraftOrders::delete_expired_draft_orders() code WC 8.7.0

public function delete_expired_draft_orders() {
	$count      = 0;
	$batch_size = 20;
	$this->ensure_draft_status_registered();
	$orders = wc_get_orders(
		[
			'date_modified' => '<=' . strtotime( '-1 DAY' ),
			'limit'         => $batch_size,
			'status'        => self::DB_STATUS,
			'type'          => 'shop_order',
		]
	);

	// do we bail because the query results are unexpected?
	try {
		$this->assert_order_results( $orders, $batch_size );
		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' );
		}
	} catch ( Exception $error ) {
		wc_caught_exception( $error, __METHOD__ );
	}
}