Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
161c802
Add --coupon-ratio parameter to order generator
layoutd Oct 22, 2025
df0fda0
Add --refund-ratio parameter to order generator
layoutd Oct 22, 2025
4d3eb9d
Improve refund logic to properly handle line items and fees
layoutd Oct 22, 2025
4dd4a96
Fix refund tax calculation to properly handle tax rate IDs
layoutd Oct 22, 2025
84a81bc
Calculate explicit refund amount from line items
layoutd Oct 22, 2025
368e5b7
Fix refund ratio logic and add multiple refund support
layoutd Oct 22, 2025
479a1a8
Update partial refund reason to show products and items
layoutd Oct 22, 2025
4f98a67
Force partial refunds for orders with existing refunds
layoutd Oct 24, 2025
a3e1536
Recalculate order totals after applying coupon
layoutd Oct 24, 2025
74ae8e1
Update includes/Generator/Order.php
layoutd Oct 24, 2025
e2494ec
Doc update
layoutd Oct 24, 2025
ff277e2
Fix array_rand edge case for orders with exactly 2 items
layoutd Oct 24, 2025
f80b01e
Refactor coupon creation to use Coupon::generate()
layoutd Oct 24, 2025
ee9ff32
Move coupon retrieval logic to Coupon::get_random()
layoutd Oct 24, 2025
07daacb
Use WordPress get_posts() API instead of raw SQL queries
layoutd Oct 24, 2025
57daab8
Add discount_type parameter to Coupon generator
layoutd Oct 24, 2025
24b2493
Refactor Order generator to use Coupon::batch()
layoutd Oct 24, 2025
f8d718c
Add discount_type parameter to CLI coupon command
layoutd Oct 24, 2025
080ee99
Update README with new coupon and order parameters
layoutd Oct 24, 2025
b9c85f9
Clarify that refunds are split evenly between partial and full
layoutd Oct 24, 2025
86a499c
Fix backwards compatibility: only set discount_type when explicitly p…
layoutd Oct 24, 2025
2c9940e
Add input validation for coupon-ratio and refund-ratio parameters
layoutd Oct 24, 2025
5b53925
Add performance comment for get_posts() in Coupon::get_random()
layoutd Oct 24, 2025
0816747
Clarify that --coupons flag is equivalent to --coupon-ratio=1.0
layoutd Oct 24, 2025
0f5dd77
Fix ratio probability calculation using integer-based random generation
layoutd Oct 24, 2025
f69d126
Add check to prevent refunds with empty line items
layoutd Oct 24, 2025
73cdb1b
Improve refund error handling and logging
layoutd Oct 24, 2025
ae9b6a6
Add validation to prevent refunds with invalid amounts
layoutd Oct 24, 2025
ac5f820
Fix refund amount validation to prevent exceeding order total
layoutd Oct 24, 2025
0b3faa5
Fix refund amount calculations to prevent rounding errors and ensure …
layoutd Oct 24, 2025
9894e7e
Fix partial refunds to track already-refunded quantities and prevent …
layoutd Oct 24, 2025
c6d22b4
Fix code quality issues in refund calculations
layoutd Oct 24, 2025
323a1a7
Add realistic date handling for refunds
layoutd Oct 24, 2025
5734f42
Refactor refund generation: extract constants and helper methods
layoutd Oct 27, 2025
f08714c
Make default 'fixed_cart' Coupon generator explicit
layoutd Oct 28, 2025
20f415b
Clarify memory impact
layoutd Oct 28, 2025
553d847
Clarify that decimal ratios for coupons and refunds are converted to …
layoutd Oct 28, 2025
e17884b
Add error logging for order generation and coupon application failures
layoutd Oct 28, 2025
36d534e
Add exact ratio distribution for deterministic coupon and refund gene…
layoutd Oct 27, 2025
09da72f
Use integer constants instead of strings for refund types
layoutd Oct 27, 2025
dc91322
Fix refund type randomness to respect explicit full/partial flags
layoutd Oct 27, 2025
6e143d9
PHPCS
layoutd Oct 30, 2025
b91c278
Simplification
layoutd Oct 30, 2025
9d62477
Extract refund distribution ratios as named constants
layoutd Oct 30, 2025
201d302
Replace array_shift with index counters to prevent state mutation
layoutd Oct 30, 2025
db93ab6
Remove unnecessary (bool) casts from wp_rand calls
layoutd Oct 30, 2025
a033f04
Use floor() instead of round() to avoid double rounding in refund dis…
layoutd Oct 30, 2025
da23c52
Simplify status check in init_ratio_flags using null coalescing
layoutd Oct 30, 2025
91f6f06
Add batch size threshold constant for memory management
layoutd Oct 30, 2025
eeed7d5
Implement threshold-based fallback for large batch operations
layoutd Oct 30, 2025
f699246
Add documentation and user notification for memory-based fallback
layoutd Oct 30, 2025
65ba0a0
Add explicit bounds checking and error logging for batch index arrays
layoutd Oct 30, 2025
a02ee68
Merge remote-tracking branch 'origin/trunk' into exact-ratio-distribu…
layoutd Oct 30, 2025
f2b9ff9
Merge branch 'trunk' into exact-ratio-distribution
layoutd Dec 11, 2025
038deb7
Remove static properties
layoutd Dec 11, 2025
fa9c16f
Update legacy coupon handling in Order generator to check batch mode …
layoutd Dec 11, 2025
1156090
Restore boolean casts for semantic clarity
layoutd Feb 3, 2026
0137d99
Add coupon parameter precedence documentation
layoutd Feb 3, 2026
8978ef3
Improve threshold messaging with warnings and error_log
layoutd Feb 3, 2026
ec3c65a
Add comprehensive testing guide for exact ratio distribution
layoutd Feb 3, 2026
81b6246
Update README with deterministic distribution documentation
layoutd Feb 3, 2026
8129f2c
Merge remote-tracking branch 'origin/trunk' into exact-ratio-distribu…
layoutd Feb 3, 2026
9f6ad3e
Use dynamic counters for coupon distribution
layoutd Feb 4, 2026
782f292
Use dynamic counters for refund distribution
layoutd Feb 4, 2026
aafdcf6
Remove obsolete flag generation methods and threshold
layoutd Feb 4, 2026
16fc6ea
Update README for unlimited batch sizes
layoutd Feb 4, 2026
e683e17
Update TESTING.md to remove threshold tests
layoutd Feb 4, 2026
9702d7d
Fix weighted selection threshold bug
layoutd Feb 4, 2026
32320bc
Restore counters when order generation fails
layoutd Feb 4, 2026
ae1e2b5
Also restore orders_remaining counter on failure
layoutd Feb 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Generate orders with a specific status.

Apply coupons to a percentage of generated orders (0.0-1.0). If no coupons exist, 6 will be created automatically (3 fixed cart, 3 percentage). Note: `--coupons` flag is equivalent to `--coupon-ratio=1.0`.

**Important:** Decimal ratios are converted to percentages using integer rounding. For example, `0.505` becomes 50% (not 50.5%) because the random generation uses integer comparison. Use whole percentages like `0.50` for precise 50% ratios.
**Deterministic Distribution (Batch Mode):** When generating multiple orders, the exact number of orders with coupons is calculated dynamically using selection without replacement (e.g., 100 orders at 0.5 ratio = exactly 50 with coupons). For odd numbers, `round()` is used (e.g., 11 orders at 0.5 = 6 with coupons). Single order generation uses probabilistic distribution.
- `wp wc generate orders <nr of orders> --coupon-ratio=0.5`

Refund a percentage of completed orders (0.0-1.0). Refunds will be split evenly between partial and full, and 25% of partial refunds will receive a second partial refund.
Refund a percentage of completed orders (0.0-1.0). Refunds are distributed as: 50% full refunds, 25% single partial refunds, and 25% multi-partial refunds (two partial refunds).

**Note:** The same decimal ratio behavior applies to refund ratios as described above for coupon ratios.
**Deterministic Distribution (Batch Mode):** When generating multiple orders, the exact number and type of refunds is calculated dynamically using weighted selection without replacement (e.g., 100 orders at 0.4 ratio = exactly 20 full, 10 partial, 10 multi-partial). For odd numbers, remainders go to multi-partial refunds. Single order generation uses probabilistic distribution.
- `wp wc generate orders <nr of orders> --status=completed --refund-ratio=0.3`

#### Order Attribution
Expand Down
208 changes: 208 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
# Testing Guide for Exact Ratio Distribution

This document provides comprehensive test cases for verifying the exact ratio distribution feature for coupons and refunds in batch mode.

## Prerequisites

- WordPress installation with WooCommerce
- WC Smooth Generator plugin installed
- WP-CLI access
- Some products already generated (run `wp wc generate products 50` if needed)

## Test Cases

### 1. Basic Coupon Ratio Tests

#### Test 1.1: Exact 50% coupon ratio
```bash
wp wc generate orders 100 --coupon-ratio=0.5
```
**Expected Result:** Exactly 50 orders with coupons

**Verification:**
```bash
# Count orders with coupons via database
wp db query "SELECT COUNT(DISTINCT order_id) FROM wp_woocommerce_order_items WHERE order_item_type = 'coupon'"
```

#### Test 1.2: Edge case - 0.0 ratio (no coupons)
```bash
wp wc generate orders 50 --coupon-ratio=0.0
```
**Expected Result:** 0 orders with coupons

#### Test 1.3: Edge case - 1.0 ratio (all coupons)
```bash
wp wc generate orders 50 --coupon-ratio=1.0
```
**Expected Result:** Exactly 50 orders with coupons

#### Test 1.4: Odd number rounding
```bash
wp wc generate orders 11 --coupon-ratio=0.5
```
**Expected Result:** Exactly 6 orders with coupons (5.5 rounds to 6)

### 2. Basic Refund Ratio Tests

#### Test 2.1: Exact 40% refund ratio with distribution
```bash
wp wc generate orders 100 --status=completed --refund-ratio=0.4
```
**Expected Result:**
- Total: 40 refunds
- Distribution: ~20 full, ~10 single partial, ~10 multi-partial

**Verification:**
```bash
# Count total refunds
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type = 'shop_order_refund'"

# Count full refunds (orders with status 'refunded')
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type = 'shop_order' AND post_status = 'wc-refunded'"
```

#### Test 2.2: Edge case - 0.0 ratio (no refunds)
```bash
wp wc generate orders 50 --status=completed --refund-ratio=0.0
```
**Expected Result:** 0 refunds

#### Test 2.3: Edge case - 1.0 ratio (all refunds)
```bash
wp wc generate orders 50 --status=completed --refund-ratio=1.0
```
**Expected Result:** Exactly 50 refunds (distributed 50/25/25)

#### Test 2.4: Odd number rounding for refunds
```bash
wp wc generate orders 11 --status=completed --refund-ratio=0.4
```
**Expected Result:**
- Total: 4 refunds (rounded)
- Distribution: ~2 full, ~1 partial, ~1 multi (remainder)

### 3. Parameter Precedence Tests

#### Test 3.1: Legacy --coupons flag
```bash
wp wc generate orders 20 --coupons
```
**Expected Result:** All 20 orders have coupons (legacy behavior preserved)

#### Test 3.2: Coupon ratio without legacy flag
```bash
wp wc generate orders 100 --coupon-ratio=0.3
```
**Expected Result:** Exactly 30 orders with coupons

#### Test 3.3: Both flags (ratio should be ignored when legacy flag present)
```bash
wp wc generate orders 100 --coupons --coupon-ratio=0.3
```
**Expected Result:** All 100 orders have coupons (--coupons takes precedence)

### 4. Single Order Generation (Probabilistic Fallback)

#### Test 4.1: Single order with coupon ratio should use probabilistic
```bash
# Run multiple times to verify probabilistic behavior
wp wc generate orders 1 --coupon-ratio=0.5
wp wc generate orders 1 --coupon-ratio=0.5
wp wc generate orders 1 --coupon-ratio=0.5
```
**Expected Result:** Approximately 50% of single orders will have coupons (varies each run)

### 5. Refund Distribution Verification

#### Test 5.1: Verify 50/25/25 refund split
```bash
# Generate orders and check distribution
wp wc generate orders 200 --status=completed --refund-ratio=0.5
```
**Expected Result:**
- Total refunds: 100
- Full refunds (~50): Orders with status "refunded"
- Single partial (~25): Orders with 1 refund, status still "completed"
- Multi-partial (~25): Orders with 2 refunds, status still "completed"

**Manual Verification:**
1. Check a sample of fully refunded orders in WP Admin
2. Check a sample of partially refunded orders
3. Count number of refund entries per order

### 6. Combined Parameters

#### Test 6.1: Date range + coupon ratio + refund ratio
```bash
wp wc generate orders 100 --date-start=2024-01-01 --date-end=2024-12-31 --status=completed --coupon-ratio=0.4 --refund-ratio=0.3
```
**Expected Result:**
- Orders spread across date range
- Exactly 40 orders with coupons
- Exactly 30 orders with refunds (distributed 50/25/25)

### 7. Failed Orders Edge Case

#### Test 7.1: Verify failed orders don't affect count
```bash
# If products are missing or invalid, some orders may fail
wp wc generate orders 100 --coupon-ratio=0.5
```
**Expected Result:**
- Count only successful orders
- If only 95 orders succeed, there should be exactly 47-48 with coupons (based on 95, not 100)

## Verification Queries

### Count Orders with Coupons
```bash
wp db query "SELECT COUNT(DISTINCT order_id) FROM wp_woocommerce_order_items WHERE order_item_type = 'coupon'"
```

### Count All Refunds
```bash
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type = 'shop_order_refund'"
```

### Count Full Refunds (Orders with 'refunded' status)
```bash
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type = 'shop_order' AND post_status = 'wc-refunded'"
```

### Count Partial Refunds
```bash
wp db query "
SELECT COUNT(*) as partial_refund_orders
FROM (
SELECT p.ID, COUNT(r.ID) as refund_count
FROM wp_posts p
LEFT JOIN wp_posts r ON r.post_parent = p.ID AND r.post_type = 'shop_order_refund'
WHERE p.post_type = 'shop_order' AND p.post_status = 'wc-completed'
GROUP BY p.ID
HAVING refund_count > 0
) as refunded_completed
"
```

### Count Multi-Partial Refunds (2 refunds on same order)
```bash
wp db query "
SELECT COUNT(*) as multi_partial_orders
FROM (
SELECT p.ID, COUNT(r.ID) as refund_count
FROM wp_posts p
LEFT JOIN wp_posts r ON r.post_parent = p.ID AND r.post_type = 'shop_order_refund'
WHERE p.post_type = 'shop_order'
GROUP BY p.ID
HAVING refund_count = 2
) as multi_refunded
"
```

## Notes

- All exact ratio tests assume successful order generation
- Exact ratio distribution uses O(1) memory via dynamic counters (selection without replacement algorithm)
- Works for any batch size without memory constraints
- Ratios are rounded using PHP's `round()` function for odd numbers
Loading