Lesson 51 Implementation: Automatically Closing Expired Auctions
One of the most important responsibilities of an auction platform is ensuring that bidding stops exactly when the auction ends. In earlier lessons, our Flipnzee Auctions plugin allowed users to place bids while the auction was active. However, there was still one significant issue—an auction could technically remain active in the database even after its scheduled end time.
In this lesson, we solved that problem by automatically closing expired auctions during the bid validation process.
The Problem
Imagine an auction scheduled to end at 15:00 UTC.
If nobody manually changes its status, the auction could continue showing as Active, allowing visitors to attempt placing bids after the deadline.
This creates several problems:
- Bids may be accepted after the auction has ended.
- Auction status becomes inaccurate.
- Administrators must manually close every auction.
- Buyers lose confidence in the auction system.
We wanted the plugin to handle this automatically.
Our Approach
Whenever a user submits a bid, the plugin now performs one additional check before accepting it:
- Retrieve the auction’s end time.
- Compare it with the current UTC time.
- If the auction has expired:
- Update its status to closed.
- Reject the bid immediately.
This ensures that expired auctions are automatically closed the first time someone interacts with them after the deadline.
Step 1: Compare the Current Time
We used PHP’s gmdate() function to generate the current UTC time and compared it with the stored auction end time.
if (
strtotime( gmdate( 'Y-m-d H:i:s' ) ) >=
strtotime( $auction->auction_end )
) {
Using UTC prevents issues caused by different server time zones.
Step 2: Update the Auction Status
If the auction has expired, we update the auction record in the database.
$wpdb->update(
$auction_table,
array(
'status' => 'closed',
),
array(
'id' => $auction_id,
),
array(
'%s',
),
array(
'%d',
)
);
This permanently marks the auction as closed.
Step 3: Reject the Bid
After closing the auction, the function immediately returns false.
return false;
This prevents any further processing and ensures no late bids are accepted.
Complete Code
The new logic added to place_bid() looks like this:
if (
strtotime( gmdate( 'Y-m-d H:i:s' ) ) >=
strtotime( $auction->auction_end )
) {
$wpdb->update(
$auction_table,
array(
'status' => 'closed',
),
array(
'id' => $auction_id,
),
array(
'%s',
),
array(
'%d',
)
);
return false;
}
Why This Design Works
Instead of relying on scheduled cron jobs or manual administration, the auction closes itself naturally whenever someone attempts to interact with it after its end time.
This approach is:
- Simple
- Reliable
- Lightweight
- Easy to maintain
It also avoids unnecessary background processes for smaller websites.
Current Limitation
While the auction now closes automatically, our frontend currently hides closed auctions from visitors.
This means that once an auction expires, its page no longer displays any auction information.
Although this successfully prevents further bidding, it isn’t the best user experience because visitors cannot see:
- the winning bidder,
- the final bid,
- or the auction history.
We’ll address this in the next lesson.
What We Learned
In this lesson, we enhanced the bidding system by introducing automatic auction closure.
Specifically, we learned how to:
- compare UTC timestamps using
gmdate()andstrtotime(), - update database records using
$wpdb->update(), - automatically change an auction’s status,
- prevent late bids,
- and improve the reliability of the auction workflow.
Next Lesson
In Lesson 52, we’ll improve the user experience by displaying completed auctions instead of hiding them. Visitors will still be able to view the final auction details, including the winning bidder, winning amount, bid history, and a clear “Auction Closed” status, while the bidding form will be disabled.
