// Add custom fields at checkout
add_filter('woocommerce_checkout_fields', function($fields) {
    $fields['billing']['passenger_name'] = array(
        'type'        => 'text',
        'label'       => __('Full Name of Passenger'),
        'required'    => true,
        'class'       => array('form-row-wide'),
        'priority'    => 21,
    );

    $fields['billing']['passenger_contact'] = array(
        'type'        => 'text',
        'label'       => __('Passenger Contact Info'),
        'required'    => true,
        'class'       => array('form-row-wide'),
        'priority'    => 22,
    );

    $fields['billing']['passenger_occasion'] = array(
        'type'        => 'text',
        'label'       => __('Occasion (Matric Farewell, Wedding, etc.)'),
        'required'    => false,
        'class'       => array('form-row-wide'),
        'priority'    => 23,
    );

    return $fields;
});

// Save custom fields to order
add_action('woocommerce_checkout_create_order', function($order, $data) {
    if (!empty($_POST['passenger_name'])) {
        $order->update_meta_data('Full Name of Passenger', sanitize_text_field($_POST['passenger_name']));
    }
    if (!empty($_POST['passenger_contact'])) {
        $order->update_meta_data('Passenger Contact Info', sanitize_text_field($_POST['passenger_contact']));
    }
    if (!empty($_POST['passenger_occasion'])) {
        $order->update_meta_data('Occasion', sanitize_text_field($_POST['passenger_occasion']));
    }
}, 10, 2);

// Display in admin order screen
add_action('woocommerce_admin_order_data_after_billing_address', function($order){
    echo '<p><strong>'.__('Full Name of Passenger').':</strong> ' . esc_html($order->get_meta('Full Name of Passenger')) . '</p>';
    echo '<p><strong>'.__('Passenger Contact Info').':</strong> ' . esc_html($order->get_meta('Passenger Contact Info')) . '</p>';
    echo '<p><strong>'.__('Occasion').':</strong> ' . esc_html($order->get_meta('Occasion')) . '</p>';
});
