You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Hook修改WooCommerce Bookings夜间超额时长定价规则

解决方案:PluginHive WooCommerce Bookings夜间时段计费规则定制

实现逻辑

将预订总时段拆分为三个区间分别计算费用,核心规则如下:

  • 非夜间时段(22:00前/10:00后):按实际预订时长的时段块计费,每30分钟50美元
  • 夜间时段(22:00-10:00):若时长超过6小时,仅按6小时(12个时段块)计费;未超过则按实际时长计算
  • 订单总规则:若整个订单总时长不足3小时,强制按最低300美元计费

自定义代码实现

将以下代码添加到你的WordPress主题functions.php文件或自定义功能插件中:

add_filter('pluginhive_booking_final_cost', 'custom_night_time_booking_cost', 10, 3);
function custom_night_time_booking_cost($final_cost, $booking_data, $product) {
    // 基础配置参数
    $base_hours = 3;
    $base_cost = 300;
    $block_minutes = 30;
    $per_block_cost = 50;
    $night_start = 22;
    $night_end = 10;
    $max_night_charge_hours = 6;

    // 获取预订时间戳与总时长
    $start_time = strtotime($booking_data['start']);
    $end_time = strtotime($booking_data['end']);
    $total_minutes = ($end_time - $start_time) / 60;
    $total_hours = $total_minutes / 60;

    // 处理跨天夜间时段的时间戳
    $current_date_start = date('Y-m-d', $start_time);
    $current_date_end = date('Y-m-d', $end_time);
    $night_start_ts = strtotime("{$current_date_start} {$night_start}:00");
    $night_end_ts = strtotime("{$current_date_end} {$night_end}:00");
    
    if ($night_end < $night_start && $night_end_ts <= $start_time) {
        $night_end_ts = strtotime('+1 day', $night_end_ts);
    }

    // 拆分三个区间的时长(分钟)
    $pre_night_minutes = max(0, min($night_start_ts, $end_time) - $start_time) / 60;
    $night_minutes = max(0, min($night_end_ts, $end_time) - max($night_start_ts, $start_time)) / 60;
    $post_night_minutes = max(0, $end_time - max($night_end_ts, $start_time)) / 60;

    // 单区间费用计算辅助函数
    function calculate_block_cost($minutes, $block_minutes, $per_block_cost) {
        if ($minutes <= 0) return 0;
        $total_blocks = ceil($minutes / $block_minutes);
        return $total_blocks * $per_block_cost;
    }

    // 计算各区间费用
    $pre_night_cost = calculate_block_cost($pre_night_minutes, $block_minutes, $per_block_cost);
    $billable_night_minutes = min($night_minutes, $max_night_charge_hours * 60);
    $night_cost = calculate_block_cost($billable_night_minutes, $block_minutes, $per_block_cost);
    $post_night_cost = calculate_block_cost($post_night_minutes, $block_minutes, $per_block_cost);

    // 应用订单最低费用规则
    $subtotal = $pre_night_cost + $night_cost + $post_night_cost;
    $final_cost = ($total_hours < $base_hours) ? max($subtotal, $base_cost) : $subtotal;

    return $final_cost;
}

代码说明

  1. 时段拆分:自动识别跨天场景,精准拆分预订时长为三个区间,确保夜间时段计算准确
  2. 夜间上限控制:夜间时长超过6小时时,自动截断为6小时计费,严格匹配需求规则
  3. 基础规则兼容:兼顾「不足3小时按300美元计费」的要求,同时不影响长时段的拆分计费逻辑
  4. 零成本容错:无时长的区间自动计0费用,避免产生不必要的额外收费

示例验证

  • 场景1:23:00-03:00(4小时):夜间时长4小时<6小时,按8个时段块计费,总费用=8×50=400美元
  • 场景2:23:00-06:00(7小时):夜间时长7小时>6小时,按12个时段块计费,总费用=12×50=600美元
  • 场景3:21:00-11:00(14小时):
    • 21:00-22:00:2个时段块,费用=2×50=100美元
    • 22:00-10:00:按6小时12个时段块计费,费用=12×50=600美元
    • 10:00-11:00:2个时段块,费用=2×50=100美元
    • 总费用=100+600+100=800美元

内容的提问来源于stack exchange,提问作者Itay Maor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:37:03