From efdea282410dc5572488d0ad96bb7feeb328d478 Mon Sep 17 00:00:00 2001 From: Justin Palmer <228780+layoutd@users.noreply.github.com> Date: Fri, 24 Oct 2025 17:48:00 +0200 Subject: [PATCH] Fix order paid and completed dates to respect historical creation dates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, when generating orders with historical dates, the paid and completed dates would be set to the current time instead of being based on the order's creation date. This was because WooCommerce's status transition hooks would override these dates during the initial save. This fix moves the date-setting logic to after the initial order save, preventing WooCommerce from overriding the historical dates. The dates are now correctly set relative to the order's creation date (paid date is 0-36 hours after creation, completed date is 0-36 hours after paid). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- includes/Generator/Order.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/includes/Generator/Order.php b/includes/Generator/Order.php index b8fbb64..206b66b 100644 --- a/includes/Generator/Order.php +++ b/includes/Generator/Order.php @@ -100,20 +100,22 @@ public static function generate( $save = true, $assoc_args = array() ) { OrderAttribution::add_order_attribution_meta( $order, $assoc_args ); } - // Set paid and completed dates based on order status. - if ( 'completed' === $status || 'processing' === $status ) { - // Add random 0 to 36 hours to creation date. - $date_paid = date( 'Y-m-d H:i:s', strtotime( $date ) + ( wp_rand( 0, 36 ) * HOUR_IN_SECONDS ) ); - $order->set_date_paid( $date_paid ); - if ( 'completed' === $status ) { - // Add random 0 to 36 hours to paid date. - $date_completed = date( 'Y-m-d H:i:s', strtotime( $date_paid ) + ( wp_rand( 0, 36 ) * HOUR_IN_SECONDS ) ); - $order->set_date_completed( $date_completed ); - } - } - if ( $save ) { $order->save(); + + // Set paid and completed dates AFTER initial save to prevent WooCommerce from overriding them. + // This must be done after save so the status transition doesn't reset the dates to current time. + if ( 'completed' === $status || 'processing' === $status ) { + // Add random 0 to 36 hours to creation date. + $date_paid = date( 'Y-m-d H:i:s', strtotime( $date ) + ( wp_rand( 0, 36 ) * HOUR_IN_SECONDS ) ); + $order->set_date_paid( $date_paid ); + if ( 'completed' === $status ) { + // Add random 0 to 36 hours to paid date. + $date_completed = date( 'Y-m-d H:i:s', strtotime( $date_paid ) + ( wp_rand( 0, 36 ) * HOUR_IN_SECONDS ) ); + $order->set_date_completed( $date_completed ); + } + $order->save(); + } } /**