@@ -165,11 +165,12 @@ public static function generate( $save = true, $assoc_args = array() ) {
165165 }
166166
167167 if ( $ should_refund ) {
168- $ is_partial = self ::create_refund ( $ order );
168+ // Create first refund with date within 2 months of completion
169+ $ first_refund = self ::create_refund ( $ order );
169170
170171 // Some partial refunds get a second refund (always partial)
171- if ( $ is_partial && wp_rand ( 1 , 100 ) <= self ::SECOND_REFUND_PROBABILITY ) {
172- self ::create_refund ( $ order , true );
172+ if ( $ first_refund && wp_rand ( 1 , 100 ) <= self ::SECOND_REFUND_PROBABILITY ) {
173+ self ::create_refund ( $ order , true , $ first_refund );
173174 }
174175 }
175176 }
@@ -368,11 +369,12 @@ protected static function get_or_create_coupon() {
368369 /**
369370 * Create a refund for an order (either full or partial).
370371 *
371- * @param \WC_Order $order The order to refund.
372- * @param bool $force_partial Force partial refund only.
373- * @return bool True if partial refund, false if full refund or on failure.
372+ * @param \WC_Order $order The order to refund.
373+ * @param bool $force_partial Force partial refund only.
374+ * @param \WC_Order_Refund|null $previous_refund Previous refund to base date on (for second refunds).
375+ * @return \WC_Order_Refund|false Refund object on success, false on failure.
374376 */
375- protected static function create_refund ( $ order , $ force_partial = false ) {
377+ protected static function create_refund ( $ order , $ force_partial = false , $ previous_refund = null ) {
376378 if ( ! $ order instanceof \WC_Order ) {
377379 error_log ( "Error: Order is not an instance of \WC_Order: " . print_r ( $ order , true ) );
378380 return false ;
@@ -672,13 +674,29 @@ protected static function create_refund( $order, $force_partial = false ) {
672674 );
673675 }
674676
677+ // Calculate refund date
678+ if ( $ previous_refund ) {
679+ // Second refund: within 1 month of first refund
680+ $ base_date = $ previous_refund ->get_date_created ();
681+ $ max_days = 30 ; // 1 month
682+ } else {
683+ // First refund: within 2 months of order completion
684+ $ base_date = $ order ->get_date_completed ();
685+ $ max_days = 60 ; // 2 months
686+ }
687+
688+ // Generate random date within the allowed timeframe
689+ $ random_days = wp_rand ( 0 , $ max_days );
690+ $ refund_date = date ( 'Y-m-d H:i:s ' , strtotime ( $ base_date ->date ( 'Y-m-d H:i:s ' ) ) + ( $ random_days * DAY_IN_SECONDS ) );
691+
675692 // Create the refund
676693 $ refund = wc_create_refund (
677694 array (
678695 'order_id ' => $ order ->get_id (),
679696 'amount ' => $ refund_amount ,
680697 'reason ' => $ reason ,
681698 'line_items ' => $ line_items ,
699+ 'date_created ' => $ refund_date ,
682700 )
683701 );
684702 if ( is_wp_error ( $ refund ) ) {
@@ -701,6 +719,6 @@ protected static function create_refund( $order, $force_partial = false ) {
701719 $ order ->save ();
702720 }
703721
704- return ! $ is_full_refund ;
722+ return $ refund ;
705723 }
706724}
0 commit comments