I have made customizations to WooCommerce Bookings And Appointments plugin, so when a customer books a particular date, the plugin automatically adds a second appointment one week after the first one. Now I need the code snipped to register three appointments one week from each other when customers make the first appointment.
When customers book an appointment on monday, november 9th, this snipped registers a follow up appointment on monday, november 16th.
// Set the follow up data
$new_appointment_data = array(
'start_date' => strtotime( '+1 week', $prev_appointment->get_start() ), // same time, 1 week on
'end_date' => strtotime( '+1 week', $prev_appointment->get_end() ), // same time, 1 week on
'staff_ids' => $prev_appointment->get_staff_ids(), // same staff
'parent_id' => $appointment_id, // set the parent
);
Now, I can't figure out how to add a third week. This would be an apointment on monday, november 23th.
Edit
This is the code snipped I am using to make one additional appointment:
add_action( 'woocommerce_appointment_in-cart_to_paid', 'auto_create_followup_appointment' );
add_action( 'woocommerce_appointment_unpaid_to_paid', 'auto_create_followup_appointment' );
add_action( 'woocommerce_appointment_confirmed_to_paid', 'auto_create_followup_appointment' );
function auto_create_followup_appointment( $appointment_id ) {
// Get the previous appointment from the ID
$prev_appointment = get_wc_appointment( $appointment_id );
// Don't want follow ups for follow ups
if ( $prev_appointment->get_parent_id() <= 0 ) {
// Set the follow up data
$new_appointment_data = array(
'start_date' => strtotime( '+1 week', $prev_appointment->get_start() ), // same time, 1 week on
'end_date' => strtotime( '+1 week', $prev_appointment->get_end() ), // same time, 1 week on
'staff_ids' => $prev_appointment->get_staff_ids(), // same staff
'parent_id' => $appointment_id, // set the parent
);
// Was the previous appointment all day?
if ( $prev_appointment->is_all_day() ) {
$new_appointment_data['all_day'] = true;
}
create_wc_appointment(
$prev_appointment->get_product_id(), // Creating a appointment for the previous appointments product
$new_appointment_data, // Use the data pulled above
$prev_appointment->get_status(), // Match previous appointments status
false // Not exact, look for next available slot
);
}
}
But I need to create another one a week after the first one. Any help is welcome.
To make a second additional appointment separated from 2 weeks, you can try use the following:
add_action( 'woocommerce_appointment_in-cart_to_paid', 'auto_create_followup_appointment' );
add_action( 'woocommerce_appointment_unpaid_to_paid', 'auto_create_followup_appointment' );
add_action( 'woocommerce_appointment_confirmed_to_paid', 'auto_create_followup_appointment' );
function auto_create_followup_appointment( $appointment_id ) {
// Get the previous appointment from the ID
$prev_appointment = get_wc_appointment( $appointment_id );
// Don't want follow ups for follow ups
if ( $prev_appointment->get_parent_id() <= 0 ) {
// 1. First additional apointment (+1 week) - Set the follow up data
$new_appointment_data_1 = array(
'start_date' => strtotime( $prev_appointment->get_start() . '+ 1 week' ), // same time, 1 week on
'end_date' => strtotime( $prev_appointment->get_end() . '+ 1 week' ), // same time, 1 week on
'staff_ids' => $prev_appointment->get_staff_ids(), // same staff
'parent_id' => $appointment_id, // set the parent
);
// Was the previous appointment all day?
if ( $prev_appointment->is_all_day() ) {
$new_appointment_data_1['all_day'] = true;
}
create_wc_appointment(
$prev_appointment->get_product_id(), // Creating a appointment for the previous appointments product
$new_appointment_data_1, // Use the data pulled above
$prev_appointment->get_status(), // Match previous appointments status
false // Not exact, look for next available slot
);
// 2. Second additional apointment (+2 weeks) - Set the follow up data
$new_appointment_data_2 = array(
'start_date' => strtotime( $prev_appointment->get_start() . '+ 2 weeks' ), // same time, 2 weeks on
'end_date' => strtotime( $prev_appointment->get_end() . '+ 2 weeks' ), // same time, 2 weeks on
'staff_ids' => $prev_appointment->get_staff_ids(), // same staff
'parent_id' => $appointment_id, // set the parent
);
// Was the previous appointment all day?
if ( $prev_appointment->is_all_day() ) {
$new_appointment_data_2['all_day'] = true;
}
create_wc_appointment(
$prev_appointment->get_product_id(), // Creating a appointment for the previous appointments product
$$new_appointment_data_2, // Use the data pulled above
$prev_appointment->get_status(), // Match previous appointments status
false // Not exact, look for next available slot
);
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.