Most online business that deals with perishable or time sensitive goods would complete orders at the end of the day. This customization is useful for such businesses that would offer same day delivery and want to mark all orders as completed within a certain timeframe.
Solution: Change Order Status Only Within Certain Timeframes
The code snippet will update all status from processing to completed when it falls within the specified date and timeframe of 8:00 AM to 03:00 PM.
add_action( 'woocommerce_order_status_changed', 'ts_mark_order_complete_if_within_timeframe', 10, 4 ); /* * This function updates orders to completed if the current time is within the specified timeframe. */ function ts_mark_order_complete_if_within_timeframe( $order_id, $from_status, $to_status, $order ) { // Set the timezone date_default_timezone_set('Asia/Kolkata'); // Define your specific timeframe $start_time = strtotime('2024-04-01 08:00:00'); // Start time: April 1, 2024, 8:00 AM $end_time = strtotime('2024-04-24 15:00:00'); // End time: April 20, 2024, 1:00 PM $current_time = strtotime("now"); // Check if the current time is within the specified timeframe and the order status is 'processing' if ($current_time >= $start_time && $current_time < $end_time && $to_status === 'processing') { // Update order status to 'completed' $order->update_status( 'completed' ); } }
Output
All orders that have been successfully transitioned to the ‘processing’ status within the designated timeframe are automatically marked as completed.
Similarly, you can also automatically complete WooCommerce orders when they go to the processing status irrespective of date and time range.