Support

Home Forums Event Espresso Premium Attempting to fetch Attendees email address on successful registration

Attempting to fetch Attendees email address on successful registration

Posted: November 28, 2020 at 4:08 pm

Viewing 5 reply threads


SCCDS

November 28, 2020 at 4:08 pm

Hello so I’ve had a look around the forum and I’ve found maybe a handful of questions that come close to this, I even found some documentation for the get_all() function but things generally seem scattered on the topic Im going to ask about. So basically, what I’m trying to accomplish is within the thank you page’s content, I am attempting to fetch and display emails for attendees associated with the transaction.

Here is a link to a github repo where I inserted the relevant snippet of code from my functions.php file.

https://github.com/carlofabyss/SCCDS-EE-Attendees/blob/main/minifunctions.php

So I approached this in a pretty procedural way, first by trying to get relevant data that’s being posted to this event

And then by doing a query for transactions using the get_all function which has a parameter referencing the transaction id and then iterating through the results to attempt to get the ATT_EMAIL value. So I tried to do so in the same way that some of the documentation says to do for ATT_ID, but for the life of me nothing has worked past this point.

So I was wondering if you guys could guide me in the right direction for this one. There’s probably some stuff in this code that doesn’t need to be here either but I’m just focusing on getting the relevant data right now.


SCCDS

November 28, 2020 at 4:12 pm

I am also using EE4


Tony

  • Support Staff

November 30, 2020 at 4:55 am

Hi there,

Without trying to bash your code here, there is a fair amount wrong with that code.

function eccent_ee_transaction_success($event_queue) {

Why $event_queue?

The AHEE__thank_you_page_overview_template__content hook is passed the EE_Transaction object which is all you need to pull all of the information you need, but using it as an event_queue is misleading as that’s not what it is. Switch that to $transaction and use is as the EE_transaction for the current registrations.

Meaning $reg = $event_queue->registrations();

Now becomes $regs = $transaction->registrations();

It makes more sense now that you’re pulling the registrations from the transaction into $regs, right? So its an array for EE_Registrations linked to the current EE_Transaction, even if that’s just a single registration.

foreach ($reg as $key) {
    // loop through and use the get method to extract the protected data
    // echo $key->get('TXN_ID'); 
    // echo '<br>';
    array_push($txn_ids, $key->get('TXN_ID'));
}

echo '<strong>Transaction IDS From This Transaction:</strong><br>';
echo var_dump($txn_ids);
echo '<br><br>';

$transactions = EEM_Transaction::instance()->get_all(
    array(
        'limit' => 10,
        array(
            'STS_ID' => 'TCM',
                'TXN_ID' => $txn_ids[0]
        ) 
    ) 
);

None of this is needed, your using the EE_Transaction to pull all of the registrations linked to that transaction, to then pull an EE_Transaction using the TXN_ID from each of the regs (which will be exactly the same for them all) to then use get_all on the EEM_Transaction model to pull the exact same EE_Transaction that is currently passed to the filter.

So, $transaction already holds this object for you and you can skip all of this.

This:

echo '<strong>Attendee Data Using First Index of Transaction IDS:</strong><br>';
foreach( $transactions as $transaction ) {
    // echo 'txn_id: '.$transaction->ID().'&nbsp;';
    $reg_att = $transaction->get_first_related( 'Registration' );
    if ( $reg_att instanceof EE_Registration ) {
            $att = $reg_att->attendee();
            if ( $att instanceof EE_Attendee ) {
                $att_id = $reg_att->attendee()->get( 'ATT_ID' );
                echo var_dump($att);

                //for($x = 0; $x < count($att); $x++) {
                //    $att_temp = $att[$x];
                //    echo $att_temp;
                //}

                //$args = array(
                //    'meta_key'   => $wpdb->prefix . 'EE_Attendee_ID',
                //    'meta_value' => $att_id,
                //);
                //$wp_user_query  = new WP_User_Query( $args );
                //if ( ! empty( $wp_user_query->results ) ){
                //    $users = $wp_user_query->get_results();
                //    foreach ( $users as $user ) {
                //        echo 'User ID: ' . $user->ID . '<br>';
                //    }
                //} else {
                //    echo 'No user found<br>';
                //}
            }
        }                               
    }
}

Is backwards within Event Espresso, the EE_Transaction groups the registrations so you use the EE_Transaction to pull EE_Registrations, then loop over the registrations to pull the data relevant to the specific registrations (including the attendee as that is related to the registration).

Your looping over ‘transactions’ (which will actually just be one) and then using the transaction, to pull the first related registration (which make sense, but not within this loop) to pull the attendee etc.

Again, my intention is not to pick away at your code here but if I don’t give you the above details you’ll be going around in circles for nothing. You need to take a look at the documentation for the model system here:

https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System

The reason I say that is the models make this really simple to do, but you obviously need to understand the models to do it. In this case, most of your code above can be replaced with:

$regs = $transaction->registrations();
foreach( $regs as $reg ) {
    if( $reg instanceof EE_Registration ) {
        $attendee = $reg->attendee();
        if( $attendee instanceof EE_Attendee ) {
            echo $attendee->email();
        }
    }
}

With regards to your WP_User_Query, there’s a method already available to do that query within the WP User integration add-on (which I assume you have active otherwise it wouldnt’ be worth pulling that data :))

See EE_WPUsers::get_attendee_user($attendee->ID());

So your whole snippet becomes something like:

function eccent_ee_transaction_success($transaction) {
    //Pull all the registrations related to the transaction.
    $regs = $transaction->registrations();
    // Loop over each registration.
    foreach( $regs as $reg ) {
        // Confirm we have an EE_Registration object.
        if( $reg instanceof EE_Registration ) {
            // Pull the EE_Contact liked to the registration.
            $attendee = $reg->attendee();
            if( $attendee instanceof EE_Attendee ) {
                // Use the EE_Contact to output the email address.
                echo $attendee->email();
                // Or whatever else you want to do with the attendee details here.
               
                // Do we have a User account linked with this EE_Contact?
                $user_id = EE_WPUsers::get_attendee_user($attendee->ID());
                if(! empty($user_id) ) {
                    //$user_id holds the ID of the WP_User account liked to this EE_Contact, do something here.
                }
            }
        }
    }
}
add_action('AHEE__thank_you_page_overview_template__content', 'eccent_ee_transaction_success', 10, 1);

Using the models to pull the data in the way it is intended to be makes the code much easier to follow here.

What I usually recommend you do when trying to find the methods on the objects you have available is use kint:

https://wordpress.org/plugins/kint-debugger/

Wrap the object in d(); and you’ll get a really nice output showing all of the methods available and various other info. So:

$attendee = $reg->attendee();
d($attendee);

Would have given you a full list of the methods available on the object, such as email().

Side note – all of the code above is untested, I’ve written it here on the forums based on your snippet, there may well be a typo or syntax error above that I’m missing.


SCCDS

November 30, 2020 at 1:51 pm

Thanks yes Tony no worries on knocking of the code, you are absolutely right, this is something of a frankenstein of resources that I came across and thus why I eventually came here for help. Essentially yes I didn’t quite get the structure of the object model and just went off of intuition and attempting to decode the array that got spit out. I’ll go ahead and try out your suggestions and report back with a yay or a nay ๐Ÿ™‚


SCCDS

November 30, 2020 at 3:43 pm

I think we can mark this a resolved. Tony was awesome and pretty much knocked out the query on the first try. Your suggestion about the kint debugger is also awesome, I was already able to move further along on what this is supposed to become. Revealing methods on certain objects using d($varname) sheds alot of light on the structure of EE objects.


Tony

  • Support Staff

December 1, 2020 at 2:09 am

Great, I’m glad it worked well for you.

As for Kint, I’m a fan ๐Ÿ™‚ If you’re using an IDE like PHPStorm you get access to a lot of info already, but I often still use Kint to throw a variable to the page and play around with it.

One additional tip with Kint and the EE Models is don’t use the + button…. ever. Because the models group other models together if you hit the + button (top left) on any of the sections it automatically expands all children within that section and that can be a lot of elements. Depending on what you’re running it on it can load so many elements it crashes the browser but, the simple workaround is to click on the element name itself to open just that section, then again on any child elements you need to view and so on.

Viewing 5 reply threads

The support post ‘Attempting to fetch Attendees email address on successful registration’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso