Skip to content

Commit

Permalink
Add new hook to catch additional completed dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jpham93 committed Mar 30, 2023
1 parent fcc6f65 commit 7da7fa0
Showing 1 changed file with 61 additions and 21 deletions.
82 changes: 61 additions & 21 deletions classes/VTAWooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down Expand Up @@ -130,23 +131,23 @@ public function add_status_col_styles(): void {
preg_match('/edit\.php/', $path)
) {
?>
<style>
<?php foreach ($this->vta_cos as $order_status) {
$status = $order_status->get_cos_key();
$color = $order_status->get_cos_color();

$css_rule = <<<CSS
mark.order-status.status-$status {
background: $color;
color: #fff;
font-weight: 500;
text-shadow: 1px 1px rgba(0,0,0,0.5);
}
CSS;

echo $css_rule;
}?>
</style>
<style>
<?php foreach ($this->vta_cos as $order_status) {
$status = $order_status->get_cos_key();
$color = $order_status->get_cos_color();

$css_rule = <<<CSS
mark.order-status.status-$status {
background: $color;
color: #fff;
font-weight: 500;
text-shadow: 1px 1px rgba(0,0,0,0.5);
}
CSS;

echo $css_rule;
}?>
</style>
<?php
}
}
Expand Down Expand Up @@ -286,6 +287,45 @@ public function add_reorderable_statuses( array $order_statuses ): array {
return $order_statuses;
}

/**
* @param int $order_id
* @param string $prev_status
* @param string $curr_status
* @param WC_Order $order
* @return void
*/
public function add_date_completed(
int $order_id,
string $prev_status,
string $curr_status,
WC_Order $order
): void {
$curr_completed = false;
$prev_completed = false;

foreach ( $this->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
Expand All @@ -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 //
Expand Down

0 comments on commit 7da7fa0

Please sign in to comment.