Optimize order date generation #185
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Replaces inefficient date array building with direct timestamp calculation.
Builds on: #182
Problem
For each order, the old code built an array of all dates in the range (e.g., 664 dates for 2 years), then used array_rand(). For 500 orders over 2 years, this created 332,000 unnecessary date strings.
Solution
Calculate random offset directly using timestamps instead of building arrays.
Impact
Tests
Test 1: Batch generation with date range produces chronological orders
Expected: Order dates should be in ascending or equal order (no date inversions where later ID has earlier date)
Test 2: Same-day date range edge case
Expected: All orders created with date 2024-06-15 (different times, randomized)
Test 3: Error handling for failed order generation
# Attempt to create orders when no products exist (should skip failed orders gracefully) wp wc generate orders 5Expected: Error logged but batch continues; indicates 0 orders generated in the end.
Test 4: Large multi-year batch maintains chronological order
Expected: Date range covers the specified period with proper distribution