File: /mnt/data/dreamsrent-wp-demo/wp-content/themes/dreamsrent/inc/bookings/dreamsrent_booking.php
<?php
// if (!session_id()) {
// session_start();
// }
// Update cart item price
add_filter('woocommerce_product_get_price', 'dreams_woocommerce_product_get_price', 10, 2);
function dreams_woocommerce_product_get_price($price, $product) {
if (!is_a($product, 'WC_Product')) {
return $price; // Ensure $product is a valid WooCommerce product
}
if (get_post_type($product->get_id()) === 'rental') {
// Ensure WooCommerce session is initialized
// if (!WC()->session || !WC()->session->has_session()) {
// return $price;
// }
$payment_type = WC()->session->get('payment_type');
if ($payment_type === 'full') {
$final_order_total = WC()->session->get('final_order_total', 0);
$price = $final_order_total;
} elseif ($payment_type === 'deposit') {
$final_order_total = WC()->session->get('product_subtotal_cart_final', 0);
$price = $final_order_total;
}
}
return $price;
}
// Cart Image Update
add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image', 10, 3 );
function custom_new_product_image( $_product_img, $cart_item, $cart_item_key ) {
$post_id = $cart_item['product_id'];
$gallery_images = get_post_meta( $post_id, 'dreams_booking_meta_id_gallery', true );
if ( $gallery_images && is_array( $gallery_images ) && ! empty( $gallery_images ) ) {
$first_image_url = reset( $gallery_images );
$image_width = 300;
$image_height = 300;
$replacement_image = '<img src="' . esc_url( $first_image_url ) . '" width="' . esc_attr( $image_width ) . '" height="' . esc_attr( $image_height ) . '" />';
return $replacement_image;
}
return $_product_img;
}
function custom_cart_totals() {
if (isset($_POST['dsrent_pickup_date']) && isset($_POST['dsrent_pickoff_date'])) {
$pickup_date = new DateTime($_POST['dsrent_pickup_date']);
$pickoff_date = new DateTime($_POST['dsrent_pickoff_date']);
global $woocommerce;
$values = WC()->session->get('car_rental', array());
foreach ($woocommerce->cart->get_cart() as $cart_item_data) {
$date_difference = wc_get_product($cart_item_data['dsrent_car_id']);
?>
<div class="cart_totals">
<?php echo esc_html($values['date_difference']); ?>
<?php if ($values['date_difference'] > 0 && $values['date_difference'] < 2) {
// esc_html__('Day', 'dreamsrent');
} else {
esc_html__('Days', 'dreamsrent');
} ?>
</div>
<?php
}
}
}
add_action('woocommerce_cart_item_quantity', 'display_custom_cart_totals');
function display_custom_cart_totals() {
custom_cart_totals();
}
// Add a custom label before the order total in the cart totals section
add_action('woocommerce_cart_totals_after_order_total', 'custom_cart_totals_labels', 10, 3);
function custom_cart_totals_labels($cart_item) {
global $woocommerce;
global $global_product_subtotal, $global_remain_balance, $global_rental_per_fix, $global_rental_deposit, $global_payment_type, $global_product_topay, $final_total;
$rental_per_fix = $global_rental_per_fix;
$rental_deposit = $global_rental_deposit;
$payment_type = $global_payment_type;
$product_totals = array();
$car_rental_data = WC()->session->get('new_cart_rental', array());
foreach ($car_rental_data as $item_data) {
$product_id = $item_data['dsrent_car_id_new'] ?? '';
if (empty($product_id)) continue;
// var_dump($product_id);
$subtotal = floatval($item_data['product_subtotal']);
$balance = floatval($item_data['remain_balance']);
$post_id = floatval($item_data['dsrent_car_id_post']);
// var_dump($post_id);
// Accumulate totals per product
if (isset($product_totals[$product_id])) {
$product_totals[$product_id]['product_subtotal'] += $subtotal;
$product_totals[$product_id]['remain_balance'] += $balance;
} else {
$product_totals[$product_id] = array(
'product_subtotal' => $subtotal,
'remain_balance' => $balance,
);
}
}
$product_subtotal_cart = 0;
$remain_balance_cart = 0;
foreach ($product_totals as $product_id => $totals) {
$product = wc_get_product($product_id);
if ($product) {
$product_name = $product->get_name();
$product_subtotal_cart += $totals['product_subtotal'];
$remain_balance_cart += $totals['remain_balance'];
}
}
$remain_balance_cart = $final_total - $product_subtotal_cart;
// var_dump($product_subtotal_cart);
if ($rental_deposit == 'yes') {
if ($rental_per_fix == 'percent' && $payment_type == 'deposit') {
?>
<tr class="custom-label">
<?php
$amount_deposit = get_post_meta($post_id, 'dreams_booking_meta_amount_deposit', true); // Fetch meta value
// var_dump($amount_deposit);
$product_subtotal_cart = ($final_total * (float) $amount_deposit / 100);
$remain_balance_cart = $final_total - $product_subtotal_cart;
$remain_balance_cart_total = $final_total + $product_subtotal_cart;
$remain_balance_cart ='0';
?>
<th><?php echo esc_html__('To Deposit', 'dreamsrent'); ?></th>
<td data-title="To Deposit"><?php echo wc_price($product_subtotal_cart); ?></td>
</tr>
<tr class="custom-label">
<th><?php echo esc_html__('Total Amount', 'dreamsrent'); ?></th>
<td data-title="Balance"><?php echo wc_price($remain_balance_cart_total); ?></td>
</tr>
<?php
} elseif ($rental_per_fix == 'value' && $payment_type == 'deposit') {
?>
<?php
$amount_deposit = get_post_meta($post_id, 'dreams_booking_meta_amount_deposit', true); // Fetch meta value
// var_dump($amount_deposit);
$product_subtotal_cart = (float) $amount_deposit;
$remain_balance_cart = $final_total - $product_subtotal_cart;
$remain_balance_cart_total = $final_total + $product_subtotal_cart;
$remain_balance_cart ='0';
?>
<tr class="custom-label">
<th><?php echo esc_html__('To Deposit', 'dreamsrent'); ?></th>
<td data-title="To Deposit"><?php echo wc_price($product_subtotal_cart); ?></td>
</tr>
<tr class="custom-label">
<th><?php echo esc_html__('Total Amount', 'dreamsrent'); ?></th>
<td data-title="Balance"><?php echo wc_price($remain_balance_cart_total); ?></td>
</tr>
<?php
} elseif ($payment_type == 'full') {
// Handle full payment logic here if necessary
}
}
WC()->session->set('product_subtotal_cart_final', $product_subtotal_cart);
// WC()->session->set('remain_balance_cart_final', $remain_balance_cart);
WC()->session->__unset('new_cart_rental');
}
add_action('woocommerce_review_order_after_order_total', 'dreams_woocommerce_review_order_after_order_total');
function dreams_woocommerce_review_order_after_order_total($cart_item) {
global $woocommerce;
global $global_product_subtotal, $global_remain_balance, $global_rental_per_fix, $global_rental_deposit, $global_payment_type, $global_product_topay, $final_total;
$rental_per_fix = $global_rental_per_fix;
$rental_deposit = $global_rental_deposit;
$payment_type = $global_payment_type;
$product_subtotal = array(); // Initialize as array
$remain_balance = array(); // Initialize as array
$car_rental_data = WC()->session->get('new_cart_rental', array());
$product_totals = array();
$new = array();
foreach ($car_rental_data as $item_data) {
$product_id = $item_data['dsrent_car_id_new'] ?? '';
$post_id = $item_data['dsrent_car_id_post'] ?? '';
if (empty($product_id)) continue;
$subtotal = floatval($item_data['product_subtotal']);
$balance = floatval($item_data['remain_balance']);
// Calculate total cost for each product
$total_cost = $subtotal + $balance;
// Add or accumulate total cost for each product
if (isset($product_totals[$product_id])) {
$product_totals[$product_id]['product_subtotal'] += $subtotal;
$product_totals[$product_id]['remain_balance'] += $balance;
} else {
$product_totals[$product_id] = array(
'product_subtotal' => $subtotal,
'remain_balance' => $balance,
);
}
}
$product_subtotal_cart = 0; // Initialize total
$remain_balance_cart = 0; // Initialize total
foreach ($product_totals as $product_id => $totals) {
$product = wc_get_product($product_id);
if ($product) {
$product_name = $product->get_name();
$product_subtotal_cart += $totals['product_subtotal']; // Accumulate total
$remain_balance_cart += $totals['remain_balance']; // Accumulate total
}
}
// $rental_per_fix = get_post_meta($product_id, 'dreams_booking_meta_type_deposit', true);
// $rental_deposit = get_post_meta($product_id, 'dreams_booking_meta_enable_deposit', true);
// $payment_type = isset($cart_item_data['dsr_type_deposit']) ? $cart_item_data['dsr_type_deposit'] : 'deposit';
$remain_balance_cart = $final_total - $product_subtotal_cart;
if ($rental_deposit == 'yes') {
if ($rental_per_fix == 'percent' && $payment_type == 'deposit') {
?>
<?php
$amount_deposit = get_post_meta($post_id, 'dreams_booking_meta_amount_deposit', true); // Fetch meta value
// var_dump($amount_deposit);
$product_subtotal_cart = ($final_total * (float) $amount_deposit / 100);
$remain_balance_cart = $final_total - $product_subtotal_cart;
$remain_balance_cart_total = $final_total + $product_subtotal_cart;
$remain_balance_cart ='0';
?>
<tr class="custom-label">
<th><?php echo esc_html__('To Deposit', 'dreamsrent'); ?></th>
<td data-title="To Deposit"><?php echo wc_price($product_subtotal_cart); ?></td>
</tr>
<tr class="custom-label">
<th><?php echo esc_html__('Total Amount ', 'dreamsrent'); ?></th>
<td data-title="Balance"><?php echo wc_price($remain_balance_cart_total); ?></td>
</tr>
<?php
WC()->session->set('dreams_remain_balance_cart', $remain_balance_cart);
WC()->session->set('dreams_product_subtotal_cart', $final_total);
WC()->session->set('dreams_product_deposit_cart', $product_subtotal_cart);
} elseif ($rental_per_fix == 'value' && $payment_type == 'deposit') {
?>
<?php
$amount_deposit = get_post_meta($post_id, 'dreams_booking_meta_amount_deposit', true); // Fetch meta value
// var_dump($amount_deposit);
$product_subtotal_cart = (float) $amount_deposit;
$remain_balance_cart = $final_total - $product_subtotal_cart;
$remain_balance_cart_total = $final_total + $product_subtotal_cart;
$remain_balance_cart ='0';
?>
<tr class="custom-label">
<th><?php echo esc_html__('To Deposit', 'dreamsrent'); ?></th>
<td data-title="To Deposit"><?php echo wc_price($product_subtotal_cart); ?></td>
</tr>
<tr class="custom-label">
<th><?php echo esc_html__('Total Amount ', 'dreamsrent'); ?></th>
<td data-title="Balance"><?php echo wc_price($remain_balance_cart_total); ?></td>
</tr>
<?php
WC()->session->set('dreams_remain_balance_cart', $remain_balance_cart);
WC()->session->set('dreams_product_subtotal_cart', $final_total);
WC()->session->set('dreams_product_deposit_cart', $product_subtotal_cart);
}
elseif ($payment_type == 'full') {
// $product_subtotal = ($rental_price * $date_difference) + $rental_insurance + $selected_extra_services;
}
}
WC()->session->__unset('new_cart_rental');
}
function dreams_calculate_deposit_remaining_amount ($cart_item) {
global $global_product_subtotal, $global_remain_balance, $global_rental_per_fix, $global_rental_deposit, $global_payment_type, $global_rental_insurance, $global_rental_amount_deposit, $global_remain_balance;
$remain_balance_cart = WC()->session->get('dreams_remain_balance_cart');
$product_subtotal_cart = WC()->session->get('dreams_product_subtotal_cart');
$dreams_product_deposit_cart = WC()->session->get('dreams_product_deposit_cart');
if (get_post_type($cart_item['product_id']) === 'rental') {
$rental_price = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rdprice', true);
$date_difference = isset($cart_item['date_difference']) ? intval($cart_item['date_difference']) : 0;
$time_option = isset($cart_item['time-option']) ? $cart_item['time-option'] : '';
$time_difference_minutes = isset($cart_item['time_difference_hours']) ? $cart_item['time_difference_hours'] : '';
$rental_insurance = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_amount_insurance', true);
$rental_per_fix = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_type_deposit', true);
$rental_amount_deposit = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_amount_deposit', true);
$rental_deposit = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_enable_deposit', true);
$payment_type = isset($cart_item['dsr_type_deposit_hidden']) ? $cart_item['dsr_type_deposit_hidden'] : '';
$selected_extra_services = 0;
if (isset($cart_item['selected_extra_services_total'])) {
if (is_array($cart_item['selected_extra_services_total'])) {
// If it's an array, sum the values
$selected_extra_services = array_sum($cart_item['selected_extra_services_total']);
} elseif (is_numeric($cart_item['selected_extra_services_total'])) {
$selected_extra_services = floatval($cart_item['selected_extra_services_total']);
}
}
$extra_services_details = isset($cart_item['extra_services_details']) ? $cart_item['extra_services_details'] : array();
$product_subtotal = isset($cart_item['product_subtotal']) ? $cart_item['product_subtotal'] : '';
$remain_balance = isset($cart_item['remain_balance']) ? $cart_item['remain_balance'] : '';
$dsrent_car_id = isset($cart_item['dsrent_car_id']) ? $cart_item['dsrent_car_id'] : '';
$dsrent_car_id_post = isset($cart_item['dsrent_car_id_post']) ? $cart_item['dsrent_car_id_post'] : '';
if($time_option === 'day'){
if ($date_difference == '1') {
$rental_price = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rdprice', true);
} else {
// Get the serialized meta data
$serialized_data = get_post_meta($cart_item['product_id'], '_day_days', true);
if ($serialized_data) {
// Unserialize the data
$data = unserialize($serialized_data);
// Extract the day_days and day_price values
$days_prices = [];
foreach ($data as $entry) {
$days_prices[] = [
'day_days' => (int)$entry['day_days'],
'day_price' => (float)$entry['day_price']
];
}
// Sort the array based on day_days in ascending order
usort($days_prices, function($a, $b) {
return $a['day_days'] - $b['day_days'];
});
// Find the correct day_price based on date_difference
$rental_price = null;
foreach ($days_prices as $index => $entry) {
if ($date_difference == $entry['day_days']) {
$rental_price = $entry['day_price'];
break;
} elseif ($date_difference < $entry['day_days']) {
if ($index == 0) {
// If date_difference is less than the smallest day_days value
$rental_price = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rdprice', true);
} else {
// Use the previous entry's day_price
$rental_price = $days_prices[$index - 1]['day_price'];
}
break;
}
}
// If date_difference is greater than the largest day_days value, use the last entry's day_price
if ($rental_price === null && !empty($days_prices)) {
$rental_price = end($days_prices)['day_price'];
}
} else {
// If no serialized data exists, fallback to default price
$rental_price = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rdprice', true);
}
}
}
else if($time_option == 'hour'){
if ($time_difference_minutes == '1') {
$hour_price_get = get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rhprice', true);
$rental_price = $hour_price_get * $time_difference_minutes;
// var_dump($rental_price);
}
else {
// Get the serialized meta data
$serialized_data = get_post_meta($cart_item['product_id'], '_hour_days', true);
if ($serialized_data) {
// Unserialize the data
$data = unserialize($serialized_data);
// Extract the day_days and day_price values
$hour_prices = [];
foreach ($data as $entry) {
$hour_prices[] = [
'hour_days' => (int)$entry['hour_days'],
'hour_price' => (float)$entry['hour_price']
];
}
// Sort the array based on day_days in ascending order
usort($hour_prices, function($a, $b) {
return $a['hour_days'] - $b['hour_days'];
});
// Find the correct day_price based on date_difference
$rental_price = null;
foreach ($hour_prices as $index => $entry) {
if ($time_difference_minutes == $entry['hour_days']) {
$rental_price = $time_difference_minutes * $entry['hour_price'];
break;
} elseif ($time_difference_minutes < $entry['hour_days']) {
if ($index == 0) {
// If date_difference is less than the smallest day_days value
$rental_price = $time_difference_minutes * get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rhprice', true) ;
} else {
// Use the previous entry's day_price
$rental_price = $time_difference_minutes * $hour_prices[$index - 1]['hour_price'];
}
break;
}
}
// If date_difference is greater than the largest day_days value, use the last entry's day_price
if ($rental_price === null && !empty($hour_prices)) {
$rental_price = end($hour_prices)['hour_price'] * $time_difference_minutes;
}
} else {
// If no serialized data exists, fallback to default price
$rental_price = floatval($time_difference_minutes) * floatval(get_post_meta($cart_item['product_id'], 'dreams_booking_meta_rhprice', true));
}
}
}
if ($date_difference === 0) {
$date_difference = 1;
}
if ($rental_deposit == 'yes') {
if ($rental_per_fix == 'percent' && $payment_type == 'deposit') {
if($time_option == 'hour'){
// $product_subtotal = ($rental_price * $rental_amount_deposit / 100) + $rental_insurance + ($selected_extra_services * $date_difference);
$product_subtotal =
((float)$rental_price * (float)$rental_amount_deposit / 100) +
(float)$rental_insurance +
(float)($selected_extra_services * $date_difference);
//$remain_balance = (($rental_price) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
$remain_balance = (
(float)$rental_price +
(float)$rental_insurance +
(float)($selected_extra_services * $date_difference)
) - (float)$product_subtotal;
} else {
//$product_subtotal = ($rental_price * $date_difference * $rental_amount_deposit / 100) + $rental_insurance + ($selected_extra_services * $date_difference);
$product_subtotal = ((float)$rental_price * (float)$date_difference * (float)$rental_amount_deposit / 100)
+ (float)$rental_insurance
+ (float)($selected_extra_services * $date_difference);
//$remain_balance = (($rental_price * $date_difference) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
$remain_balance = (
((float)$rental_price * (float)$date_difference) +
(float)$rental_insurance +
(float)($selected_extra_services * $date_difference)
) - (float)$product_subtotal;
}
} elseif ($rental_per_fix == 'value' && $payment_type == 'deposit') {
if($time_option == 'hour'){
$product_subtotal = (float) $rental_amount_deposit;
//$remain_balance = (($rental_price ) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
$remain_balance = (
(float)$rental_price +
(float)$rental_insurance +
(float)($selected_extra_services * $date_difference)
) - (float)$product_subtotal;
}
else {
$product_subtotal = (float) $rental_amount_deposit;
$remain_balance = (
(float)$rental_price * (float)$date_difference +
(float)$rental_insurance +
(float)($selected_extra_services * $date_difference)
) - (float)$product_subtotal;
}
} elseif ($payment_type == 'full') {
$product_subtotal =
((float) $rental_price * (int) $date_difference) +
(float) $rental_insurance +
(float)($selected_extra_services * $date_difference);
}
} elseif ($rental_deposit == 'yeswithfull') {
if ($rental_per_fix == 'percent' && $payment_type == 'deposit') {
if($time_option == 'hour'){
$product_subtotal = ($rental_price * $rental_amount_deposit / 100) + $rental_insurance + ($selected_extra_services * $date_difference);
$remain_balance = (($rental_price) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
} else {
$rental_price = (float) $rental_price;
$date_difference = (int) $date_difference;
$rental_amount_deposit = (float) $rental_amount_deposit;
$rental_insurance = (float) $rental_insurance;
$selected_extra_services = (float) $selected_extra_services;
$product_subtotal = ($rental_price * $rental_amount_deposit / 100);
$remain_balance = (($rental_price) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
}
} elseif ($rental_per_fix == 'value' && $payment_type == 'deposit') {
if($time_option == 'hour'){
$product_subtotal = (float) $rental_amount_deposit;
$remain_balance = (($rental_price ) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
}
else {
$product_subtotal = (float) $rental_amount_deposit;
$remain_balance = (($rental_price) + $rental_insurance + ($selected_extra_services * $date_difference)) - $product_subtotal;
}
}
$remain_balance_cart = WC()->session->get('remain_balance_cart');
$product_subtotal_cart = WC()->session->get('product_subtotal_cart');
}
else {
// $product_subtotal = ($rental_price * $date_difference) + $rental_insurance + ($selected_extra_services * $date_difference);
$product_subtotal = (float) $rental_price * (float) $date_difference
+ (float) $rental_insurance
+ (float) ($selected_extra_services * $date_difference);
}
}
// $deposit_amount = $product_subtotal;
// $remaining_amount = $remain_balance;
$deposit_amount = $product_subtotal_cart ;
$remaining_amount = $remain_balance_cart ;
$deposit_remaining_amount[0] = $deposit_amount;
$deposit_remaining_amount[1] = $remaining_amount;
$deposit_remaining_amount[2] = $dreams_product_deposit_cart;
return $deposit_remaining_amount;
}
function add_custom_data_to_order_item($item, $cart_item_key, $values, $order) {
if (get_post_type($values['product_id']) === 'rental') {
$hour_format = dreamsrent_fl_framework_getoptions('hour_format');
// Convert dates to 24-hour format
$pickup_date_24h = '';
$pickoff_date_24h = '';
if ($hour_format == '12') {
$pickup_date = DateTime::createFromFormat('d-m-Y h:i A', $values['dsrent_pickup_date']);
$pickup_date_24h = $pickup_date ? $pickup_date->format('Y-m-d H:i') : '';
$pickoff_date = DateTime::createFromFormat('d-m-Y h:i A', $values['dsrent_pickoff_date']);
$pickoff_date_24h = $pickoff_date ? $pickoff_date->format('Y-m-d H:i') : '';
} else if ($hour_format == '24') {
$pickup_date_24h = $values['dsrent_pickup_date'];
$pickoff_date_24h = $values['dsrent_pickoff_date'];
}
// if(isset($values['relocation_price'])) {
// $relocation_price = $values['relocation_price'];
// } else {
// $relocation_price = '';
// }
// if(isset($values['total_price_avg_new'])) {
// $total_price_avg_new = $values['total_price_avg_new'];
// } else {
// $total_price_avg_new = '';
// }
// Add meta data to order item
$item->add_meta_data('dsrent_pickup_date', $pickup_date_24h);
$item->add_meta_data('dsrent_pickoff_date', $pickoff_date_24h);
$item->add_meta_data('dsrent_pickup_loc', $values['dsrent_pickup_loc']);
$item->add_meta_data('dsrent_pickoff_loc', $values['dsrent_pickoff_loc']);
$item->add_meta_data('dsrent_date_difference', $values['date_difference']);
$item->add_meta_data('dsrent_vehicle_terms', $values['dsrent_vehicle_terms']);
$item->add_meta_data('dsrent_quantity', $values['dsrent_quantity']);
$item->add_meta_data('dsrent_insurance_amount', $values['dsrent_insurance']);
$item->add_meta_data('dsrent_reloaction_price', $values['relocation_price']);
$item->add_meta_data('total_price_avg_new', $values['total_price_avg_new']);
$item->add_meta_data('selected_extra_services', $values['selected_extra_services_total'] );
if (isset($values['extra_services_details']) && is_array($values['extra_services_details'])) {
// Store the serialized array in meta
$item->add_meta_data('extra_services_details', $values['extra_services_details']);
// Also add a human-readable version
$services_html = '<ul class="extra-services-details">';
foreach ($values['extra_services_details'] as $service) {
$services_html .= sprintf(
'<li>%s - %s × %d = %s</li>',
esc_html($service['name']),
wc_price($service['price']),
$service['quantity'],
wc_price($service['total_price'])
);
}
$services_html .= '</ul>';
$item->add_meta_data('extra_services_details_display', $services_html);
}
$total_discount_coupen = WC()->session->get('total_discount_coupen', 0);
if (isset($total_discount_coupen)) {
$total_discount_coupen = floatval($total_discount_coupen);
$item->add_meta_data('dsrent_total_discount_coupen', $total_discount_coupen);
}
// Calculate and add deposit and remaining amounts
$deposit_remaining_amount = dreams_calculate_deposit_remaining_amount($values);
$deposit_amount = $deposit_remaining_amount[0] ? $deposit_remaining_amount[0] : '0';
$remaining_amount = $deposit_remaining_amount[1] ? $deposit_remaining_amount[1] : '0';
$price_total_deposit = $deposit_remaining_amount[2] ? $deposit_remaining_amount[2] : '0';
$car_rental_data = WC()->session->get('car_rental');
$payment_type = isset($car_rental_data['payment_type']) ? $car_rental_data['payment_type'] : '';
if ($payment_type == 'deposit') {
// $item->add_meta_data('dsrent_product_subtotal', $deposit_amount);
// $item->add_meta_data('dsrent_remain_balance', $remaining_amount);
$deposit_amount_add = floatval($deposit_amount + $remaining_amount + $price_total_deposit);
$item->add_meta_data('dsrent_product_subtotal', $deposit_amount_add);
$item->add_meta_data('dsrent_deposite_subtotal', $price_total_deposit);
//$item->add_meta_data('dsrent_remain_balance', 0);
}
if ($payment_type == 'full') {
$deposit_amount_add = floatval($deposit_amount + $remaining_amount + $price_total_deposit);
$item->add_meta_data('dsrent_product_subtotal', $deposit_amount_add);
$item->add_meta_data('dsrent_deposite_subtotal', $price_total_deposit);
//$item->add_meta_data('dsrent_remain_balance', 0);
}
// Add car ID and time option
$dsrent_car_id = isset($values['dsrent_car_id']) ? $values['dsrent_car_id'] : '';
$time_option = isset($values['time-option']) ? $values['time-option'] : '';
$dsrent_car_id_post = isset($values['dsrent_car_id_post']) ? $values['dsrent_car_id_post'] : '';
// $item->update_meta_data('_product_id', $dsrent_car_id);
// $item->update_meta_data('time_option', $time_option);
$item->set_product_id($dsrent_car_id);
$item->add_meta_data('time_option', $time_option); // Use add_meta_data for non-internal keys
return $item;
}
}
add_action('woocommerce_checkout_create_order_line_item', 'add_custom_data_to_order_item', 20, 4);
//update order id in bookings tables
function update_order_id_in_booking_table( $order_id) {
global $wpdb;
if (isset($_SESSION['booking_id'])) {
$booking_id = $_SESSION['booking_id'];
$result = $wpdb->update(
$wpdb->prefix . 'dreamsrent_booking',
array('order_id' => $order_id),
array('id' => $booking_id),
array('%d'),
array('%d')
);
}
// if ($result === false) {
// // error_log('Error updating order ID in booking table: ' . $wpdb->last_error);
// } else {
// // error_log('Order ID updated successfully in booking table for booking ID: ' . $booking_id);
// }
}
add_filter( 'woocommerce_order_item_display_meta_value', 'dreams_change_order_item_meta_value', 20, 3 );
function dreams_change_order_item_meta_value( $value, $meta, $item ) {
// By using $meta-key we are sure we have the correct one.
if ( 'dsrent_pickup_loc' === $meta->key ) { $key = esc_html__(' Pick-up Location ', 'dreamsrent'); }
if ( 'dsrent_pickoff_loc' === $meta->key ) { $key = esc_html__(' Drop-off Location ', 'dreamsrent'); }
if ( 'dsrent_pickup_date' === $meta->key ) { $key = esc_html__(' Pick-up Date ', 'dreamsrent'); }
if ( 'dsrent_pickoff_date' === $meta->key ) { $key = esc_html__(' Drop-off Date ', 'dreamsrent'); }
if ( 'dsrent_date_difference' === $meta->key ) { $key = esc_html__(' Total Days ', 'dreamsrent'); }
if ( 'dsrent_vehicle_terms' === $meta->key ) { $key = esc_html__(' Vehicle ID ', 'dreamsrent'); }
if ( 'time_option' === $meta->key ) { $key =esc_html__( 'Rental Type', 'dreamsrent' );
$value = ucfirst($meta->value);
}
if ( 'dsrent_insurance_amount' === $meta->key ) {
$key =esc_html__( 'Amount Of Insurance', 'dreamsrent' );
$value = wc_price($meta->value);
}
if ( 'dsrent_reloaction_price' === $meta->key ) {
$key =esc_html__( 'Relocation Amount', 'dreamsrent' );
$value = wc_price($meta->value);
}
if ( 'total_price_avg_new' === $meta->key ) {
$key =esc_html__( 'Total Price Avg new', 'dreamsrent' );
$value = wc_price($meta->value);
}
if ( 'dsrent_total_discount_coupen' === $meta->key ) {
$key =esc_html__( 'Total Discount Coupen', 'dreamsrent' );
$value = wc_price($meta->value);
}
if ( 'dsrent_product_subtotal' === $meta->key ) {
$key =esc_html__( 'Total Amount', 'dreamsrent' );
$value = wc_price($meta->value);
}
if ( 'dsrent_deposite_subtotal' === $meta->key ) {
$key =esc_html__( 'Deposit Amount', 'dreamsrent' );
$value = wc_price($meta->value);
}
// if ( 'dsrent_remain_balance' === $meta->key ) {
// $key =esc_html__( 'Balance Amount', 'dreamsrent' );
// $value = wc_price($meta->value);
// }
if ( 'selected_extra_services' === $meta->key ) {
$key =esc_html__( 'Extra Services', 'dreamsrent' );
$value = wc_price($meta->value);
}
if ( 'dsrent_quantity' === $meta->key ) { $key = esc_html__(' Quantity ', 'dreamsrent'); }
return $value;
}
// Hide specific meta key from the Thank You page
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'dreams_hide_meta_on_thankyou', 10, 2 );
function dreams_hide_meta_on_thankyou( $formatted_meta, $item ) {
if ( is_order_received_page() ) { // Only on the Thank You page
foreach ( $formatted_meta as $key => $meta ) {
if ( $meta->key === 'dsrent_product_subtotal' ) {
unset( $formatted_meta[ $key ] );
}
if ( $meta->key === 'dsrent_quantity' ) {
unset( $formatted_meta[ $key ] );
}
if ( $meta->key === 'dsrent_remain_balance' ) {
unset( $formatted_meta[ $key ] );
}
}
}
return $formatted_meta;
}
add_filter( 'woocommerce_order_item_display_meta_key', 'dreams_change_order_item_meta_title', 20, 3 );
function dreams_change_order_item_meta_title( $key, $meta, $item ) {
// By using $meta-key we are sure we have the correct one.
if ( 'dsrent_pickup_loc' === $meta->key ) { $key = esc_html__(' Pick-up Location ', 'dreamsrent'); }
if ( 'dsrent_pickoff_loc' === $meta->key ) { $key = esc_html__(' Drop-off Location ', 'dreamsrent'); }
if ( 'dsrent_pickup_date' === $meta->key ) { $key = esc_html__(' Pick-up date ', 'dreamsrent'); }
if ( 'dsrent_pickoff_date' === $meta->key ) { $key = esc_html__(' Drop-off date ', 'dreamsrent'); }
if ( 'dsrent_date_difference' === $meta->key ) { $key = esc_html__(' Total Days ', 'dreamsrent'); }
//if ( 'dsrent_vehicle_terms' === $meta->key ) { $key = esc_html__(' Vehicle ID ', 'dreamsrent'); }
if ( 'dsrent_insurance_amount' === $meta->key ) { $key =esc_html__( 'Amount Of Insurance', 'dreamsrent' ); }
if ( 'dsrent_reloaction_price' === $meta->key ) { $key =esc_html__( 'Relocation Amount', 'dreamsrent' ); }
if ( 'dsrent_product_subtotal' === $meta->key ) { $key =esc_html__( 'Total Amount', 'dreamsrent' ); }
if ( 'dsrent_deposite_subtotal' === $meta->key ) { $key =esc_html__( 'Deposit Amount', 'dreamsrent' ); }
if ( 'dsrent_remain_balance' === $meta->key ) { $key =esc_html__( 'Balance Amount', 'dreamsrent' ); }
if ( 'selected_extra_services' === $meta->key ) { $key =esc_html__( 'Extra Services total', 'dreamsrent' ); }
if ( 'extra_services_details' === $meta->key ) { $key = esc_html__( 'Extra Services details', 'dreamsrent' ); }
if ( 'time_option' === $meta->key ) { $key =esc_html__( 'Rental Type', 'dreamsrent' ); }
if ( 'dsrent_quantity' === $meta->key ) { $key = esc_html__(' Quantity ', 'dreamsrent'); }
if ( 'dsrent_total_discount_coupen' === $meta->key ) { $key = esc_html__( 'Total Discount Coupen', 'dreamsrent' ); }
return $key;
}
function dreams_format_extra_services_display( $display_key, $meta, $item ) {
if ( 'extra_services_details' === $meta->key ) {
$services = maybe_unserialize( $meta->value );
if ( is_array( $services ) && ! empty( $services ) ) {
$output = '<br /><ul class="extra-services-list">';
foreach ( $services as $service ) {
$output .= sprintf(
'<li>%s - %s x %d = %s</li>',
esc_html( $service['name'] ),
wc_price( $service['price'] ),
$service['quantity'],
wc_price( $service['total_price'] )
);
}
$output .= '</ul>';
return $output;
}
}
return $display_key;
}
// Hook the formatting function
add_filter( 'woocommerce_order_item_display_meta_value', 'dreams_format_extra_services_display', 10, 3 );
add_action('woocommerce_checkout_create_order', 'dreams_checkout_order_processed', 10, 2);
function dreams_checkout_order_processed($order, $data) {
global $woocommerce;
// $order->set_total($original_total);
$order->save();
}
function ds_update_order_meta_value( $order_id ) {
global $woocommerce, $global_rental_deposit, $global_dsrent_car_id;
// Get the order object
$order = wc_get_order( $order_id );
$values = WC()->session->get('car_rental');
$rental_insurance = isset($values['rental_insurance']) ? $values['rental_insurance'] : '';
$payment_type = isset($values['payment_type']) ? $values['payment_type'] : '';
$dsrent_car_id = isset($values['dsrent_car_id']) ? $values['dsrent_car_id'] : '';
$dsrent_car_id_post = isset($values['dsrent_car_id_post']) ? $values['dsrent_car_id_post'] : '0';
$remain_balance_cart = WC()->session->get('dreams_remain_balance_cart');
$product_subtotal_cart = WC()->session->get('dreams_product_subtotal_cart');
$dreams_product_deposit_cart = WC()->session->get('dreams_product_deposit_cart');
// Calculate the total amount with tax
$total_amount_with_tax = $product_subtotal_cart + $remain_balance_cart;
// Calculate total before tax
$total_amount = $total_amount_with_tax;
// Update the order meta values
$order->update_meta_data('_dsrent_insurance_amount', $rental_insurance);
//$order->update_meta_data('_dsrent_car_id', $dsrent_car_id);
$order->update_meta_data('_dsrent_car_id_glo', $global_dsrent_car_id);
// $order->update_meta_data('dsrent_car_id_post', $dsrent_car_id_post);
// $post_author_id = get_post_field('post_author', $dsrent_car_id_post);
// $order->update_meta_data('post_author', $post_author_id);
// $post_author_id = get_post_field('post_author', $values['car_id']);
// $order->update_meta_data('post_author', $post_author_id);
$total_discount_coupen = WC()->session->get('total_discount_coupen', 0);
if (isset($total_discount_coupen)) {
$order->update_meta_data('_dsrent_total_discount_coupen', $total_discount_coupen);
}
if ($payment_type == 'deposit') {
$order->update_meta_data('_dsrent_deposit_balance', $dreams_product_deposit_cart);
// $order->update_meta_data('_dsrent_remain_balance', $remain_balance_cart);
$order->update_meta_data('_dsrent_total_amount', $total_amount);
} else {
$order->update_meta_data('_dsrent_deposit_balance', $total_amount);
// $order->update_meta_data('_dsrent_remain_balance', 0);
$order->update_meta_data('_dsrent_total_amount', $total_amount);
}
// Set the total amount (excluding tax) to the order
// $order->set_total($total_amount);
$order->save();
// Clear the session data
WC()->session->__unset('car_rental');
}
add_action('woocommerce_checkout_create_order', 'ds_update_order_meta_value', 10, 1);
function dreams_saved_order_items( $order_id, $cart_item ){
global $global_dsrent_car_id;
$order = wc_get_order( $order_id );
$order->read_meta_data( true );
print_r($order);
foreach ($order->get_items() as $item_id => $item) {
$dsrent_car_id = $item->get_product_id();
// $time_option = wc_get_order_item_meta( $item_id, 'time_option', true );
// $dsrent_car_id = wc_get_order_item_meta( $item_id, $global_dsrent_car_id, true );
$dsrent_car_id = isset($cart_item['dsrent_car_id']) ? $cart_item['dsrent_car_id'] : ( wc_get_order_item_meta( $item_id, 'dsrent_car_product_id', true ) ? wc_get_order_item_meta( $item_id, 'dsrent_car_product_id', true ) : '' );
$original_deposit = wc_get_order_item_meta( $item_id, 'dsrent_product_subtotal', true );
if ($original_deposit) {
// wc_update_order_item_meta( $item_id , '_line_total' , $original_deposit );
// wc_update_order_item_meta( $item_id , '_line_subtotal' , $original_deposit );
wc_update_order_item_meta( $item_id , '_product_id' , $dsrent_car_id );
//wc_update_order_item_meta( $item_id , '_rental_type_get' , $time_option );
}
}
}
add_action( 'woocommerce_checkout_order_processed', 'dreams_saved_order_items' , 10 , 2 );
//19.03.2024
//require_once get_template_directory() . '/src/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use Dompdf\Options;
add_action('woocommerce_thankyou', 'change_total_on_thank_you_page');
function change_total_on_thank_you_page($order_id) {
$order = wc_get_order($order_id);
$order_items = $order->get_items();
$new_total = 0;
foreach ($order_items as $item_id => $item) {
$new_total += $item->get_total();
}
$order->set_total($new_total);
$order->calculate_totals();
update_order_id_in_booking_table($order_id);
// send_invoice_email($order, $new_total);
}
?>