$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Automatically remove a canceled order in Woocommerce|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Automatically remove a canceled order in Woocommerce

matteradmin17PV0评论

Been looking around on this website and the internet but can't seem to find a solution. My client wants every order which has the order status cancelled to be completely removed out of WooCommerce after an amount of time.

<?php
function update_order_status( $order_id ) {
$order = new WC_Order( $order_id );
$order_status = $order->get_status();

if ('cancelled' == $order_status || 'failed' == $order_status ||   'pending' == $order_status ) {    
        wp_delete_post($order_id,true);    
   }    


}

I currently have the above code snippet but I want this action to be delayed 5 minutes because pending orders could be still in payment.

So TL;DR Orders with status 'cancelled', 'failed' & 'pending' should be completely deleted after 5 minutes.

Anyone who could help me out on this one?

Best regards, Dylan

Been looking around on this website and the internet but can't seem to find a solution. My client wants every order which has the order status cancelled to be completely removed out of WooCommerce after an amount of time.

<?php
function update_order_status( $order_id ) {
$order = new WC_Order( $order_id );
$order_status = $order->get_status();

if ('cancelled' == $order_status || 'failed' == $order_status ||   'pending' == $order_status ) {    
        wp_delete_post($order_id,true);    
   }    


}

I currently have the above code snippet but I want this action to be delayed 5 minutes because pending orders could be still in payment.

So TL;DR Orders with status 'cancelled', 'failed' & 'pending' should be completely deleted after 5 minutes.

Anyone who could help me out on this one?

Best regards, Dylan

Share Improve this question asked Nov 22, 2016 at 8:19 Dylan SmitDylan Smit 133 silver badges7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

do the following code in your child theme function.php file as given below.

function wc_remove_cancelled_status( $statuses ){
  if( isset( $statuses['wc-cancelled'] ) ){
      unset( $statuses['wc-cancelled'] );
  }
  return $statuses;
} 
add_filter( 'wc_order_statuses', 'wc_remove_cancelled_status' );

Hmm... If we use your script, I think if you save the time like:

<?php
function update_order_status( $order_id ) {
$order = new WC_Order( $order_id );
$order_status = $order->get_status();

if ('cancelled' == $order_status || 'failed' == $order_status ||   'pending' == $order_status ) { 
        $current_time = date('h:i:s');    /* this is not necessary - not being used. */

        sleep(300);       // 300 seconds in 5 minutes

        wp_delete_post($order_id,true);    
   }    


}

Look, I don't know if this will work, but it's worth a try.

I think the user can can change the order status within five minutes. So I wrote the below code with hook-

add_action( 'woocommerce_order_status_failed', 'the_dramatist_woocommerce_auto_delete_order' );
add_action( 'woocommerce_order_status_pending', 'the_dramatist_woocommerce_auto_delete_order' );
add_action( 'woocommerce_order_status_cancelled', 'the_dramatist_woocommerce_auto_delete_order' );

function the_dramatist_woocommerce_auto_delete_order( $order_id ) {
    // 5*60 = 300 seconds. Here 1minute = 60 seconds.
    wp_schedule_single_event(tim() + 300, 'the_dramatist_main_delete_event', $order_id);
}

function the_dramatist_main_delete_event( $order_id ) {
    global $woocommerce;
    $order = new WC_Order( $order_id );
    $order_status = $order->get_status();
    if ( !$order_id )
        return false;
    if ('cancelled' == $order_status || 'failed' == $order_status ||   'pending' == $order_status ) {
        wp_delete_post($order_id,true);
        return true;
    }
    return false;
}

Here we are detecting order status change by hook and checking the order status again after wake from sleep. So if the user change the order status with in five minutes then the delete will not occur. Please test it. I've not tested it. Hope that help you.

P.S. I think sleep() function will cause some delay in the WordPress life cycle. So better we use wp_schedule_single_event function. So I updated my code.

Post a comment

comment list (0)

  1. No comments so far