Support

Home Forums Event Espresso Premium EE4: Problem accessing EE objects from within a WP filter function

EE4: Problem accessing EE objects from within a WP filter function

Posted: May 26, 2020 at 9:02 pm


mbeede@tracom.com

May 26, 2020 at 9:02 pm

Greetings – I am using my own filter function to customize the message that is displayed upon registration success. I added my filter like this (on a tip that Josh gave me a few months ago):

add_filter( 'FHEE__thank_you_page_overview_template__order_conf_desc', 'my_custom_thank_you_text' );  

function my_custom_thank_you_text() 
{
  // Call a method to get the associated external session type
  $externalSessionType = getExternalSessionType();	  
  if (strcmp($externalSessionType, SESSION_TYPE_PROGRAM_SHOWCASE) == 0)
     return THANK_YOU_PROGRAM_SHOWCASE;
  else
     return THANK_YOU_DEFAULT;
}

function getExternalSessionType()
{
  // We need to get a few objects from Event Espresso
  $checkout = EE_Registry::instance()->SSN->checkout();
  if ( $checkout instanceof EE_Checkout ) 
  {
    $transaction = $checkout->transaction;
    if ( $transaction instanceof EE_Transaction ) 
    {
      foreach ( $transaction->registrations() as $registration )
      {
        if ( $registration instanceof EE_Registration ) 
	{
	   $event = $registration->event();
	   break; // Got the event
	}
      }
    }  
  }

  // Get the Session Type associated with this registration
  $type = get_post_meta($event->ID(), 'max_session_type_id', true);
  error_log(print_r("External Session Type = $type", true));
  return ($type);
}

The problem is that the $event object appears to be empty when I make these EE calls to get the information I need. When I use this same code earlier in the registration process it works. When I call it for this filter it doesn’t. Do the EE objects go away or get wiped out at some point in the registration processing? Any ideas why $event is no longer valid? Appreciate any help!!!


Tony

  • Support Staff

May 27, 2020 at 5:17 am

Hi there,

The problem is that the $event object appears to be empty when I make these EE calls to get the information I need.

You’ll likely find the problem isn’t $event, it’s $transaction.

The reason being is the code above uses the current session and pulls the checkout object from that, then pulls the transaction from checkout and so on. On the thank you page, the registration(s) has been finalized and the session closed, so SSN->checkout() will return null.

The reason it works prior to the thank you page is due to the session being open (which it needs to be for the registrations to work).

From the above, your hooking into the order description to output just an ID, is that visible?

The reason I ask is your code is using SSN to the EE_Transaction object related to the request, but there are multiple other hooks on the thank you page that are already passed the current EE_Transaction object, for example:

do_action('AHEE__thank_you_page_overview_template__top', $transaction);
do_action('AHEE__thank_you_page_overview_template__content', $transaction);
do_action('AHEE__thank_you_page_overview_template__bottom', $transaction);

They obviously don’t fire for the order confirmation description but could you not use one of those for what you are trying to do?

If not, it is possible to pull the EE_Transaction object from the values passed in the query string when the thank you page is loaded for a specific registration and we have some details on how here:

https://github.com/eventespresso/event-espresso-core/blob/master/docs/T–Tutorials/customizing-the-ee-thank-you-page.md


mbeede@tracom.com

May 27, 2020 at 9:40 am

Tony – My goal is to change the thank you message that is displayed by default on the reg. confirmation page. Josh told me a while back that I had to tie into that EE/WP hook to be able to do that. Let me back up a little…

We have EE events created in WordPress for each different type of training that we offer. Each type of training is associated with an ID. That ID is entered as part of creating each event in EE. We add it as an extra field for the event.
In my PHP code I access that ID using the following code that Josh gave me a while back…

$externalSessionType = get_post_meta($event->ID(), ‘max_session_type_id’, true);

So I need the EE event object in order to get at that extra ID. As you stated if the transaction is longer there in that WP hook I can’t get at EE event object. Is there another way to get that ID that would work within that hook filter method?

FYI – I also tried using a global variable to solve this. So earlier on I get the ID using the code above then stored it in a PHP global variable, thinking it would be there when I need it. But that didn’t work either. When I go to access the global variable in my hook filter method it is gone. Not sure why.

Is there any other way to change the confirmation page thank you message that you can think of? That’s all I need to be able to do…

Thanks in advance – Mark


Tony

  • Support Staff

May 27, 2020 at 10:09 am

So I need the EE event object in order to get at that extra ID. As you stated if the transaction is longer there in that WP hook I can’t get at EE event object. Is there another way to get that ID that would work within that hook filter method?

Ok, so you need to do it within the filter hook you mentioned then and yes there is a method to do just that included in the documentation link I gave you earlier:

https://github.com/eventespresso/event-espresso-core/blob/master/docs/T–Tutorials/customizing-the-ee-thank-you-page.md#getting-the-transaction-data

You’d basically be replacing the first 5 lines of your getExternalSessionType() function with:

$transaction = EE_Registry::instance()->load_model( 'Transaction' )->get_transaction_from_reg_url_link();

You also don’t need to loop over all registrations from the transaction (I know you break the loop) but you can just use $transaction->primary_registration(); to pull the primary registrant object and then the event from that.

FYI – I also tried using a global variable to solve this. So earlier on I get the ID using the code above then stored it in a PHP global variable, thinking it would be there when I need it. But that didn’t work either. When I go to access the global variable in my hook filter method it is gone. Not sure why.

If I understand correctly, that global variable wouldn’t be set on the thank you page request as you set it on the attendee info/payment options step.


mbeede@tracom.com

May 27, 2020 at 1:27 pm

Tony – The code that you referred me to did the trick. Thanks for your help!!!


Tony

  • Support Staff

May 27, 2020 at 4:29 pm

You’re most welcome, I’m glad it works for you.

The support post ‘EE4: Problem accessing EE objects from within a WP filter function’ 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