I'm setting up WooCommerce bookings with the WooCommerce Product Add-Ons plugin to make a bookable product with pickup and return time.
It works pretty well but the only issue is the corresponding price will not automatically update when the customer will change the time which is made with WooCommerce Product Add-Ons plugin.
It only updates when the booking calendar is changed. Here is the custom code.
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);