From f4b629a09234abd745ed0f726d901ab6d7b6c37a Mon Sep 17 00:00:00 2001 From: Brian DiChiara <122309362+bd-viget@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:42:25 -0500 Subject: [PATCH] [#928] Highest Bid Issue (#929) * [#928] Save last bid order in Auction Meta * [#928] Remove Delete Order, Switch to Cancel Order --- .../goodbids/src/classes/Auctions/Auction.php | 14 ++++++++- .../src/classes/Auctions/Auctions.php | 30 +++++++++++++++++-- .../classes/Plugins/WooCommerce/Checkout.php | 4 +-- .../classes/Plugins/WooCommerce/Orders.php | 15 ++-------- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php b/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php index 8e8db07a..0e7c4252 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php @@ -80,6 +80,12 @@ class Auction { */ const BID_LOCKED_META_KEY = '_goodbids_bid_locked'; + /** + * @since 1.0.1 + * @var string + */ + const LAST_BID_ORDER_META_KEY = '_goodbids_last_bid_order_id'; + /** * @since 1.0.0 */ @@ -560,7 +566,7 @@ public function is_ending_soon(): bool { return false; } - // if the diff is less than the max for the threshold + // if the diff is less than the max for the threshold // and the diff is more than the min for the threshold // (i.e., if it falls within the threshold), // then it's ending soon. @@ -1141,6 +1147,12 @@ public function get_user_total_donated_formatted( int $user_id ): string { * @return ?WC_Order */ public function get_last_bid(): ?WC_Order { + $last_order = get_post_meta( $this->get_id(), self::LAST_BID_ORDER_META_KEY, true ); + + if ( $last_order ) { + return wc_get_order( $last_order ); + } + $orders = $this->get_bid_orders( 1 ); if ( empty( $orders ) ) { diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php b/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php index aa2a3a7e..447e3621 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php @@ -118,6 +118,9 @@ public function __construct() { // Configure some values for new Auction posts. $this->new_auction_post_init(); + // Save the highest bid order ID in Auction Meta. + $this->store_highest_bid_order(); + // Clear metric transients on new Bid Order. $this->maybe_clear_metric_transients(); @@ -644,7 +647,30 @@ function ( int $order_id, int $auction_id ) { $this->clear_transients( $auction_id ); }, - 11, + 15, + 2 + ); + } + + /** + * Store the Highest Bid Order ID in Auction Meta. + * + * @since 1.0.1 + * + * @return void + */ + private function store_highest_bid_order(): void { + add_action( + 'goodbids_order_payment_complete', + function ( int $order_id, int $auction_id ) { + // Bail early if this isn't a Bid order. + if ( ! goodbids()->woocommerce->orders->is_bid_order( $order_id ) ) { + return; + } + + update_post_meta( $auction_id, Auction::LAST_BID_ORDER_META_KEY, $order_id ); + }, + 10, 2 ); } @@ -699,7 +725,7 @@ function ( int $order_id, int $auction_id ) { Log::error( 'There was a problem extending the Auction', compact( 'auction_id' ) ); } }, - 10, + 13, 2 ); } diff --git a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php index ab21a58c..41e9a5e3 100644 --- a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php +++ b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php @@ -159,7 +159,7 @@ function ( WC_Order $order ): void { // Make sure Auction has not ended. if ( $auction->has_ended() ) { goodbids()->notices->add_notice( Notices::AUCTION_HAS_ENDED ); - goodbids()->woocommerce->orders->delete( $order->get_id(), true ); + goodbids()->woocommerce->orders->cancel( $order->get_id() ); return; } @@ -180,7 +180,7 @@ function ( WC_Order $order ): void { // Perform this check last to ensure the bid hasn't already been placed. if ( ! $auction->bid_allowed( $info ) ) { - goodbids()->woocommerce->orders->delete( $order->get_id(), true ); + goodbids()->woocommerce->orders->cancel( $order->get_id() ); goodbids()->notices->add_notice( Notices::BID_ALREADY_PLACED ); return; } diff --git a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php index 7b2065b1..a1e6571a 100644 --- a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php +++ b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php @@ -426,30 +426,21 @@ function () { } /** - * Delete an Order + * Change an order status to "Cancelled" * * @since 1.0.1 * * @param int $order_id - * @param bool $force * * @return void */ - public function delete( int $order_id, bool $force = false ): void { + public function cancel( int $order_id ): void { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } - $order->delete( $force ); - wp_delete_post( $order->get_id(),$force ); - - if ( $force ) { - global $wpdb; - $wpdb->delete( $wpdb->prefix . 'woocommerce_order_items', [ 'order_id' => $order_id ] ); - $wpdb->delete( $wpdb->prefix . 'wc_orders_meta', [ 'order_id' => $order_id ] ); - $wpdb->delete( $wpdb->prefix . 'wc_orders', [ 'id' => $order_id ] ); - } + $order->update_status( 'cancelled' ); } }