Support

Home Forums Event Espresso Premium Hook Called Twice

Hook Called Twice

Posted: April 19, 2022 at 9:53 pm

Viewing 5 reply threads


scoutmaster254

April 19, 2022 at 9:53 pm

We wrote a custom function. This function creates a JSON and sends it to a Zapier webhook, which then creates a QuickBooks invoice with line items for registrations. We allow for purchasing through both a credit card (via Stripe) or with check. Check functionality is working perfectly, but Stripe sends two Zapier triggers. The first is at completion of the transaction as expected and the other is consistently an hour later (give or take a few seconds).

The function is called by being added to the hook “AHEE__EE_Transaction_Processor__update_transaction_and_registrations_after_checkout_or_payment”. We added a line to the function to trace why the function was being called twice. Specifically, $called_action_hook = current_filter(), the contents of which is emailed to us. Both at checkout and 1 hour later, we received an email stating that the hook had executed the function. It seems that something is firing the hook 1 hour after the transaction.


Tony

  • Support Staff

April 21, 2022 at 8:24 am

Hi there,

When AHEE__EE_Transaction_Processor__update_transaction_and_registrations_after_checkout_or_payment is called it will pass the current transaction object and an array of update parameters.

The update parameters will differ depending on why/how the function was called and they are there to help determine if code needs to be run from that specific call, are you comparing any of those values currently?

To explain, that hook will be called when the registration is finalized and also during the expired transaction check we run on a cron roughly 1 hour after the session is started (which is what you are finding above, its expected behaviour).

When do you want your code to send a request over to Zapier?


scoutmaster254

April 30, 2022 at 9:49 am

We’d like for the request to only be sent to Zapier when registration is finalized.


Tony

  • Support Staff

May 2, 2022 at 2:52 am

The problem is your definition of finalized sounds different to ours.

So may I ask, what do you consider a ‘finalized registration’?


scoutmaster254

May 2, 2022 at 9:19 am

When the user receives confirmation that the registration has been submitted. For check, this would be after completing the required questions (default registration status for our events is set to approved). For credit cards, it would be after completion of the registration form and collection of payment.


Tony

  • Support Staff

May 11, 2022 at 3:51 am

I did some digging into this comparing the $update_params passed to the hook in order to limit when the above function could be ran.

However, then I realised that the issue you are facing here is purely from the cron request so you can simply add a conditional to return out of your function if DOING_CRON, so add something like:

if( wp_doing_cron() ) {
    return;
}

To the beginning of your function, the request will still run on the initial call but will do nothing when the off-site cron runs again for Stripe.

Note that wp_doing_cron() was added to WordPress version 4.8.0, if you are running an older version than that (I’m not sure why you would be!) then the above function wont work.

Viewing 5 reply threads

The support post ‘Hook Called Twice’ 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