0

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!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
James
  • 109
  • 12
  • You can find all the available woocommerce hooks [here](https://docs.woocommerce.com/document/bookings-action-and-filter-reference/). `woocommerce_booking_form_get_posted_data` is a `filter`, you can use it to alter the displayed information of the booking form. As you didn't specify exactly when you want to alter your data, you should see the `Action` section of the documentation to find the hook that suits you. Maybe `woocommerce_before_booking_object_save`? – Nicolas Cami Apr 23 '18 at 08:42

1 Answers1

3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, thank you for your reply. It does work and it's well written, thanks! but how do i use it to change the start date and duration to products that are already in cart? As I need to change the booking duration for all the items in already in cart, **when adding a new booking to the cart**. Every new product added to the cart should divide the total booking duration for each product in cart by the number of items in cart. Thank you in advance. – James Apr 23 '18 at 22:13
  • @James Sorry, but this code example is tested **and perfectly works.**… The hook title is very explicit and it works **on posted data** only one item by one when they are added to cart. In your question you asked how to make this hook work and my answer is answering that... So I will not go anymore further. – LoicTheAztec Apr 23 '18 at 22:21
  • @LoicTheAztec, This example was very helpful, and it seems from my testing that updating the duration does reflect in the calculation of the price. – christian Nov 11 '19 at 04:49