Skip to content

Conversation

@layoutd
Copy link
Contributor

@layoutd layoutd commented Oct 28, 2025

Summary

  • Prevents refund dates from being set in the future
  • Ensures second refunds always occur after first refunds
  • Handles edge cases where time windows are insufficient

Related

Builds on: #182

Changes

Modified calculate_refund_date() in Order.php:

  • Added time() checks to cap refund dates at current time
  • For first refunds: ensures date is between order completion and now (capped at 60 days)
  • For second refunds: ensures date is between first refund and now (capped at 30 days), with minimum 1-second gap
  • Added fallback logic for edge cases (e.g., orders completed today)

Technical Details

Before:

  • First refund: completion_date + random(0-60 days) → could be in future
  • Second refund: first_refund_date + random(0-30 days) → could be in future

After:

  • First refund: random(completion_date, min(completion_date + 60 days, now))
  • Second refund: random(first_refund_date + 1 second, min(first_refund_date + 30 days, now))

Test Plan

  • Generate orders from this month and verify refunds date/time doesn't exceed current time
    wp wc generate orders 10 --date-start=2025-10-01 --status=completed --refund-ratio=1.0
  • Generate orders with --date-start in the past and verify refund dates are realistic (all in Q1 2025)
    wp wc generate orders 10 --date-start=2025-01-01 --date-end=2025-01-31 --status=completed --refund-ratio=1.0
  • Verify second refunds always have timestamps after first refunds (note they display in reverse order in the UI)

layoutd and others added 30 commits October 23, 2025 01:13
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]>
layoutd and others added 9 commits October 24, 2025 16:58
…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]>
@layoutd layoutd self-assigned this Oct 28, 2025
@layoutd layoutd requested a review from Copilot October 29, 2025 23:35
Copy link
Contributor

Copilot AI left a 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.

Base automatically changed from add-order-coupon-refund-ratios to trunk October 30, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants