-
Notifications
You must be signed in to change notification settings - Fork 55
Fix refund date constraints to prevent future dates #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
- 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]>
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]>
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]>
…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]>
…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]>
- 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]>
- 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]>
…percentages using integer rounding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the calculate_refund_date method to ensure refund dates are realistic and never occur in the future. The changes prevent edge cases where generated refund dates could exceed the current time or where second refunds could occur before first refunds.
- Replaced date-based calculations with timestamp-based logic
- Added validation to ensure refund dates never exceed current time
- Implemented safeguards to guarantee second refunds always occur after first refunds
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Summary
Related
Builds on: #182
Changes
Modified
calculate_refund_date()inOrder.php:time()checks to cap refund dates at current timeTechnical Details
Before:
completion_date + random(0-60 days)→ could be in futurefirst_refund_date + random(0-30 days)→ could be in futureAfter:
random(completion_date, min(completion_date + 60 days, now))random(first_refund_date + 1 second, min(first_refund_date + 30 days, now))Test Plan
wp wc generate orders 10 --date-start=2025-10-01 --status=completed --refund-ratio=1.0wp wc generate orders 10 --date-start=2025-01-01 --date-end=2025-01-31 --status=completed --refund-ratio=1.0