Attendee Questions

This tutorial requires 1.11.0 which at the time of writing is due to be released in the coming weeks.

‘Attendee Questions’ means form fields that are replicated on your booking form – one for each ticket selected in the booking. So if your bookings typically involve more than one ticket, this allows you, for example, to collect the names and e-mail address of those attending, or perhaps dietary requirements.

attendee-questions

In this example there are two ‘attendee question’ fields: name and e-mail. And these appear twice: once for the selected standard ticket and once for the selected student ticket.

There is no UI for adding attendee questions; it is all done through the form API. Additionally, Event Organiser doesn’t do anything, by default, with the collected data, this tutorial however will show you how to include this data in the ticket CSV export and the bookings admin page.

As with all snippets posted on this site, they should work in your theme’s functions.php but it is recommended you create a dedicated ‘site utility plugin’. See more details on this page: Where should I put code from the tutorials?.

Adding the questions

First, here’s the the snippet that adds the attendee questions in its entirety:

function my_attach_attendee_questions( $form ){

    //Define the field we want to add    
    $attendee_fields = array(
        array(
            'id'   => 'attendee-name',
            'type' => 'name',
            'required' => true,
        ),
        array(
            'id'   => 'attendee-email',
            'type' => 'email',
            'required' => true,
        ),
    );

    //The attendee-questions element acts as a 'holder' for the elements 
    //we want repeated for each ticket
    $attendee_questions = $form->get_element( 'attendee-questions' );

    //If it doesn't exist, we'll create it
    if ( ! $attendee_questions ) {

        $ticketpicker = $form->get_element( 'ticketpicker' );
        $position     = intval( $ticketpicker->get( 'position' ) ) + 1;
        $parent       = $ticketpicker->get_parent();
        $parent       = ( $parent ? $parent->id : false );

        //Create the attendee questions els
        $attendee_questions = EO_Booking_Form_Element_Factory::create(array(
            'id'           => 'attendee-questions',
            'type'         => 'attendee-questions',
            'elements'     => $attendee_fields,
            'ticket_label' => "Ticket: {{{ticketname}}}",
        ));

        //Add the attendee questions right after the ticket picker
        $form->add_element( $attendee_questions, array( 'at' => $position, 'parent' => $parent ) );

    } else {
        //Attendee questions field already exists, optional: over-ride with our elements
        $attendee_questions->set( 'elements', $attendee_fields );
        $attendee_questions->_modelElements->set( $attendee_questions->get('elements'), array( 'form' => $form ) );
    }

}
add_action( 'eventorganiser_get_event_booking_form', 'my_attach_attendee_questions', 5 );

Lets break this down a bit. The first bit is the most important. We first define what fields we want to appear for each attendee:

    $attendee_fields = array(
        array(
            'id'   => 'attendee-name',
            'type' => 'name',
            'required' => true,
        ),
        array(
            'id'   => 'attendee-email',
            'type' => 'email',
            'required' => true,
        ),
    );

This is an array of question or ‘element’ definitions, each of which is itself an array containing (at minimum):

  • id – A unique ID for this field.
  • type – The type of field (e.g. input (text), select, textarea, radio, checkbox)

It’s strongly recommended to make sure your IDs are unique by prepending your own initials, or those of the project your are working on.

Take a note of the IDs you decide above as we’ll be using them later. Other optional properties include:

  • label – The label for this field
  • required – Whether this field must be completed
  • options – (for select, radio, checkbox fields) An array of the options presented to the user. The key is what is stored, and the value is what is displayed on the form

The second part simply adds the ‘Attendee Question’ element (if it isn’t already present) and adds our form fields beneath it. This allows the plug-in to encapsulate our attendee questions and repeat them for each ticket. The only noteworthy part is the ticket label:

$attendee_questions = EO_Booking_Form_Element_Factory::create(array(
    'id'           => 'attendee-questions',
    'type'         => 'attendee-questions',
    'elements'     => $attendee_fields,
    'ticket_label' => "Ticket: {{{ticketname}}}",
));

The label is assigned to each set of attendee questions, and {{{ticketname}}} is replaced by the name of the associated ticket. This helps the bookee associate each section of questions which a particular ticket type.

Including the attendee metadata in the ticket CSV export

First we add the columns to our CSV export for our additional data:

add_filter( 'eventorganiser_export_tickets_headers', function( $columns ) {
    $columns['ticket_first_name'] = 'Ticket Holder (first name)';
    $columns['ticket_last_name'] = 'Ticket Holder (last name)';
    $columns['ticket_email'] = 'Ticket Holder Email';
    return $columns;
} );

Take a note of the columns keys as we’ll use them next. We now define the content of the cell for each ticket in the export:

add_filter( 'eventorganiser_export_tickets_cell', function( $cell, $column, $ticket ) {

    switch( $column ){

        case 'ticket_first_name':
            $name = eo_get_booking_ticket_meta( $ticket->booking_ticket_id, '_eo_booking_meta_attendee-name' ); //_eo_booking_meta_{field id}
            return $name[0]; //$name is an array: array( 'first name', 'last name' );
            break;

        case 'ticket_last_name':
            $name = eo_get_booking_ticket_meta( $ticket->booking_ticket_id, '_eo_booking_meta_attendee-name' ); //_eo_booking_meta_{field id}
            return $name[1]; //$name is an array: array( 'first name', 'last name' );
            break;

        case 'ticket_email':
            return eo_get_booking_ticket_meta( $ticket->booking_ticket_id, '_eo_booking_meta_attendee-email', true ); //_eo_booking_meta_{field id}
            break;

        default:
            return $cell;
    }

}, 10, 3 );

The $column is the column ID of the cell. When it is is equal to either of the three columns we added in the previous part we set the content of the cell. To retrieve ticket meta data we use the following:

 $value = eo_get_booking_ticket_meta( $ticket->booking_ticket_id, '_eo_booking_meta_{question-id}', true ); 

where {question-id} should be replaced by the ID of the appropriate attendee field we defined at the beginning. E.g.

 $value = eo_get_booking_ticket_meta( $ticket->booking_ticket_id, '_eo_booking_meta_attendee-email', true );

Note that you should return not print the value to be inserted into the cell.

Details of the eo_get_booking_ticket_meta() function can be found here: http://codex.wp-event-organiser.com/function-eo_get_booking_ticket_meta.html

Adding columns to the tickets admin table

To add the collected data to the ticket table in the bookings admin page, first use the eventorganiser_booking_tickets_table filter to insert an extra column(s). This should be given a unique identifier to help identify it later. Below, we’ve added a ‘name’ column which we will use to display the first and last name of the ticket holder

add_filter( 'eventorganiser_booking_tickets_table', function( $columns ){
    $columns['name'] = 'Ticket owner';
    return $columns;
});

Next we’ll use the eventorganiser_booking_tickets_table_column filter to render the content of each cell in our column. Recall the meta key of the data is _eo_booking_meta_{question-id} where {question-id} in this case is attendee-name.

Once we’ve retrieved the name array (first and last name) we’ll use implode to print it:

add_action( 'eventorganiser_booking_tickets_table_column', function( $column_name, $item ){
    if( 'name' == $column_name ){
        $name = (array) eo_get_booking_ticket_meta( $item->booking_ticket_id, '_eo_booking_meta_attendee-name', false );
        echo implode( ' ', $name );
    }
},10,2);