1

我正在使用 WooCommerce 产品附加组件插件设置 WooCommerce 预订,以制作具有取货和退货时间的可预订产品。

它工作得很好,但唯一的问题是当客户更改使用 WooCommerce 产品附加组件插件制作的时间时,相应的价格不会自动更新。

它仅在更改预订日历时更新。这是自定义代码。

add_filter( 'booking_form_calculated_booking_cost', 'wdm_add_hotel_booking', 10, 3);
function wdm_add_hotel_booking( $cost, $book_obj, $posted ) {
    $product    = wc_get_product($posted['add-to-cart'] );

    $product_id = $posted['add-to-cart'];
    $qntytxt = $product_id+1;
    $qnty    = $posted['wc_bookings_field_persons_'.$qntytxt];

    $product    = wc_get_product( $product_id );
    $rent       = $product->get_price();

    $start_date_month   = $posted['wc_bookings_field_start_date_month'];
    $start_date_day     = $posted['wc_bookings_field_start_date_day'];
    $start_date_year    = $posted['wc_bookings_field_start_date_year'];

    $to_date_month   = $posted['wc_bookings_field_start_date_to_month'];
    $to_date_day     = $posted['wc_bookings_field_start_date_to_day'];
    $to_date_year    = $posted['wc_bookings_field_start_date_to_year'];


    $time       = strtotime($start_date_month.'/'.$start_date_day.'/'.$start_date_year);
    $fromDate   = date('Y-m-d',$time);

    if($to_date_month !=""){
        $time       = strtotime($to_date_month.'/'.$to_date_day.'/'.$to_date_year);
        $ToDate     = date('Y-m-d',$time);
    }else{
        $ToDate     = $fromDate;
    }

    $pickup_time = $posted['addon-'.$product_id.'-pick-up-time-0'];
    $return_time = $posted['addon-'.$product_id.'-return-time-1'];

    if(isset($pickup_time) && ($pickup_time!="")){
        $ptimeRange = explode("-",$pickup_time);
        $ptimeRangelevel = $ptimeRange[2];
    }

    if(isset($return_time) && ($return_time!="")){
        $rtimeRange = explode("-",$return_time);
        $rtimeRangelevel = $rtimeRange[2];
    }

    if($ptimeRangelevel!="" && $rtimeRangelevel!=""){

        $ts1 = strtotime($fromDate);
        $ts2 = strtotime($ToDate);
        $timeDiff = abs($ts2 - $ts1);

        $numberDays = $timeDiff/86400;  // 86400 seconds in one day

        // and you might want to convert to integer
        $numberDays = intval($numberDays);

        if ($rtimeRangelevel<=$ptimeRangelevel){
            if($numberDays>0){                
                $ActuvalCnt = $numberDays;
                $ActualPrice = $ActuvalCnt * $qnty * $rent;


                if($rtimeRangelevel < $ptimeRangelevel) {
                    if($numberDays>1){
                        $cost = $ActualPrice;
                        $ActualPrice = $ActuvalCnt * $qnty * $rent;
                        $cost = $cost - $ActualPrice;                        
                        if ($rtimeRangelevel < $ptimeRangelevel) {
                            echo $ActualPrice = ($ActuvalCnt + 1 ) * $qnty * $rent;
                            $cost = $ActualPrice - ($rent*$qnty);                            
                        }

                    }else{                          
                        $ActualPrice = $ActuvalCnt * $qnty * $rent;
                        $cost = $cost - $ActualPrice;
                    }
                }else{
                    $ActualPrice = $ActuvalCnt * $qnty * $rent;
                    $cost = $ActualPrice;
                }    

            }
        } else {        

            $ActuvalCnt = $numberDays + 1;
            if (($ActuvalCnt > 2) && ($rtimeRangelevel != $ptimeRangelevel)){

                $ActualPrice = $ActuvalCnt * $qnty * $rent;
                $cost = $ActualPrice - $rent;                
                if ($rtimeRangelevel > $ptimeRangelevel) {
                    $cost = $ActualPrice;
                } 
            }
        }
    }

    return $cost;
}

// clear checout cache
add_filter('woocommerce_checkout_get_value','__return_empty_string', 1, 1);
4

0 回答 0