mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-02 12:02:25 +08:00
https://stackoverflow.com/questions/78567928/auto-complete-woocommerce-processing-orders-with-bookings-which-start-date-is-pa
51 lines
1.8 KiB
Text
51 lines
1.8 KiB
Text
// Define a custom schedule time for Cron schedules tasks
|
|
add_filter( 'cron_schedules','hourly_cron_schedule' );
|
|
function hourly_cron_schedule( $schedules ) {
|
|
if ( ! isset( $schedules["hourly"] ) ) {
|
|
$schedules["hourly"] = array(
|
|
'interval' => HOUR_IN_SECONDS,
|
|
'display' => __( 'Hourly' )
|
|
);
|
|
}
|
|
return $schedules;
|
|
}
|
|
|
|
// Define the scheduled task
|
|
add_action('init', 'my_custom_schedule_task');
|
|
function my_custom_schedule_task() {
|
|
// Schedule an action (if it's not already scheduled)
|
|
if ( ! wp_next_scheduled( 'scheduled_complete_processing_orders' ) ) {
|
|
wp_schedule_event( time(), 'hourly', 'scheduled_complete_processing_orders' );
|
|
}
|
|
}
|
|
|
|
|
|
// Function to run on the scheduled event
|
|
add_action( 'scheduled_complete_processing_orders', 'complete_processing_orders_with_bookings' );
|
|
function complete_processing_orders_with_bookings() {
|
|
// Get all processing orders
|
|
$orders = wc_get_orders( array(
|
|
'type' => 'shop_order',
|
|
'limit' => -1,
|
|
'status' => 'processing',
|
|
) );
|
|
|
|
// Lopp through processing orders
|
|
foreach ( $orders as $order ) {
|
|
// Get Bookings (IDs) from the order
|
|
$booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_id( $order->get_id() );
|
|
|
|
// Only orders with bookings
|
|
if( $booking_ids ) {
|
|
// Loop through bookings within the order
|
|
foreach ( $booking_ids as $booking_id ) {
|
|
$booking = new WC_Booking( $booking_id ); // Get the WC_Booking object
|
|
|
|
// If start date has passed
|
|
if ( strtotime( $booking->get_start_date() ) > time() ) {
|
|
$order->update_status( 'completed' );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|