based on Automatically cancel order after X days if no payment in WooCommerce answer code, I have make some changes trying to autocomplete paid orders after x Minutes (or x hours):
add_action( 'woocommerce_order_status_changed', 'hourly_update_status_orders', 10, 4 );
function hourly_update_status_orders( $order_id, $old_status, $new_status, $order ) {
// Set Your shop time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/London');
// Enable the process to be executed daily
if( in_array( $new_status, array('processing') )
&& get_option( 'paid_orders_hourly_process' ) < time() ) :
$time_delay = 60 * 5; // <=== SET the time delay (here 1 hour)
$current_time = strtotime(str_replace(':','-', date('Y-m-d h:i:s') ));
$targeted_time = $current_time - $time_delay;
// Get paid orders (10 mints old)
$paid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'processing',
'date_created' => '<' . $targeted_time,
'return' => 'ids',
) );
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_time + $time_delay );
endif;
}
I can only make it work for a daily change, but not for an hourly change…
Could someone help me to check where I am wrong?