skip to Main Content

I used a hook on my product page and added a function to add 10 minutes (600 seconds) of bidding time when any bid has been placed.

add_action( 'woocommerce_simple_auctions_outbid', 'woocommerce_simple_auctions_extend_time', 50 );
add_action( 'woocommerce_simple_auctions_proxy_outbid', 'woocommerce_simple_auctions_extend_time', 50 );
 
function woocommerce_simple_auctions_extend_time($data){
    $product = get_product( $data['product_id'] );
    if ('auction' === $product->get_type() ){
        $date1 = new DateTime($product->get_auction_dates_to());
        $date1->add(new DateInterval('PT600S'));
        update_post_meta( $data['product_id'], '_auction_dates_to', $date1->format('Y-m-d H:i:s') );
    }
}

but when customer bid, it will increase 10 min by anytime(I don’t want the bid time increase to a huge number), so I probably need to set a limit that this code only effect when bid time less than 3 minutes.

Edit:

I am changing the code to the limit time with an if validation, but it still add the time when the countdown more than 3 minute:

function woocommerce_simple_auctions_extend_time($data){
    $product = get_product( $data['product_id'] );
    if ('auction' === $product->get_type() ){
        //check if time shorter than 3 min
        $date1 = new DateTime($product->get_auction_dates_to());
        $current_dt = new DateTime('NOW');
        $interval= date_diff($current_dt, $date1);
        $remaining_seconds = $interval->format("%s");
        
        if($remaining_seconds < 180) {
        // add time
        $date1->add(new DateInterval('PT600S'));
        update_post_meta( $data['product_id'], '_auction_dates_to', $date1->format('Y-m-d H:i:s') );
        }
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I used current_time(timestamp) to capture the current time, and used strtotime('+5 minutes', $time) to compare with the auction deadline.

    Here is the code that I can run successfully:

    $time = current_time('timestamp' );
             $timeplus5 = date('Y-m-d H:i:s', strtotime('+5 minutes', $time));
     
             if ($timeplus5> $auctionendformat) {
                 $auctionend->add(new DateInterval('PT600S'));
                 update_post_meta( $data['product_id'],'_auction_dates_to', $auctionend->format('Y-m-d H:i:s') );
    

  2. You can take the product end time $date1 and run a difference against another datetime object for the current time and check if the difference is less than 3 minutes.

    If it’s less than 3 minutes then add your 10 minutes to the end time for the product.

    $current_dt = new DateTime('NOW');
    $interval= date_diff($current_dt, $date1);
    $remaining_seconds = $interval->format("%s");
    
    if($remaining_seconds < 300) {
      // add time
    }
    

    PHP DateTime::diff

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search