From 42ea92badc8eaf82bbed32be7bcd9a795d58cf5f Mon Sep 17 00:00:00 2001 From: Brian DiChiara Date: Fri, 26 Apr 2024 11:34:19 -0500 Subject: [PATCH] [#928] Adjust order results to sort by Subtotal --- .../src/classes/Auctions/Auctions.php | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php b/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php index 447e3621..56d839d7 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php @@ -561,7 +561,44 @@ public function get_bid_order_ids( ?int $auction_id = null, int $limit = -1, ?in ]; } - return wc_get_orders( $args ); + $orders = wc_get_orders( $args ); + + if ( ! $orders || ! is_array( $orders ) ) { + return []; + } + + if ( count( $orders ) <= 1 ) { + return $orders; + } + + return $this->sort_orders_by_subtotal( $orders ); + } + + /** + * Sort Orders by Subtotal + * + * @since 1.0.1 + * + * @param array $orders + * + * @return int[] + */ + private function sort_orders_by_subtotal( array $orders ): array { + $sorted = []; + $totals = []; + + foreach ( $orders as $order_id ) { + $order = wc_get_order( $order_id ); + $totals[ $order_id ] = $order->get_subtotal(); + } + + arsort( $totals ); + + foreach ( $totals as $order_id => $total ) { + $sorted[] = $order_id; + } + + return $sorted; } /**