init_hooks();
}
/**
* Initialize WordPress hooks
*/
private function init_hooks() {
// Add tourist tax to booking calculations
add_filter('eb_booking_total_price', array($this, 'add_tourist_tax_to_total'), 10, 2);
// Add tourist tax display to booking forms
add_action('eb_booking_form_after_price', array($this, 'display_tourist_tax_info'));
// Add tourist tax to checkout summary
add_action('eb_checkout_summary_after_taxes', array($this, 'display_tourist_tax_line'));
// Add tourist tax to booking confirmation
add_action('eb_booking_confirmation_details', array($this, 'display_tourist_tax_confirmation'));
// Save tourist tax data with booking
add_action('eb_booking_created', array($this, 'save_tourist_tax_data'), 10, 2);
// Add admin settings
add_action('admin_menu', array($this, 'add_admin_menu'));
// Enqueue scripts and styles
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
/**
* Calculate tourist tax for booking
*
* @param int $guests Number of guests
* @param int $nights Number of nights
* @return float Tourist tax amount
*/
public function calculate_tourist_tax($guests, $nights) {
// Apply tourist tax only to adults (children usually exempted)
$adults = max(1, $guests); // At least 1 adult
// Calculate: €2.20 × adults × nights
$tax_amount = self::TAX_RATE * $adults * $nights;
return round($tax_amount, 2);
}
/**
* Add tourist tax to booking total price
*
* @param float $total_price Current total price
* @param array $booking_data Booking information
* @return float Modified total price
*/
public function add_tourist_tax_to_total($total_price, $booking_data) {
if (!isset($booking_data['guests']) || !isset($booking_data['nights'])) {
return $total_price;
}
$tourist_tax = $this->calculate_tourist_tax(
$booking_data['guests'],
$booking_data['nights']
);
return $total_price + $tourist_tax;
}
/**
* Display tourist tax information on booking forms
*/
public function display_tourist_tax_info() {
?>
get_current_booking_data();
if (!$booking_data) {
return;
}
$tourist_tax = $this->calculate_tourist_tax(
$booking_data['guests'],
$booking_data['nights']
);
if ($tourist_tax > 0) {
?>
|
()
|
€
|
0) {
?>
calculate_tourist_tax(
$booking_data['guests'],
$booking_data['nights']
);
// Save tourist tax data
update_post_meta($booking_id, '_eb_tourist_tax_amount', $tourist_tax);
update_post_meta($booking_id, '_eb_tourist_tax_rate', self::TAX_RATE);
update_post_meta($booking_id, '_eb_tourist_tax_guests', $booking_data['guests']);
update_post_meta($booking_id, '_eb_tourist_tax_nights', $booking_data['nights']);
}
/**
* Get current booking data from various sources
*
* @return array|false Booking data or false if not available
*/
private function get_current_booking_data() {
// Try to get from POST data (during checkout)
if (isset($_POST['eb_guests']) && isset($_POST['eb_checkin']) && isset($_POST['eb_checkout'])) {
$checkin = sanitize_text_field($_POST['eb_checkin']);
$checkout = sanitize_text_field($_POST['eb_checkout']);
$guests = intval($_POST['eb_guests']);
$checkin_date = new DateTime($checkin);
$checkout_date = new DateTime($checkout);
$nights = $checkout_date->diff($checkin_date)->days;
return array(
'guests' => $guests,
'nights' => $nights
);
}
// Try to get from session
if (isset($_SESSION['eb_booking_data'])) {
return $_SESSION['eb_booking_data'];
}
return false;
}
/**
* Add admin menu for tourist tax settings
*/
public function add_admin_menu() {
if (!class_exists('EB_CORE')) {
return;
}
add_submenu_page(
'eb_bookings',
__('Tourist Tax', 'eb-tourist-tax'),
__('Tourist Tax', 'eb-tourist-tax'),
'manage_options',
'eb-tourist-tax',
array($this, 'admin_page')
);
}
/**
* Render admin page
*/
public function admin_page() {
// Get tourist tax statistics
$stats = $this->get_tourist_tax_stats();
?>
display_recent_bookings_table(); ?>
get_results(
"SELECT pm.meta_value as tax_amount
FROM {$wpdb->postmeta} pm
WHERE pm.meta_key = '_eb_tourist_tax_amount'
AND pm.meta_value > 0"
);
$total_bookings = count($bookings);
$total_tax_collected = array_sum(array_column($bookings, 'tax_amount'));
$average_tax_per_booking = $total_bookings > 0 ? $total_tax_collected / $total_bookings : 0;
return array(
'total_bookings' => $total_bookings,
'total_tax_collected' => $total_tax_collected,
'average_tax_per_booking' => $average_tax_per_booking
);
}
/**
* Display recent bookings table
*/
private function display_recent_bookings_table() {
global $wpdb;
// Get recent bookings with tourist tax
$bookings = $wpdb->get_results(
"SELECT p.ID, p.post_title, p.post_date,
pm1.meta_value as tax_amount,
pm2.meta_value as guests,
pm3.meta_value as nights
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm1 ON p.ID = pm1.post_id AND pm1.meta_key = '_eb_tourist_tax_amount'
LEFT JOIN {$wpdb->postmeta} pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_eb_tourist_tax_guests'
LEFT JOIN {$wpdb->postmeta} pm3 ON p.ID = pm3.post_id AND pm3.meta_key = '_eb_tourist_tax_nights'
WHERE p.post_type = 'eagle_booking'
AND pm1.meta_value > 0
ORDER BY p.post_date DESC
LIMIT 10"
);
if (empty($bookings)) {
echo '' . __('No bookings with tourist tax found.', 'eb-tourist-tax') . '
';
return;
}
?>
|
|
|
|
|
|
#ID; ?>
|
post_date)); ?> |
guests; ?> |
nights; ?> |
€tax_amount, 2); ?> |