From 7da7fa0d8a73e1df5ce4540995543983eb852f21 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 30 Mar 2023 09:20:53 -0700 Subject: [PATCH] Add new hook to catch additional completed dates --- classes/VTAWooCommerce.php | 82 ++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/classes/VTAWooCommerce.php b/classes/VTAWooCommerce.php index 7e7c990..7ecb35e 100644 --- a/classes/VTAWooCommerce.php +++ b/classes/VTAWooCommerce.php @@ -65,6 +65,7 @@ public function __construct( string $plugin_name, string $plugin_version, VTACos // Add Re-orderable statuses add_filter('woocommerce_valid_order_statuses_for_order_again', [ $this, 'add_reorderable_statuses' ], 9, 1); add_filter('wc_order_is_editable', [ $this, 'add_editable_statuses' ], 10, 2); + add_action('woocommerce_order_status_changed', [ $this, 'add_date_completed' ], 10, 4); } // POST STATUS / ORDER STATUS REGISTRATION CALLBACKS // @@ -130,23 +131,23 @@ public function add_status_col_styles(): void { preg_match('/edit\.php/', $path) ) { ?> - + settings->get_reorderable_statuses() as $order_status ) { + // 1. check if curr status is a reorderable status + if ( $order_status->get_cos_key() === $curr_status ) { + $curr_completed = true; + } + // 2. check if prev status is a reorderable status + if ( $order_status->get_cos_key() === $prev_status ) { + $prev_completed = true; + } + } + + // 3. if prev was not completed & curr is completed, + // set new completed date + if ( !$prev_completed && $curr_completed ) { + try { + $order->set_date_completed(time()); + $order->save(); + } catch ( Exception $e ) { + error_log("Could not set date completed for Order ID #$order_id - $e", E_ERROR); + } + } + } + /** * Filter CB that allows non-finished orders to be editable. * @param bool $is_editable @@ -295,11 +335,11 @@ public function add_reorderable_statuses( array $order_statuses ): array { public function add_editable_statuses( bool $is_editable, WC_Order $order ): bool { $status = $order->get_status(); foreach ( $this->settings->get_reorderable_statuses() as $vta_cos ) { - if ($status === $vta_cos->get_cos_key()) { - return false; - } + if ( $status === $vta_cos->get_cos_key() ) { + return false; + } } - return true; + return true; } // PRIVATE METHODS //