I want to make that after paying for the subscription renewal, bookings are created until the next payment day. First, I get the user id from the subscription and the next payment day. Next, I get bookings that were made by the user and whose parent_id is greater than zero. If parent_id is greater than 0, then this is a recurring booking. In the loop, I check if the booking was made in the last week. Next comes the loop with checking that the booking time is less than the day of the next payment day. According to my idea, bookings should be created in the last loop, but this does not happen. Can you help me to create bookings after renewal?
function create_bookings_after_renewal($subscription) {
// Getting the user ID from the current subscription object
$user_id = get_post_meta($subscription->ID, '_customer_user', true);
//Get next payment date
$next_payment_day = $subscription->get_time('next_payment');
$args = array(
'posts_per_page' => -1,
'author' => $user_id,
'post_type' => 'wc_booking',
'post_status' => 'complete',
'fields' => 'ids',
'meta_query' => [
[
'key' => '_booking_parent_id',
'value' => 0,
'compare' => '>',
],
]
);
$bookings_ids = get_posts( $args );
foreach ( $bookings_ids as $recurring_booking_id ) {
$booking = new WC_Booking( $recurring_booking_id ); // Get the WC_Booking instance object
if($booking->get_start() >= strtotime("-6 days 00:00:00") && $booking->get_end() <= strtotime("+1 day 00:00:00")){
$booking_id = get_post_meta( $recurring_booking_id, '_booking_parent_id', true );
for ($i = strtotime( '+1 week', $booking->get_start()), $e = strtotime( '+1 week', $booking->get_end()) ; $i < $next_payment_day; $i = strtotime( '+1 week', $i ),$e = strtotime( '+1 week', $e )) {
// Set the follow up data
$new_booking_data = array(
'start_date' => $i,
'end_date' => $e,
'parent_id' => $booking_id, // set the parent
);
// Was the previous booking all day?
if ( $booking->is_all_day() ) {
$new_booking_data['all_day'] = true;
}
create_wc_booking(
$booking->get_product_id(), // Creating a booking for the previous bookings product
$new_booking_data, // Use the data pulled above
'paid', // bookings status
false // Not exact, look for a slot
);
}
}
}
}
add_action('woocommerce_subscription_renewal_payment_complete','create_bookings_after_renewal',10,1);