how to do a processing Orders into Complete after x Minutes or x hour in WooCommerce

Question

I have tried this code its works only one time. help me and check where I am wrong. code goes in child theme function.php

    add_action( 'woocommerce_order_status_changed', 'hourly_update_status_orders', 10, 4 );
function hourly_update_status_orders( $order_id, $old_status, $new_status, $order ) {
    // Enable the process to be executed daily
    if( in_array( $old_status, array('processing') ) 
        && get_option( 'paid_orders_hourly_process' ) < time() ) :

    $hours_delay = 1; // <=== SET the delay (number of hours to wait before completed)

    $one_hour    = 10 * 60;
    $current_hour      = strtotime( date('Y-m-d H:00:00') );
    $current_time = $current_time - $current_time % (10 * 60);
    // Get paid orders (10 mints old)
    $paid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'processing',
        'date_created' => '<' . ( $current_hour - ($hours_delay * $one_hour) ),
    ) );

    if ( sizeof($paid_orders) > 0 ) {
        $completed_text = __("The order is Completed.", "woocommerce");

        // Loop through WC_Order Objects
        foreach ( $paid_orders as $paid_order ) {
            $order->update_status( 'completed', $complted_text );
        }
    }
    // Schedule the process to the next day (executed once restriction)
    update_option( 'paid_orders_hourly_process', $current_hour + $one_hour );

    endif;
}
0
Muhammad Waqas 2 years 2020-12-31T11:10:20-05:00 0 Answers 3 views 0

Leave an answer

Browse
Browse