Booking Form API

This page refers to the booking form API, and is aimed at developers. Event Organiser Pro also provides an easy-to-use form customiser for configuring the booking form, and adding fields.

Adding a field

To add a field you need to first create it. Each field must have a known (registered) type and an ID. The ID must be unique. Other values which can be set include label, options for select/radio/checkbox fields and whether it should be required.

To create a field you should use the factory:

 add_action( 'eventorganiser_get_event_booking_form', 'add_field_to_booking_form', 10, 2 );
 function add_field_to_booking_form( $form, $event_id ){

     //Create a field
     $checkbox = EO_Booking_Form_Element_Factory::create( array(
         'type'        => 'checkbox',
         'id'          => 'my-unique-id', //Must be unique in the form. You should not use 'name', 'ticketpicker', 'gateway' or 'email' as an ID,
         'label'       => 'Select an option:',
         'options'     => array( 'A' => 'Option A', 'B' => 'Option B', 'C' => 'Option C' ),
         'required'    => true, //Default is false
         'description' => "Pick one of the options",
     ) );

     $form->add_element( $checkbox );

 }

Editing an existing field

 add_action( 'eventorganiser_get_event_booking_form', 'add_conditional_logic_to_booking_form', 10, 2 );
 function add_conditional_logic_to_booking_form( $form, $event_id ){

     //Retrieve a field by ID.
     $name = $form->get_element( "name" );
     $name->set( 'lname_required', true ); //Require last name

 }

Validating a field

The eventorganiser_validate_booking_form_submission hook allows you to retrieve the values provided by the user in the booking form. If you add an error to the booking form, or any of its elements, the booking will not go through. Instead the user will be returned to the booking form, and errors added to the form will display at the top, and errors added to any elements shall appear underneath the element.

 add_action( 'eventorganiser_validate_booking_form_submission', 'my_pre_booking_validation' );
 function my_pre_booking_validation( $form ){

     //Retrieve an element:
     $element = $form->get_element( 'my-unique-id' );

     if( !$element ){
         $form->add_error( 'my-error-code', "We couldn't find the element!" );
         return;
     }

     $value = $element->get_value(); //Get the submitted value. For checkboxes this shall be an array.


     //This check is a bit pointless, but is for demonstrative purposes only
     if( $value && in_array( 'D', $value ) ){
         $element->add_error( "my-element-error-code", "Erm, D wasn't an option..." );
     }

 }

Conditional logic

Elements can be made to appear (or disappear) subject to one or conditions. This done by setting the element’s conditional_logic attribute, which accepts a multi-dimensional array:

  • action – The action to perform if the conditions succeed. 'show' or 'hide'.
  • gate – The logical gate applied to the conditions. 'all' or 'any'. I.e. Perform action if all/any conditions hold
  • ‘conditions’ – An array of conditions. Each condition is an array indexed by
    • ‘target’ – The ID of the element to compare a value against
    • ‘value’ – The value to compare against the value of the target element
    • ‘operator’ – The operator to use to compare the two above. One of ‘equals’, ‘notequals’, ‘greaterthan’, ‘lessthan’, ‘contains’, ‘notcontains’

For example this following element A only appears if element B’s value is ‘Show A’.

 add_action( 'eventorganiser_get_event_booking_form', 'add_conditional_logic_to_booking_form', 10, 2 );
 function add_conditional_logic_to_booking_form( $form, $event_id ){

     //Retrieve a field
     $field_a = $form->get_element( "A" );

     $conditional_logic = array(
          'action' => 'show', //or hide. Action to peform
          'gate'   => 'all', //or 'any'. Logical gate
          'conditions' => array(
              array(
                   'target'    => 'B', //The other element to check against
                   'value'     => 'Show A', //The value to compare against
                   'operator'  => 'equals', //could be notequals, greaterthan, lessthan, contains, notcontains                  
              )
          ),
     );

     $field_a->set( 'conditional', $conditional_logic );

 }