Automattic\WooCommerce\Blocks\Domain\Services

DraftOrders::delete_expired_draft_orderspublicWC 1.0

Delete draft orders older than a day in configurable batches (default: 20).

Ran on a daily cron schedule. Batch size is filterable via woocommerce_delete_expired_draft_orders_batch_size.

Method of the class: DraftOrders{}

Returns

null. Nothing (null).

Usage

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

DraftOrders::delete_expired_draft_orders() code WC 10.7.0

public function delete_expired_draft_orders() {
	$count = 0;
	/**
	 * Filters the number of draft orders deleted per batch during cleanup.
	 *
	 * Increasing this value can help improve deletion throughput for high-volume or busy stores
	 * when the cleanup task cannot keep up with the draft orders backlog.
	 *
	 * @since 10.7.0
	 * @param int $batch_size Number of draft orders to delete per batch. Default 20.
	 */
	$batch_size = max( 1, (int) apply_filters( 'woocommerce_delete_expired_draft_orders_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( self::DRAFT_CLEANUP_EVENT_HOOK );
		}
	} catch ( Exception $error ) {
		wc_caught_exception( $error, __METHOD__ );
	}
}