php - Automatically remove a canceled order in Woocommerce

Been looking around on this website and the internet but can't seem to find a solution. My client wants every order

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745669010a4639290.html

相关推荐

  • php - Automatically remove a canceled order in Woocommerce

    Been looking around on this website and the internet but can't seem to find a solution. My client wants every order

    21天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信