Created
January 2, 2026 13:06
-
-
Save thisissandip/f76ad372b411fbcd7e13cbb75b69a7f1 to your computer and use it in GitHub Desktop.
Cancel WooCommerce Bookings when an order status changes from Pending to Failed.
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
| /** | |
| * Cancel WooCommerce Bookings when an order status changes from Pending to Failed. | |
| */ | |
| add_action('woocommerce_order_status_changed', 'wc_cancel_bookings_on_failed_order', 10, 4); | |
| function wc_cancel_bookings_on_failed_order($order_id, $from_status, $to_status, $order) { | |
| // Check if the transition is from 'pending' to 'failed' | |
| if ($from_status === 'pending' && $to_status === 'failed') { | |
| // Ensure the Bookings classes exist | |
| if (!class_exists('WC_Booking_Data_Store')) { | |
| return; | |
| } | |
| // Get all bookings associated with this order | |
| $booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_id($order_id); | |
| if (!empty($booking_ids)) { | |
| foreach ($booking_ids as $booking_id) { | |
| $booking = new WC_Booking($booking_id); | |
| // Update booking status to cancelled | |
| // This also triggers the availability to be released | |
| $booking->update_status('cancelled'); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment