在 Woocommerce 预订中使用过滤器挂钩更改持续时间

Change the duration using a filter hook in Woocommerce bookings

woocommerce_booking_form_get_posted_data 挂钩如何工作以及如何使用它来更改已在购物车 中的每个产品的预订期限?在 Internet 上找不到任何相关信息。

你能重新计算这个挂钩的持续时间吗?什么时候解雇?更改此挂钩的持续时间是否会重新计算购物车中的产品价格以及在继续处理订单后的价格? 任何有关它的信息将不胜感激!

这是一个示例,将向您展示如何使用 woocommerce_booking_form_get_posted_data 过滤器挂钩更改 Woocommerce 预订中的持续时间:

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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

But it will not change the price, as this need to be done in another way and in another hook.