How does woocommerce_booking_form_get_posted_data
hook work and how to use it to change a booking duration for each product already in cart? Couldn't find any information about it on the internet.
Can you do duration recalculations on this hook? When is it fired? Will changing the duration on this hook recalculate the price of products in the cart and also after proceeding with the order? Any kind of information about it would be appreciated!
Here is an example that will show you how to make duration changes in Woocommerce bookings using woocommerce_booking_form_get_posted_data
filter hook:
add_filter( 'woocommerce_booking_form_get_posted_data', 'filter_booking_form_get_posted_data', 10, 3 );
function filter_booking_form_get_posted_data( $posted_data, $product, $duration_length ) {
## --- Settings :: New duration --- ##
$new_start_date ='2018-05-08 00:00:00'; // new start date
$new_duration = 6; // new duration
$duration_unit = __('day', 'woocommerce'); // (if needed)
## --- Calculations and changes --- ##
$day_time = 86400;
$new_start_time = strtotime($new_start_date);
$new_end_time = $new_start_time + ( $day_time * $new_duration ) - 1;
$posted_data['_year'] = date('Y', $new_start_time);
$posted_data['_month'] = date('n', $new_start_time);
$posted_data['_day'] = date('j', $new_start_time);
$posted_data['_date'] = date('Y-n-j', $new_start_time);
$posted_data['date'] = date('M j, Y', $new_start_time);
$posted_data['_start_date'] = $new_start_time;
$posted_data['_end_date'] = $new_end_time;
$posted_data['_duration'] = $new_duration;
$posted_data['duration'] = sprintf( _n( '%s day', '%s days', $new_duration, 'woocommerce' ), $new_duration );
return $posted_data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But it will not change the price, as this need to be done in another way and in another hook.