Commit a314f05
Add coupon and refund ratio parameters to order generator (#182)
* Add --coupon-ratio parameter to order generator
* Add --refund-ratio parameter to order generator
* Improve refund logic to properly handle line items and fees
* Fix refund tax calculation to properly handle tax rate IDs
* Calculate explicit refund amount from line items
* Fix refund ratio logic and add multiple refund support
* Update partial refund reason to show products and items
* Force partial refunds for orders with existing refunds
* Recalculate order totals after applying coupon
* Update includes/Generator/Order.php
Co-authored-by: Copilot <[email protected]>
* Doc update
Co-authored-by: Copilot <[email protected]>
* Fix array_rand edge case for orders with exactly 2 items
* Refactor coupon creation to use Coupon::generate()
* Move coupon retrieval logic to Coupon::get_random()
* Use WordPress get_posts() API instead of raw SQL queries
* Add discount_type parameter to Coupon generator
* Refactor Order generator to use Coupon::batch()
* Add discount_type parameter to CLI coupon command
* Update README with new coupon and order parameters
* Clarify that refunds are split evenly between partial and full
* Fix backwards compatibility: only set discount_type when explicitly provided
* Add input validation for coupon-ratio and refund-ratio parameters
* Add performance comment for get_posts() in Coupon::get_random()
* Clarify that --coupons flag is equivalent to --coupon-ratio=1.0
* Fix ratio probability calculation using integer-based random generation
* Add check to prevent refunds with empty line items
* Improve refund error handling and logging
- Add detailed logging when orders cannot be refunded due to empty line items
- Consolidate refund creation error logs into single formatted message
- Add error logging for invalid order instance check
These improvements will help diagnose why some completed orders aren't receiving refunds when --refund-ratio=1.0 is specified.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add validation to prevent refunds with invalid amounts
Adds check to ensure refund amount is greater than 0 before calling wc_create_refund(). This prevents "Invalid refund amount" errors that occur when:
- Orders have 100% discount coupons (total = $0)
- Line items have $0 totals
- Calculation results in 0 or negative amount
Logs order ID, calculated amount, and order total when skipping refund.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Fix refund amount validation to prevent exceeding order total
The "Invalid refund amount" error occurred because calculated refund amounts slightly exceeded the available order total due to rounding errors in tax calculations.
Changes:
- Calculate maximum refundable amount (order total - already refunded)
- Cap refund amount to maximum available before calling wc_create_refund()
- Round both calculated refund and max refund to 2 decimal places
- Improve error logging to show order total and already refunded amounts
Example of the issue:
- Order total: $24851.03
- Calculated refund (with 3 decimal tax): $24851.04
- Result: $0.01 over limit → "Invalid refund amount" error
This fix ensures refunds never exceed the mathematically available amount, preventing WooCommerce validation errors.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Fix refund amount calculations to prevent rounding errors and ensure partial refunds stay under 50%
For full refunds, use the order's actual total instead of summing line items to avoid rounding discrepancies that created tiny 0.01 refunds. For partial refunds, ensure the total stays below 50% of the order total by removing items if needed, preventing two partial refunds from fully refunding an order.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Fix partial refunds to track already-refunded quantities and prevent over-refunding
When creating multiple refunds, the code was using original order quantities instead of accounting for items already refunded. This caused second refunds to exceed the original order quantities (e.g., 11 items refunded from an 8-item order).
Now tracks refunded quantities per item and only refunds remaining quantities. All refund logic (full items, partial quantities, and fallback) now calculates remaining quantity = original - already_refunded before processing.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Fix code quality issues in refund calculations
- Add division-by-zero guards before all $original_qty divisions
- Change parameter checks from !empty() to isset() to support explicit 0 values
- Remove unused variable $removed_item in refund amount calculation
These changes improve robustness and prevent potential PHP warnings.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add realistic date handling for refunds
- First refunds are created within 2 months of order completion date
- Second refunds are created within 1 month of first refund date
- Update create_refund() to return refund object instead of boolean
- Pass previous refund to second refund for proper date calculation
This makes generated refund data more realistic for testing.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Refactor refund generation: extract constants and helper methods
* Make default 'fixed_cart' Coupon generator explicit
* Clarify memory impact
* Clarify that decimal ratios for coupons and refunds are converted to percentages using integer rounding
* Add error logging for order generation and coupon application failures
* Fix indentation
Co-authored-by: Chi-Hsuan Huang <[email protected]>
---------
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Chi-Hsuan Huang <[email protected]>1 parent febe0d0 commit a314f05
4 files changed
+610
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
37 | 47 | | |
38 | 48 | | |
39 | 49 | | |
| |||
52 | 62 | | |
53 | 63 | | |
54 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
55 | 68 | | |
56 | 69 | | |
57 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
316 | 316 | | |
317 | 317 | | |
318 | 318 | | |
319 | | - | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
320 | 332 | | |
321 | 333 | | |
322 | 334 | | |
| |||
381 | 393 | | |
382 | 394 | | |
383 | 395 | | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
384 | 403 | | |
385 | | - | |
| 404 | + | |
386 | 405 | | |
387 | 406 | | |
388 | 407 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | | - | |
| 36 | + | |
34 | 37 | | |
35 | 38 | | |
36 | 39 | | |
| |||
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
71 | 83 | | |
72 | 84 | | |
73 | 85 | | |
| |||
76 | 88 | | |
77 | 89 | | |
78 | 90 | | |
79 | | - | |
80 | | - | |
| 91 | + | |
81 | 92 | | |
82 | 93 | | |
83 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
84 | 103 | | |
85 | 104 | | |
86 | 105 | | |
| |||
125 | 144 | | |
126 | 145 | | |
127 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
128 | 174 | | |
129 | 175 | | |
0 commit comments