Last active
August 7, 2023 15:09
-
-
Save jessepearson/a16b3d3c20cea27fdc6a27c7cec1682d to your computer and use it in GitHub Desktop.
Automatically create a follow up booking when a booking is paid for in WooCommerce Bookings.
This file contains 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 // do not copy this line | |
/** | |
* Code goes in theme functions.php | |
* | |
* In this example we're creating a booking 1 week after a booking is paid for. | |
* This does not create another order or payment, just an additional booking. | |
* $exact is false meaning if our slot is taken, the next available slot will be used. | |
* @link https://docs.woocommerce.com/document/creating-bookings-programatically/ | |
*/ | |
function auto_create_followup_booking( $booking_id ) { | |
// Get the previous booking from the ID | |
$prev_booking = get_wc_booking( $booking_id ); | |
// Don't want follow ups for follow ups | |
if ( $prev_booking->get_parent_id() <= 0 ) { | |
// Set the follow up data | |
$new_booking_data = array( | |
'start_date' => strtotime( '+1 week', $prev_booking->get_start() ), // same time, 1 week on | |
'end_date' => strtotime( '+1 week', $prev_booking->get_end() ), // same time, 1 week on | |
'resource_id' => $prev_booking->get_resource_id(), // same resource | |
'parent_id' => $booking_id, // set the parent | |
); | |
// Did the previous booking have persons? | |
$persons = $prev_booking->get_persons(); | |
if ( is_array( $persons ) && 0 < count( $persons ) ) { | |
$new_booking_data['persons'] = $persons; | |
} | |
// Was the previous booking all day? | |
if ( $prev_booking->is_all_day() ) { | |
$new_booking_data['all_day'] = true; | |
} | |
create_wc_booking( | |
$prev_booking->get_product_id(), // Creating a booking for the previous bookings product | |
$new_booking_data, // Use the data pulled above | |
$prev_booking->get_status(), // Match previous bookings status | |
false // Not exact, look for a slot | |
); | |
} | |
} | |
add_action( 'woocommerce_booking_in-cart_to_paid', 'auto_create_followup_booking' ); | |
add_action( 'woocommerce_booking_unpaid_to_paid', 'auto_create_followup_booking' ); | |
add_action( 'woocommerce_booking_confirmed_to_paid', 'auto_create_followup_booking' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment