Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#928] Highest Bid Issue #929

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion client-mu-plugins/goodbids/src/classes/Auctions/Auction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) ) {
Expand Down
30 changes: 28 additions & 2 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
}