Skip to content
Open
Changes from all commits
Commits
Show all changes
42 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
152b099
Prevent future refund dates and ensure second refunds occur after first
layoutd Oct 28, 2025
293f57f
Update includes/Generator/Order.php
layoutd Oct 29, 2025
490290c
Update includes/Generator/Order.php
layoutd Oct 29, 2025
52675a0
Merge remote-tracking branch 'origin/trunk' into fix-refund-date-cons…
layoutd Oct 30, 2025
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
37 changes: 29 additions & 8 deletions includes/Generator/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,23 +771,44 @@ protected static function calculate_refund_totals( $line_items ) {

/**
* Calculate a realistic refund date based on order completion or previous refund.
* Ensures refund dates never exceed current time and second refunds always occur after first.
*
* @param \WC_Order $order Order being refunded.
* @param \WC_Order_Refund|null $previous_refund Previous refund (for second refunds).
* @return string Refund date in 'Y-m-d H:i:s' format.
*/
protected static function calculate_refund_date( $order, $previous_refund = null ) {
$now = time();

if ( $previous_refund ) {
// Second refund: within 1 month of first refund
$base_date = $previous_refund->get_date_created();
$max_days = self::SECOND_REFUND_MAX_DAYS;
// Second refund: must be after first refund but before current time
$base_timestamp = strtotime( $previous_refund->get_date_created()->date( 'Y-m-d H:i:s' ) );
$max_timestamp = min( $base_timestamp + ( self::SECOND_REFUND_MAX_DAYS * DAY_IN_SECONDS ), $now );

// Ensure second refund is always after first refund
if ( $max_timestamp <= $base_timestamp ) {
// If there's no time window, use base timestamp + 1 hour (or now if that's in future)
$refund_timestamp = min( $base_timestamp + HOUR_IN_SECONDS, $now );
} else {
$refund_timestamp = wp_rand( $base_timestamp + 1, max( $base_timestamp + 1, $max_timestamp ) );
}
} else {
// First refund: within 2 months of order completion
$base_date = $order->get_date_completed();
$max_days = self::FIRST_REFUND_MAX_DAYS;
// First refund: within 2 months of order completion, but never in the future
$completion_timestamp = strtotime( $order->get_date_completed()->date( 'Y-m-d H:i:s' ) );
$max_timestamp = min( $completion_timestamp + ( self::FIRST_REFUND_MAX_DAYS * DAY_IN_SECONDS ), $now );

// Ensure we have a valid time window
if ( $max_timestamp < $completion_timestamp ) {
// Order completed in the future somehow, use current time
$refund_timestamp = $now;
} elseif ( $max_timestamp == $completion_timestamp ) {
// No time window, use completion timestamp
$refund_timestamp = $completion_timestamp;
} else {
$refund_timestamp = wp_rand( $completion_timestamp, $max_timestamp );
}
}

$random_days = wp_rand( 0, $max_days );
return date( 'Y-m-d H:i:s', strtotime( $base_date->date( 'Y-m-d H:i:s' ) ) + ( $random_days * DAY_IN_SECONDS ) );
return date( 'Y-m-d H:i:s', $refund_timestamp );
}
}