Ứng dụng đặt xe taxi trên WordPress với Google Maps và Zalo

Dưới đây là hướng dẫn triển khai ứng dụng đặt xe taxi trên nền tảng WordPress, tích hợp tính năng tính cước theo quãng đường bằng Google Maps và gửi thông báo qua Zalo.

1. Cài đặt WordPress và các plugin cần thiết

Trước tiên, bạn cần cài đặt các plugin sau trên WordPress:

  1. Elementor Pro (để tạo trang đặt xe)

  2. WPForms (form đặt xe)

  3. Google Maps Widget (hiển thị bản đồ)

  4. Zalo Official Account OA (tích hợp Zalo)

2. Tạo trang đặt xe với Elementor

Tạo một trang mới với Elementor và thiết kế giao diện đặt xe:

<!-- Template HTML cho trang đặt xe -->
<div class="taxi-booking-container">
    <h2>Đặt xe taxi Nội Bài & Đường dài</h2>
    
    <div class="booking-form">
        <div class="form-group">
            <label>Điểm đón</label>
            <input type="text" id="pickup-location" placeholder="Nhập điểm đón">
        </div>
        
        <div class="form-group">
            <label>Điểm đến</label>
            <input type="text" id="dropoff-location" placeholder="Nhập điểm đến">
        </div>
        
        <div class="form-group">
            <label>Loại xe</label>
            <select id="car-type">
                <option value="4">Xe 4 chỗ</option>
                <option value="7">Xe 7 chỗ</option>
                <option value="16">Xe 16 chỗ</option>
            </select>
        </div>
        
        <div class="form-group">
            <label>Số điện thoại</label>
            <input type="tel" id="phone-number" placeholder="Nhập số điện thoại">
        </div>
        
        <button id="calculate-fare">Tính cước phí</button>
        
        <div id="fare-result" style="display:none;">
            <p>Khoảng cách: <span id="distance">0</span> km</p>
            <p>Cước phí: <span id="fare">0</span> VND</p>
            <button id="book-now">Đặt xe ngay</button>
        </div>
    </div>
    
    <div id="map-container" style="height: 400px; margin-top: 20px;"></div>
</div>

3. Code JavaScript xử lý tính cước và đặt xe

Thêm đoạn code sau vào footer của website (hoặc tạo một custom JS file):

jQuery(document).ready(function($) {
    // Khởi tạo bản đồ
    var map = new google.maps.Map(document.getElementById('map-container'), {
        center: {lat: 21.0285, lng: 105.8542}, // Hà Nội
        zoom: 12
    });
    
    var directionsService = new google.maps.DirectionsService();
    var directionsRenderer = new google.maps.DirectionsRenderer();
    directionsRenderer.setMap(map);
    
    // Tính cước phí
    $('#calculate-fare').click(function() {
        var origin = $('#pickup-location').val();
        var destination = $('#dropoff-location').val();
        var carType = $('#car-type').val();
        
        if (!origin || !destination) {
            alert('Vui lòng nhập điểm đón và điểm đến');
            return;
        }
        
        calculateDistanceAndFare(origin, destination, carType);
    });
    
    // Đặt xe
    $('#book-now').click(function() {
        var phone = $('#phone-number').val();
        if (!phone) {
            alert('Vui lòng nhập số điện thoại');
            return;
        }
        
        bookTaxi(phone);
    });
    
    function calculateDistanceAndFare(origin, destination, carType) {
        var request = {
            origin: origin,
            destination: destination,
            travelMode: 'DRIVING',
            unitSystem: google.maps.UnitSystem.METRIC
        };
        
        directionsService.route(request, function(response, status) {
            if (status == 'OK') {
                directionsRenderer.setDirections(response);
                
                var distance = response.routes[0].legs[0].distance.value / 1000; // km
                var duration = response.routes[0].legs[0].duration.text;
                
                // Tính cước phí
                var fare = calculateFare(distance, carType);
                
                // Hiển thị kết quả
                $('#distance').text(distance.toFixed(1));
                $('#fare').text(fare.toLocaleString());
                $('#fare-result').show();
                
                // Lưu thông tin tạm
                sessionStorage.setItem('taxi_booking', JSON.stringify({
                    origin: origin,
                    destination: destination,
                    distance: distance,
                    fare: fare,
                    carType: carType
                }));
            } else {
                alert('Không thể tính toán tuyến đường: ' + status);
            }
        });
    }
    
    function calculateFare(distance, carType) {
        var baseFare = 10000;
        var perKm = 15000;
        var noiBaiSurcharge = 20000;
        
        // Tính giá theo loại xe
        if (carType == '7') perKm = 18000;
        if (carType == '16') perKm = 25000;
        
        var fare = baseFare + (distance * perKm);
        
        // Phụ phí sân bay Nội Bài
        if ($('#pickup-location').val().includes('Nội Bài') || 
            $('#dropoff-location').val().includes('Nội Bài')) {
            fare += noiBaiSurcharge;
        }
        
        return Math.round(fare);
    }
    
    function bookTaxi(phone) {
        var bookingData = JSON.parse(sessionStorage.getItem('taxi_booking'));
        if (!bookingData) {
            alert('Vui lòng tính cước phí trước khi đặt xe');
            return;
        }
        
        // Gửi dữ liệu đến backend WordPress
        $.ajax({
            url: '/wp-admin/admin-ajax.php',
            type: 'POST',
            data: {
                action: 'process_taxi_booking',
                phone: phone,
                origin: bookingData.origin,
                destination: bookingData.destination,
                distance: bookingData.distance,
                fare: bookingData.fare,
                car_type: bookingData.carType
            },
            success: function(response) {
                if (response.success) {
                    alert('Đặt xe thành công! Thông tin đã được gửi qua Zalo');
                } else {
                    alert('Có lỗi xảy ra: ' + response.message);
                }
            },
            error: function() {
                alert('Lỗi kết nối, vui lòng thử lại');
            }
        });
    }
});

4. Code PHP xử lý backend (thêm vào functions.php)

// Xử lý đặt xe
add_action('wp_ajax_process_taxi_booking', 'process_taxi_booking');
add_action('wp_ajax_nopriv_process_taxi_booking', 'process_taxi_booking');

function process_taxi_booking() {
    // Kiểm tra nonce bảo mật
    if (!isset($_POST['phone']) {
        wp_send_json_error('Vui lòng nhập số điện thoại');
    }

    $phone = sanitize_text_field($_POST['phone']);
    $origin = sanitize_text_field($_POST['origin']);
    $destination = sanitize_text_field($_POST['destination']);
    $distance = floatval($_POST['distance']);
    $fare = intval($_POST['fare']);
    $car_type = sanitize_text_field($_POST['car_type']);

    // Tạo mã đặt xe
    $booking_id = 'TAXI-' . date('YmdHis');

    // Lưu vào database (cần tạo custom post type hoặc custom table)
    $booking_data = array(
        'post_title' => $booking_id,
        'post_type' => 'taxi_booking',
        'post_status' => 'publish',
        'meta_input' => array(
            'phone' => $phone,
            'origin' => $origin,
            'destination' => $destination,
            'distance' => $distance,
            'fare' => $fare,
            'car_type' => $car_type,
            'status' => 'pending'
        )
    );
    
    $booking_post_id = wp_insert_post($booking_data);

    if ($booking_post_id) {
        // Gửi thông báo qua Zalo
        $zalo_result = send_zalo_notification($phone, $booking_id, $origin, $destination, $distance, $fare);
        
        if ($zalo_result) {
            wp_send_json_success('Đặt xe thành công');
        } else {
            wp_send_json_error('Gửi Zalo thất bại');
        }
    } else {
        wp_send_json_error('Lỗi khi lưu đơn đặt xe');
    }
}

// Hàm gửi thông báo Zalo
function send_zalo_notification($phone, $booking_id, $origin, $destination, $distance, $fare) {
    $zalo_oa_token = 'YOUR_ZALO_OA_TOKEN'; // Thay bằng token của bạn
    
    // Format số điện thoại
    $formatted_phone = preg_replace('/^0/', '84', $phone);
    
    $message = "✅ Đặt xe thành công!
Mã đơn: $booking_id
Điểm đón: $origin
Điểm đến: $destination
Khoảng cách: $distance km
Cước phí: " . number_format($fare) . " VND
Tài xế sẽ liên hệ với bạn trong ít phút.";

    $args = array(
        'headers' => array(
            'access_token' => $zalo_oa_token,
            'Content-Type' => 'application/json'
        ),
        'body' => json_encode(array(
            'recipient' => array(
                'user_id' => $formatted_phone
            ),
            'message' => array(
                'text' => $message
            )
        ))
    );
    
    $response = wp_remote_post('https://openapi.zalo.me/v2.0/oa/message', $args);
    
    if (!is_wp_error($response)) {
        $body = json_decode($response['body'], true);
        return $body['error'] == 0;
    }
    
    return false;
}

// Tạo custom post type cho đơn đặt xe
add_action('init', 'register_taxi_booking_post_type');
function register_taxi_booking_post_type() {
    register_post_type('taxi_booking',
        array(
            'labels' => array(
                'name' => __('Đơn đặt xe'),
                'singular_name' => __('Đơn đặt xe')
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'custom-fields'),
            'capability_type' => 'post',
            'show_in_menu' => true,
            'menu_icon' => 'dashicons-car'
        )
    );
}

5. Các bước triển khai

  1. Cài đặt Google Maps API:

    • Đăng ký Google Cloud Platform

    • Bật Directions API và Maps JavaScript API

    • Tạo API key và thêm vào code

  2. Tích hợp Zalo OA:

    • Đăng ký tài khoản Zalo Official Account

    • Lấy OA token từ Zalo Developer Console

    • Thêm token vào code PHP

  3. Tạo custom fields cho đơn đặt xe:

    • Có thể sử dụng plugin Advanced Custom Fields để quản lý các trường dữ liệu

  4. Bảo mật:

    • Thêm nonce check cho AJAX request

    • Validate dữ liệu đầu vào

    • Giới hạn API key Google Maps

  5. Mở rộng tính năng:

    • Thêm trang quản lý đơn đặt xe cho admin

    • Tích hợp thanh toán online

    • Thêm tính năng đánh giá tài xế

6. Các plugin hỗ trợ thêm

  1. Advanced Custom Fields – Quản lý thông tin đơn đặt xe

  2. WPML – Đa ngôn ngữ nếu cần

  3. WooCommerce – Nếu muốn tích hợp thanh toán

  4. Booking Calendar – Đặt xe theo lịch trình

Bạn cần hỗ trợ thêm về phần nào không? Tôi có thể giải thích chi tiết hơn về:

  • Cách tích hợp Google Maps API

  • Cấu hình Zalo OA

  • Tạo trang quản lý đơn đặt xe

  • Tích hợp thanh toán điện tử

Chia sẻ

Tin tức

Cách tạo banner động

Để tạo banner động cho website WordPress, bạn có thể làm theo các bước sau. Banner động thường là hình ảnh hoặc video chuyển động

Tin tức

Các cách kiếm tiền online

Dưới đây là các cách kiếm tiền online phổ biến và hiệu quả tại Việt Nam, phù hợp với nhiều đối tượng, từ người mới

0963286779