Repository navigation
Add exact ratio distribution for deterministic test data generation - #186
Merged
Merged
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
layoutd
marked this pull request as ready for review
October 30, 2025 00:53
…before applying --coupons flag
chihsuan
approved these changes
Feb 4, 2026
chihsuan
left a comment
Member
There was a problem hiding this comment.
Great work on this feature! @layoutd Confirmed that coupons/refunds are generated correctly. 👍
I wanted to add a potential enhancement to this feature. Instead of pre-generating arrays, maybe we can calculate probability dynamically using "selection without replacement", something like this:
// Initialize counters (O(1) memory)
$coupons_remaining = isset( $args['coupon-ratio'] )
? (int) round( $amount * floatval( $args['coupon-ratio'] ) )
: 0;
$orders_remaining = $amount;
for ( $i = 1; $i <= $amount; $i++ ) {
$include_coupon = null;
if ( isset( $args['coupon-ratio'] ) ) {
// Probability = remaining_coupons / remaining_orders
// Guarantees exact count while maintaining random distribution
$include_coupon = ( wp_rand( 1, $orders_remaining ) <= $coupons_remaining );
if ( $include_coupon ) {
$coupons_remaining--;
}
}
$orders_remaining--;
// ... generate order
}The approach might be worth considering for the elimination threshold.
Contributor
Author
|
Thanks for the suggestion @chihsuan, I've implemented the changes to improve . As a follow-up PR, it would be interesting to add unit tests to cover all generation and edge cases. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
NOTE: This PR branches from #182 and will need merge conflict resolution with #184 and #185 (all modify the
batch()method). The conflicts are trivial - just combining initialization calls.Builds on: #182
Summary
Adds exact ratio distribution for coupon and refund generation in batch mode, eliminating variance from probabilistic distribution. Also includes critical bug fixes for weighted selection and counter restoration.
Current Behavior (Probabilistic)
With 100 orders at 0.5 coupon ratio:
New Behavior (Deterministic)
With 100 orders at 0.5 coupon ratio:
Key Changes
orders_remainingand distribution arrays when order generation failsBenefits
Testing
Original Implementation Details
Coupon Distribution
Pre-generates an array of boolean flags:
Odd number handling: Uses
round()for non-exact splits:Refund Distribution
Pre-generates refund type flags with exact split using integer constants for memory efficiency:
Odd number handling: Remainder goes to multi-partial:
Memory efficiency: For 20,000 orders, integers use ~160 KB vs strings ~160-320 KB, plus better CPU cache locality.