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');
}
}
?>
@onkstudio
Copy link

Hi, thanks for this code !
I have a problem, using this code the booking status is effectively changed to "confirmed" but the confirmation e-mail is sent twice to the client (identical emails). Did you experience anything like that using this modification ?
I tried several things but can't figure out why it is sent twice...
Thanks a lot !

@NikGru
Copy link

NikGru commented Nov 9, 2017

Hi Guys,
unfortunately the booking status of my bookable products didn't changed - Have anybody an idea why?

@eggdesign
Copy link

Same issue here with email being sent twice. Any fix for this? Thanks

@dedicatedmoves
Copy link

I am having the same issue sending email twice. I have woocommerce deposits add on. Not sure if that is reason. I change the orders from partial payments to confirmed bookings. In turn the order is sent to customer, "product" has been confirmed (Order "____"). Did you manage to resolve the issue? I see this thread is from the end of 2017. Thank you in advance for any input on this matter.

@mzykin
Copy link

mzykin commented Jul 12, 2018

I ran into the same issue with two emails being sent for confirmed bookings.

After testing and tweaking, I think I found the sweet spot. I had the Booking product set to "Virtual" to automatically change the order status to confirmed after checkout. I think this is where the issue might have been stemming from--I'm not a PHP expert, but it almost seems like the above code + automatically changing the status to complete was tricking Woocommerce into updating the Bookings status twice, thus triggering two emails (I could be completely wrong, this is just a guess!)

So what I did was disable the "Virtual" option on the product (doesn't matter to me since I'm not collecting payment--I can go back and complete all the orders later. What matters to me is that the bookings are confirmed automatically), then I tweaked the code a bit:

From:

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();

// and so on

To:

add_action( 'woocommerce_order_status_processing', 'mark_confirm_bookings', 20 );
	
	function mark_confirm_bookings( $order_id ) {
	global $wpdb;
	$order = wc_get_order( $order_id );
	$bookings = array();

// and so on

Now customers get one email for order processing and one email for a confirmed appointment. I hope this helps anyone that's running into the same issue!

@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