wordpresswoocommercehook

Change the status of orders automatically


I need to change the status of my orders to (Cancelled) automatically after 7 days if it is not processed.

I need to change the status from: New Request to Cancelled. Or I need to change the status from: Processing to Cancelled.

Thanks Adnan


Solution

  • I did that with the help of this code:

    function autocancel_wc_orders(){
        $query = ( array(
            'limit'   => 10,
            'orderby' => 'date',
            'order'   => 'DESC',
            'status'  => array( 'wc-pending', 'wc-ywraq-new', 'wc-ywraq- 
            pending')
        ) );
        $orders = wc_get_orders( $query );
        foreach( $orders as $order ){       
    
            $date     = new DateTime( $order->get_date_created() );
            $today    = new DateTime();
            $interval = $date->diff($today);
    
            $datediff = $interval->format('%a');
    
            if( $datediff > 2 ){
                $order->update_status('cancelled', 'Cancelled for missing 
                payment');
            }   
        }
    } 
    add_action( 'admin_init', 'autocancel_wc_orders' );
    

    I found this answer online in the link: https://samuelsilva.pt/cancel-woocommerce-not-paid-orders-automatically/