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

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.

  • Thank you. Just gave it a test but it doesn't seem to work. I want every cancelled or failed order to remove after 5 minutes. This will only delete this based on if the user does change the order status if i'm right? – Dylan Smit Nov 23 '16 at 9:35

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.