Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active June 29, 2022 10:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jany-M/7067af91e67adef69e44 to your computer and use it in GitHub Desktop.
Save Jany-M/7067af91e67adef69e44 to your computer and use it in GitHub Desktop.
[WordPress] [WooCommerce] [Bookings] Auto-Confirm Booking upon Order Complete
<?php
add_action( 'woocommerce_order_status_completed', 'mark_confirm_bookings', 20, 1 );
function mark_confirm_bookings( $order_id ) {
global $wpdb;
$order = new WC_Order( $order_id );
$bookings = array();
foreach ( $order->get_items() as $order_item_id => $item ) {
if ( 'line_item' == $item['type'] ) {
if ( !wc_booking_requires_confirmation( $item['product_id'] ) ) {
$bookings = array_merge( $bookings, $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_booking_order_item_id' AND meta_value = %d", $order_item_id ) ) );
}
}
}
foreach ( $bookings as $booking_id ) {
$booking = get_wc_booking( $booking_id );
$booking->update_status('confirmed');
}
}
?>
@rodsan27
Copy link

Same issue with email being sent twice, I tried the method that @mzykin wrote, I deactivate the product as virtual, I change the function and when I perform the test the client's mail is not sent... Any suggestion?

@Xyala
Copy link

Xyala commented Mar 10, 2022

Not totally related, but I was wondering how the opposite could be done.
Assuming a booking status changes to complete, and I'd want the order with that booking to also complete ?

@Jany-M
Copy link
Author

Jany-M commented Mar 14, 2022

Sorry guys... just saw all the comments (Github not sending me any notifications about comments on gists).
Keep in mind the code is from 2015, so things may have changed since then (actually, most likely, hence my snippet is not really compatible anymore).
Also, I have not used the Bookings plugin in years, so I can't really be helpful anymore about it.
If the email is now sending twice, maybe uncheck the "processing" email and only leave the completed one to be sent? or viceversa?
@mzykin About setting the product as non-virtual, not sure that's recommended, since it's a Booking and not a physical product?
@Xyala Sorry, I really wouldn't know, haven't used that plugin in years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment