Add new booking fields

Booking Filters

To customize the booking fields you can use WordPress actions and filters.

The booking fields in WP Hotelier are divided in two parts: Address Fields and Additional Information Fields. Each of those group of fields have a custom filter, so you can use that filter to add/remove/modify a field from each group.

The name of the filters are hotelier_booking_default_address_fields for the Address Fields and  hotelier_booking_additional_information_fields for the Additional Information Fields.

You can view the arrays of each group in  hotelier/includes/class-htl-booking.php (source code).

How it works? Show and store the field

To add a new field, for example a credit card, you just need to create a function in functions.php (of your child theme) that returns this new field, and add it to your desired field group using its filter.

function custom_guest_details_field( $fields ) {
     $fields[ 'custom_field' ] = array(
         'label'       => esc_html__(  'Custom Label', 'hotelier'  ),
         'placeholder' => esc_html_x(  'Custom placeholder', 'placeholder', 'hotelier'  ),
         'type'        => 'text',
         'required'    => true,
         'class'       => array(  'form-row-wide'  )
     );
     return $fields; 
}
add_filter( 'hotelier_booking_default_address_fields' , 'custom_guest_details_field' );

This code creates a new required text field named " Custom Label" in the Address Fields group. And with the "custom_field" ID. The ID is the key of the array. This is very important, because the field will be stored in the database using that ID and then you will be able to retrieve that value with (note the guest_ prefix):

$your_field = $reservation->guest_custom_field;

Each field contains the following array properties:

  • type – type of field (text, textarea, email, tel and select are the possible values) 
  • label – label of the input field 
  • placeholder – placeholder for the input 
  • class – class for the input 
  • required – true or false, whether or not the field is required 
  • options – for select boxes, array of options (key => value pairs)

Show the field on the admin reservation page

The code above will only show the field on the booking page and store it in the reservation data. To display the custom field on the admin reservation page you need some extra code.

function custom_fields_in_reservation_data() {
    global $post;
    $reservation = htl_get_reservation( $post->ID );
    if ( $reservation->guest_custom_field ) {
        $field = array(
            'id'    => '_guest_custom_field',
            'label' => esc_html__( 'Custom Label', 'text-domain' ),
            'value' => $reservation->guest_custom_field,
        );
    HTL_Meta_Boxes_Helper::text_input( $field );
    }
}
add_action( 'hotelier_reservation_before_guest_details', 'custom_fields_in_reservation_data' );

function add_custom_field_to_reservation_metaboxes( $metaboxes ) {
    $metaboxes['_guest_custom_field'] = 'text';
    return $metaboxes;
}
add_filter( 'hotelier_reservation_meta_boxes', 'add_custom_field_to_reservation_metaboxes' );

Please remember, you need to prefix the ID of the field with guest_:

$reservation->guest_custom_field

Same for the ID:

'_guest_custom_field'

There are 6 available places to show your custom field:

'hotelier_reservation_before_guest_details': before the guest details column 

'hotelier_reservation_after_guest_details': after the guest details column 

'hotelier_reservation_before_guest_arrival_time': before the guest arrival time section 

'hotelier_reservation_after_guest_arrival_time': before the guest guest arrival time section 

'hotelier_reservation_before_guest_special_requets': before the special requests section 

'hotelier_reservation_after_guest_special_requets': after the special requests section 

You can use one of those actions to display your field with:

add_action( 'hotelier_reservation_before_guest_details', 'custom_fields_in_reservation_data' );