Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@woogists
Created March 9, 2018 16:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogists/ed563cda9a36daa04565d9ea7840ba1e to your computer and use it in GitHub Desktop.
Save woogists/ed563cda9a36daa04565d9ea7840ba1e to your computer and use it in GitHub Desktop.
[WooCommerce Bookings] Use case: Creating a one-week follow-up
/**
* 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