Search

Managing Registrations in Event Espresso: Bulk Import Users

Listen to this article

register-event-espressoManaging events on a WordPress website is best done with Event Espresso. It’s an excellent plugin; it’s feature rich and lets you take complete control of all the event-related data, right from tickets to event dates to tax administration.

With Event Espresso, it’s also possible to manage event Registrations. A front end user can Register him/herself for an event as follows:

[space]

[space]

[space]

  1. Navigate to the event ticket selection page.
    [space]
  2. After selecting the ticket, click Register Now.
    [space]
  3. This leads to a registration form, to be filled in by the user. Event Espresso lets an Admin custom design this form as per requirements, with the fields First Name, Last Name and Email being mandatory.
    [space]
  4. Once the payment process is completed, the user will then be registered to the event, with the payment status as either approved or not-approved.

This is fairly easy, when considered one user at a time. But let’s be real, a particularly popular event will probably the number of attendees running into hundreds, potentially thousands. Registering them one by one would no longer be feasible, so how about we simply import all of their data in the form of a CSV file?

 Plugin for Registrations in Event Espresso Through CSV

Theoretically, all we have to do is create a plugin that provides us with the requisite functionality. This is done using one and only one single hook provided by Event Espresso. But why this particular hook, and not others? Because, the hooks used in such processes requires data which we cannot generate on our own e.g. registration URL link, registration code etc. What are those? We’ll get to that in a moment.

Table Management

Before we move on to the part involving the actual plugin development, we ought know about the tables in database used in managing these registrations. They are crucial to performing this task, and are as follows:

  1. {prefix}_posts
  2. {prefix}_esp_attendee_meta
  3. {prefix}_esp_transaction
  4. {prefix}_esp_registration

Let’s examine these in detail:

1) {prefix}_posts

This is a wordpress table used to manage posts. Is it, then, really relevant to what we’re doing here? Yes, it is. This is the table where our registration process is initiated. When we register a user for an event, a post with custom post_type as espresso_attendees is automatically created, containing a listing of all the attendees for the event. The post_title would be Name of attendee and the post_id of the post involving the attendee is the ID for the attendee.

2) {prefix}_esp_attendee_meta

This is the table where basic information of the attendee is stored while registering. The  fields set by administrator in the registration form are the meta_keys in this table.

3) {prefix}_esp_transaction

This is the table where the registration status of the attendee is maintained, as per the pricing of the ticket selected. It’s possible to specify the  ticket quantity in the CSV file. And we can manage the payment_status by specifying the STS_ID parameter.

4) {prefix}_esp_registration

This is perhaps the most important table when considering registrations. This is where the actual registration is recorded and from where the dashboard fetches the registration data to display.

The two important aspects about this table, to be dealt with while coding the plugin, are namely: reg_code and reg_url_link. The reg_url_link is nothing but a random text generated for security purposes. It is generated using md5() method of php. Final reg_url_is as follows:

  att_num-reg_url_link.

   e.g. 1-3e61569177e94571132d2e5f22a96686

Here, 1 is the number of attendees we registered and the string 3e61569177e94571132d2e5f22a96686 is a randomly generated text via the md5 function.

The second factor i.e. reg_code is dependent on the reg_url as follows:

 reg_id-tkt_id-first_six_characters_of_reg_url_link

   e.g. 202-9-1-3e61

In this example, 202 is the registration ID, 9 is the ticket id and 1-3e61 are the first 6 characters of afore mentioned url_link.

Plugin Creation

About time we start talking about the hook using which we write the function to send all of the registration data to the database. The filter hook involved is FHEE__EE_SPCO_Reg_Step_Attendee_Information__save_registration_form_input.

This filter passes 5 parameters as follows:

1) $bool = boolean variable

2) $registration = object of EE_registration class

3) $form_input = object of form to know the fields of registration

4) $input_value = the values to be sent to processing

5) $obj = process handling object

We write our main function i.e. the function to create a UI for uploading the CSV file, prepare the data for registration on this filter and then return the data prepared.

The format of CSV file is to be maintained in the following manner:

Event ID Ticket ID First Name Last Name E-mail
122 123 9 10 PQR XYZ [email protected]
124 12 ABC DEF [email protected]

Here’s an example:

Event-Espresso-bulk-registration-csv-file

If a user is to be registered for multiple events, you don’t need to create another row specifically for the user. Instead, you can add the event IDs and the corresponding ticket IDs separated by a space in the CSV.

Extraction of data from CSV

The data from CSV can be extracted as follows:

1) Create a form with enctype as multipart/form-data and method=post tags

   <form method=”POSTenctype=”multipart/form-data”>

    </form>

2) Then create an input to choose files in the same way you create for taking an input of text

 <input type=”fileaccept=”.csvname=”uploadCSV”/>
 <input type=”submit” />


3) This submit button will set the $_FILE variable so that we can access the file chosen.

4) Now, the following piece of code will extract the data from CSV into $rows_from_csv array

    if ( isset( $_FILES[ 'uploadCSV' ] ) ) {
        $file_name     = $_FILES[ 'uploadCSV' ][ 'name' ];
        $file         = fopen( $_FILES[ 'uploadCSV' ][ 'tmp_name' ], "r" );


        if ( $file ) {
        while ( ! feof( $file ) ) {
            $rows_from_csv[] = fgetcsv( $file, 1024 );
        }
        } else {
        echo "file not found";
        }
        fclose( $file );
     }

5) Here on, we can use this array $rows_from_csv in conventional ways.

6) Note that our file object is closed.

Structuring the Data

Up next, we structure the extracted data to be updated in Event Espresso database. This is done in 3 segments:

1) We don’t need special customizations for data pertaining to posts and esp_attendee_meta table. The data extracted from the CSV file is sent to database directly as per its acquisition.

e.g. The data for these tables was sent directly to the database using global variable.

/*     * ************* Inserting in wp_posts table-Starts ********************** */
    $wpdb->insert(
    $wpdb->prefix.'posts', array(
        'post_title'     => $data[ 'fname' ] . " " . $data[ 'lname' ],
        'post_type'     => 'espresso_attendees',
        'post_content'     => 'No Biography Provided',
        'post_excerpt'     => 'No Biography Provided',
        'post_author'     => $author,
        'post_name'     => strtolower( $data[ 'fname' ] ) . "-" . strtolower( $data[ 'lname' ] ),
    ), array(
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s'
    ) );

    /*     * ************* Inserting in wp_posts table-Ends **************** */

    /*     * ************* Inserting in wp_esp_attendees table -Starts ******** */
    $att_id = $wpdb->insert_id;
    $wpdb->insert(
    $wpdb->prefix.'esp_attendee_meta', array(
        'ATT_ID'     => $att_id,
        'STA_ID'     => 0,
        'ATT_fname'     => $data[ 'fname' ],
        'ATT_lname'     => $data[ 'lname' ],
        'ATT_email'     => $data[ 'email' ],
    ), array(
        '%s',
        '%d',
        '%s',
        '%s',
        '%s'
    ) );
    /*     * **** Inserting in wp_esp_attendees table - Ends ****************** */

2) To prepare the data for ticket price, the ticket_id extracted from the CSV file was used. By default, the ticket_price itself was the final registration cost, since registration was limited to one user. Extracting the ticket_price and consequently storing the data is the transaction table is done using the following lines of code:


/*     * ******* Inserting data into wp_esp_transaction table - Starts *************** */

    $tkt_price_obj     = $wpdb->get_row( 'select TKT_price from wp_esp_ticket where TKT_ID=' . $ticket );
    $tkt_price     = floatval( $tkt_price_obj->TKT_price );


    $wpdb->insert(
    'wp_esp_transaction', array(
        'TXN_timestamp'     => date(),
        'TXN_total'     => $tkt_price,
        'TXN_paid'     => $tkt_price,
        'STS_ID'     => 'TCM',
    ), array(
        '%s',
        '%f',
        '%f',
        '%s'
    ) );

    /*     * ****** Inserting data into wp_esp_transaction table - Ends ************* */

3) This is the most crucial part regarding the preparation of data; it deals with preparing the reg_url_link and reg_code and sending it for storage in the  database. Take a look at the codes below.

   Firstly, we create the reg_url_link as follows:

    $item;
    $reg_url_link     = $item instanceof EE_Line_Item ? $item->code() : $item;
    $reg_url_link     = $att_num . '-' . md5( $reg_url_link . Microtime() );

We then use this reg_url_link for the creation of reg_code as follows:

   $reg_code = $txn_id . '-' . $ticket_id . '-' . substr($reg_url_link, 0, 6 );

  Sending the data to the registration table is done as follows:

  /*     * ********* Inserting data into wp_esp_registration table - Starts ******** */
    $txn_id         = $wpdb->insert_id;
    $evt_id         = $event;
    $ticket_id     = $ticket;
    $sts_id         = 'RAP';
    $att_num     = 1;

    $item;
    $reg_url_link     = $item instanceof EE_Line_Item ? $item->code() : $item;
    $reg_url_link     = $att_num . '-' . md5( $reg_url_link . microtime() );

    $reg_code = $txn_id . '-' . $ticket_id . '-' . substr( $reg_url_link, 0, 6 );

    $wpdb->insert(
    $wpdb->prefix.'esp_registration', array(
        'EVT_ID'         => $evt_id,
        'ATT_ID'         => $att_id,
        'TXN_ID'         => $txn_id,
        'TKT_ID'         => $ticket_id,
        'STS_ID'         => $sts_id,
        'REG_date'         => date(),
        'REG_final_price'     => $tkt_price,
        'REG_paid'         => $tkt_price,
        'REG_code'         => $reg_code,
        'REG_url_link'         => $reg_url_link,
    ), array(
        '%d',
        '%d',
        '%d',
        '%d',
        '%s',
        '%s',
        '%f',
        '%f',
        '%s',
        '%s',
    ) );

    /*     * ****** Inserting data into wp_esp_registration table - Ends ************ */

There you go! The plugin is ready to roll and should work flawlessly. Take a look.

Uploading the sample table shown above displays the data as follows:

Event-Espresso-bulk-registration-table

And once you hit upload, the entries will automatically be uploaded for the corresponding event:

Event-Espresso-bulk-registration

Managing multiple registrations is now easy; you’ll not only be able to register multiple users, but also multiple events for multiple users and book their respective tickets, all at a single click. Saves you a lot of time, and certainly a lot clicks.

Enjoy your Espresso!

Ketan Vyawahare

Ketan Vyawahare

2 Responses

  1. Hi, is this still working with the actual verion of event espresso? I would be interrested in using this plugin. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads

    WordPress Tips & Tricks
    Ketan Vyawahare

    How to Make Responsive Tables using CSS without Tag Read More »