Support

Home Forums Event Espresso Premium Grabbing event and user information after payment has been received

Grabbing event and user information after payment has been received

Posted: September 7, 2023 at 9:09 am


Zoe

September 7, 2023 at 9:09 am

Hi all, I need to create a function that does the following, could I get some guidance on what hook I need and the quickest way to grab the info please?

This needs to fire when a payment has been made (and stripe has confirmed the payment has been received).

Then I need to be able to:
– Loop through all events on the order
– If one of those events appears in a specific event category then get:
– The Event ID
– The delegates/users name
– The delegates/users email address

For this specific type/category of event we are limiting it to one per order, so there would only ever be 1 delegate against the event.

Thank youuuuu!

Zoe


Brent Christensen

  • Support Staff

September 11, 2023 at 12:59 pm

Hi Zoe,

A good hook to use would probably be:

AHEE__EE_SPCO_Reg_Step_Finalize_Registration__process_reg_step__completed

which gives you access to:

  1. the EE_Checkout object which has access to pretty much every bit of data collected during the registration process, including:
    1. $cart the ‘EE_CART’ object
    2. $transaction the ‘EE_Transaction’ object
    3. $primary_attendee_obj the ‘EE_Attendee’ object for the primary registrant
    4. $payment the ‘EE_Payment’ object
  2. plus an array of details from the transaction, which you likely don’t need

you can check the status of the payment using $payment->status() which you can compare against one of the payment status codes on EEM_Payment.

Then to do the following:

– Loop through all events on the order

you can get the list of registrations from the transaction object:


$registrations = $transaction->registrations();

then get the events from the registrations:


foreach ( $registrations as $registration ) {
    $event = $registration->event();
}

– If one of those events appears in a specific event category then get:

if ( $event->category() === 'my category' ) { ... }

– The Event ID

$event->ID()

– The delegates/users name

$registration->attendee()->full_name()

– The delegates/users email address

$registration->attendee()->email()

Hope that helps!


Zoe

September 12, 2023 at 9:30 am

Thanks Brent this is really helpful! Where can I get a list of payment codes for EEM_Payment? We’ll be using stripe so I’ll just be looking for whatever confirms stripe successfully took payment.

Cheers 🙂

Zoe


Tony

  • Support Staff

September 12, 2023 at 2:42 pm

Where can I get a list of payment codes for EEM_Payment?

As soon as you open up EEM_Payment.model.php you’ll see the payment statuses available, like THIS.

We’ll be using stripe so I’ll just be looking for whatever confirms stripe successfully took payment.

You don’t actually need the Stripe specific payment status as the EE_Payment object will have been updated at that point, you’d be looking to confirm the transaction status is complete and if checking individual EE_Payment objects you ca use something like:

if ($payment->status() === EEM_Payment::status_id_approved) {
    //This payment has a status of Approved.
}

Also, depending on the specifics of what this is for you could also just hook into when a registration is Approved, within EE_Registration.class.php there is a set_status() method, that is used to set the registration status which happens automatically when a payment is made in full (although also can happen at other times).

So this action:

do_action('AHEE__EE_Registration__set_status__to_approved', $this, $old_STS_ID, $new_STS_ID, $context);

Fires when a registration status is set to Approved, $this, will be an EE_Registration object. $old_STS_ID is what thee status was before this recent update, $new_STS_ID is what its just been set to (In this case it will be Approved) and $context gives you more details on what is updating the status.

It really depends on the specifics of what you are using these for, Brents hook also works fine so this is just another option.


Zoe

September 27, 2023 at 2:53 am

Thanks both, we’ve written this function but it’s causing console errors on the checkout! I’m sure we’re doing something daft wrong…

The payment->status if statement is commented out at the moment but we will be needing it, so confirmation on whether that is correct would be appreciated too 🙂

add_action( 'AHEE__EE_SPCO_Reg_Step_Finalize_Registration__process_reg_step__completed', 'my_function', 10, 2 );

function my_function( EE_Checkout $checkout) {

  //do I need these??
  $transaction = $checkout->transaction;
  $payment = $checkout->payment; 

  $payment_status = $payment->status();

  //if the payment status is complete
  //if ($payment->status() === EEM_Payment::status_id_approved) {

    //loop through all events purchased
    $registrations = $transaction->registrations();
    foreach ( $registrations as $registration ) {
      $event = $registration->event();

      //if an event was elearning
      if ( $event->category() === 'elearning' ) {
        
        //get all the details
        $the_event = $event->ID();
        $usersName = $registration->attendee()->full_name();
        $usersEmail = $registration->attendee()->email();

        $productID = get_field('product_id',$the_event);
        $productPrice = get_field('product_price',$the_event);

        //register them with Learnworlds
        create_user_enroll($productID, $productPrice, $usersName, $usersEmail);
      }
    //}
  }
}

Zoe


Tony

  • Support Staff

September 27, 2023 at 6:44 am

Can you post the errors you are getting in the console?

Also, if you check your server’s PHP error logs when testing the above code, does that show any errors there?

The fact that you’ve commented out:

if ($payment->status() === EEM_Payment::status_id_approved) {

Leads me to believe the code now works without that check? If so and you getting errors when that code is in use, my guess right now is that $payment isn’t an instance of EE_Payment there.

Output the contents of $payment (or write it to the log file) and see what it is.


Zoe

September 29, 2023 at 4:46 am

Hi Tony, thanks for helping – we’ve commented in the if statement and tested again, but it still errors.

It errors once we enter stripe (test) payment details and press “Pay Now” – the cog appears and it just remains on screen indefinitley.

This is the console error:

single_page_checkout.js?ver=5.0.7.p:884 
Uncaught TypeError: Cannot set properties of undefined (setting 'success')
    at Object.success (single_page_checkout.js?ver=5.0.7.p:884:57)
    at fire (jquery.js?ver=3.7.0:3213:31)
    at Object.fireWith [as resolveWith] (jquery.js?ver=3.7.0:3343:7)
    at done (jquery.js?ver=3.7.0:9617:14)
    at XMLHttpRequest.<anonymous> (jquery.js?ver=3.7.0:9878:9)

The only thing appearing in the debug log is [29-Sep-2023 10:42:57 UTC] CART (4) – we’ve added error_log($payment); – so presumably $payment is empty? How would we resolve that?

The server error logs are also clear.

Thanks,

Zoe


Tony

  • Support Staff

September 29, 2023 at 5:53 am

But it works as expected if you completely remove the above function?

For example if you comment this out:

add_action( 'AHEE__EE_SPCO_Reg_Step_Finalize_Registration__process_reg_step__completed', 'my_function', 10, 2 );

So that the code doesn’t run there’s no more console error?


Zoe

September 29, 2023 at 6:18 am

Hi Tony, that’s correct! We’ve made it as an mu-plugin for now and if we delete the file and follow the same steps to register it works fine with no console errors and takes us to the thank you page…


Tony

  • Support Staff

September 29, 2023 at 6:49 am

It’s working for me but I’ve commented out all of the code with the category check as I don’t have the same setup.

Add this function to that file:

if ( ! function_exists('tw_ee_write_log')) {
   function tw_ee_write_log ( $log )  {
      if ( is_array( $log ) || is_object( $log ) ) {
         error_log( print_r( $log, true ) );
      } else {
         error_log( $log );
      }
   }
}

Then use tw_ee_write_log($payment);

You should get some more details on what that object is.

Or just error_log( print_r( $payment, true ) ); but the helper function is easier to reuse imo.


Tony

  • Support Staff

September 29, 2023 at 6:57 am

Actually… that’s why!

Where are you getting $event->category() from? We don’t have a category method on EE_Event.

$category = $event->first_event_category();
if ($category instanceof EE_Term) {
    if ( $category->name() === 'elearning' ) { 
        //get all the details
        ....
    }
}


Zoe

September 29, 2023 at 8:52 am

Beautiful! Thanks Tony, it’s now working 😀

Thanks for your help today, I really needed to get this boxed off and we’ve managed it 🙂


Tony

  • Support Staff

October 4, 2023 at 3:59 am

Just an additional note as I had a fatal on my test site running the above code.

This: $payment_status = $payment->status();

Will fatal if the registration is on a free ticket, as there is no payment so $payment will be null. So so will the conditional check you have on for the status here:

if ($payment->status() === EEM_Payment::status_id_approved) {

Get rid of this $payment_status = $payment->status(); as you aren’t using that variable anyway.

Change the conditional to check for a EE_Payment object:

if (
    $payment instanceof EE_Payment
    && $payment->status() === EEM_Payment::status_id_approved
) {

Confirm the create_user_enroll() function is still running when you expect it to.


Zoe

October 5, 2023 at 6:22 am

Good spot, thanks Tony, we’ll change this and test it 🙂

The support post ‘Grabbing event and user information after payment has been received’ 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