Skip to content

Commit 071b096

Browse files
committed
Ensure chronological date ordering for batch-generated orders
1 parent 90172f5 commit 071b096

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

includes/Generator/Order.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class Order extends Generator {
1717
*/
1818
const SECOND_REFUND_PROBABILITY = 25;
1919

20+
/**
21+
* Pre-generated dates for batch order creation (ensures chronological order by ID).
22+
*
23+
* @var array|null
24+
*/
25+
protected static $batch_dates = null;
26+
2027
/**
2128
* Return a new order.
2229
*
@@ -202,13 +209,22 @@ public static function batch( $amount, array $args = array() ) {
202209
return $amount;
203210
}
204211

212+
// Pre-generate dates if date-start is provided
213+
// This ensures chronological order: lower order IDs = earlier dates
214+
if ( ! empty( $args['date-start'] ) ) {
215+
self::$batch_dates = self::generate_batch_dates( $amount, $args );
216+
}
217+
205218
$order_ids = array();
206219

207220
for ( $i = 1; $i <= $amount; $i ++ ) {
208221
$order = self::generate( true, $args );
209222
$order_ids[] = $order->get_id();
210223
}
211224

225+
// Clear batch dates after generation
226+
self::$batch_dates = null;
227+
212228
return $order_ids;
213229
}
214230

@@ -241,10 +257,18 @@ public static function get_customer() {
241257
* between `date-start` and the current date. You can pass an `end-date` and a random date between start
242258
* and end will be chosen.
243259
*
260+
* In batch mode with date-start set, dates are pre-generated and sorted chronologically,
261+
* ensuring lower order IDs have earlier or equal dates.
262+
*
244263
* @param array $assoc_args CLI arguments.
245264
* @return string Date string (Y-m-d)
246265
*/
247266
protected static function get_date_created( $assoc_args ) {
267+
// In batch mode, pop next date from pre-generated sorted array
268+
if ( null !== self::$batch_dates && ! empty( self::$batch_dates ) ) {
269+
return array_shift( self::$batch_dates );
270+
}
271+
248272
$current = date( 'Y-m-d', time() );
249273
if ( ! empty( $assoc_args['date-start'] ) && empty( $assoc_args['date-end'] ) ) {
250274
$start = $assoc_args['date-start'];
@@ -256,8 +280,7 @@ protected static function get_date_created( $assoc_args ) {
256280
return $current;
257281
}
258282

259-
// Use timestamp-based random selection instead of building date array
260-
// This is much more efficient, especially for large date ranges
283+
// Use timestamp-based random selection for single order generation
261284
$start_timestamp = strtotime( $start );
262285
$end_timestamp = strtotime( $end );
263286
$days_between = (int) ( ( $end_timestamp - $start_timestamp ) / DAY_IN_SECONDS );
@@ -722,4 +745,42 @@ protected static function create_refund( $order, $force_partial = false, $previo
722745

723746
return $refund;
724747
}
748+
749+
/**
750+
* Generate an array of sorted dates for batch order creation.
751+
* Ensures chronological order when creating multiple orders.
752+
*
753+
* @param int $count Number of dates to generate.
754+
* @param array $args Arguments containing date-start and optional date-end.
755+
* @return array Sorted array of date strings (Y-m-d).
756+
*/
757+
protected static function generate_batch_dates( $count, $args ) {
758+
$current = date( 'Y-m-d', time() );
759+
760+
if ( ! empty( $args['date-start'] ) && empty( $args['date-end'] ) ) {
761+
$start = $args['date-start'];
762+
$end = $current;
763+
} elseif ( ! empty( $args['date-start'] ) && ! empty( $args['date-end'] ) ) {
764+
$start = $args['date-start'];
765+
$end = $args['date-end'];
766+
} else {
767+
// No date range specified, return array of current dates
768+
return array_fill( 0, $count, $current );
769+
}
770+
771+
$start_timestamp = strtotime( $start );
772+
$end_timestamp = strtotime( $end );
773+
$days_between = (int) ( ( $end_timestamp - $start_timestamp ) / DAY_IN_SECONDS );
774+
775+
$dates = array();
776+
for ( $i = 0; $i < $count; $i++ ) {
777+
$random_days = wp_rand( 0, $days_between );
778+
$dates[] = date( 'Y-m-d', $start_timestamp + ( $random_days * DAY_IN_SECONDS ) );
779+
}
780+
781+
// Sort chronologically so lower order IDs get earlier dates
782+
sort( $dates );
783+
784+
return $dates;
785+
}
725786
}

0 commit comments

Comments
 (0)