Skip to content

Instantly share code, notes, and snippets.

@thisissandip
Last active July 11, 2023 09:18
Show Gist options
  • Save thisissandip/a0137018c26f7c16c18f35ed56d1027c to your computer and use it in GitHub Desktop.
Save thisissandip/a0137018c26f7c16c18f35ed56d1027c to your computer and use it in GitHub Desktop.
WooCommerce Bookings: Keep the booking in cart if the order fails
/*
Removes "schedule_failed_order_event" handler which marks the booking as unpaid after
an order fails and expires the booking after an hour.
In place of the above handle, the below snippet adds a custom handler that updates the booking status to "in-cart"
such that customer can retry payment for the booking.
*/
remove_action('woocommerce_order_status_failed', 'schedule_failed_order_event');
add_action('woocommerce_order_status_failed', 'handle_failed_order_event_for_bookings');
function handle_failed_order_event_for_bookings( $order_id ){
$booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_id( $order_id );
if ( empty( $booking_ids ) ) {
return;
}
$args = array(
'order_id' => $order_id,
'booking_ids' => $booking_ids,
);
$order = wc_get_order( $order_id );
if ( ! is_a( $order, 'WC_Order' )
|| 'failed' !== $order->get_status()
|| empty( $booking_ids ) ) {
return;
}
foreach ( $booking_ids as $booking_id ) {
$booking = get_wc_booking( $booking_id );
$booking->set_status( 'in-cart' );
$booking->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment