Last active
August 7, 2023 15:06
-
-
Save marekale/d21140263e01b9571acb74ec944f2be8 to your computer and use it in GitHub Desktop.
By design WooCommerce only autocompletes orders for downloadable and virtual products. This snippet autocompletes order which has only bookable products with all bookings completed, and has status processing..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class MaraleAutocompleteOrders { | |
public static function wc_booking_complete_action11( $booking_id ) { | |
if ( 'wc-booking-complete' !== current_action() ) { | |
return; | |
} | |
$booking = get_wc_booking( $booking_id ); | |
if ( 'complete' === $booking->get_status() ) { | |
$order_id = $booking->get_order_id(); | |
static::maybe_complete_order($order_id); | |
} | |
} | |
public static function maybe_complete_order( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
if ( $order->has_status( 'processing' ) ) { | |
$completed_bookings_order = null; | |
$booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_id( $order_id ); | |
$count = count( $order->get_items() ); | |
if ( $count > 0 && $count === count( $booking_ids ) ) { | |
foreach ( $booking_ids as $id ) { | |
$booking = get_wc_booking($id); | |
$completed_bookings_order = is_object($booking) ? | |
$booking->has_status('complete') : FALSE; | |
if ( ! $completed_bookings_order ) { | |
break; | |
} | |
} | |
} | |
if ( $completed_bookings_order ) { | |
$order->update_status('completed'); | |
return 'completed'; | |
} | |
} | |
return $order->get_status(); | |
} | |
public static function maybe_complete_all_orders() { | |
$processing_orders = wc_get_orders( array( | |
'status' => 'processing', | |
'return' => 'ids', | |
'posts_per_page' => -1 ) ); | |
foreach ( $processing_orders as $order_id ) { | |
static::maybe_complete_order($order_id); | |
} | |
} | |
} | |
add_action( 'wc-booking-complete', array( MaraleAutocompleteOrders::class, 'wc_booking_complete_action11' ), 11, 1 ); | |
//MaraleAutocompleteOrders::maybe_complete_all_orders(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment