Support

Home Forums Event Espresso Premium Introspecting ticket information upon event creation

Introspecting ticket information upon event creation

Posted: July 7, 2021 at 1:40 pm


skillstat

July 7, 2021 at 1:40 pm

Hi there,

Im doing a Woocommerce integration and need to introspect ticket information when the wp post associated with the EE_Event is created or updated.

Im using the standard ‘save_post_espresso_events’ hook and getting an instance of the EE_Event with ‘$theEvent = EEM_Event::instance()->get_one_by_ID($post_id);’, which returns an event with basic info like name and description in it; However, $theEvent->tickets() returns an empty array. Its only after I resave/update the post the mentioned code returns a populated array of EE_Ticket items.

Perhaps Im acquiring the EE_Event object incorrectly? Or the ticket info doesn’t get saved until later? Is there a different hook I should be using that gets called after the ticket info is saved/available?

– Morgan


Tony

  • Support Staff

July 8, 2021 at 5:09 pm

Hi Morgan,

Tbh, I’m surprised save_post (and therefor save_post_espresso_events) doesn’t work for this as I’d expect it to.

After a little digging we have another option for you:

add_filter(
    'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
    function ($callbacks) {
        $callbacks[] = 'tw_ee_insert_update_event';
        return $callbacks;
    },
    20
);

function tw_ee_insert_update_event( EE_Event $event, array $data ) {
    var_dump($event->tickets());
}

The above callbacks are used whenever an EE event is created/updated and the $event object is passed directly so no need to for EEM_Event.


skillstat

July 9, 2021 at 3:42 pm

Thank you for the code; Unfortunately Im getting the same results!

Here is my code (organized in an SS_EEWC_BRIDGE class)

//In the constructor
add_action(‘FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks’, [‘SS_EEWC_BRIDGE’,’insertUpdateEventCallback’], 10, 1);

//Then…..

static public function insertUpdateEventCallback($callbacks){
$callbacks[] = [‘SS_EEWC_BRIDGE’, ‘ee_event_updated_callback’];
return $callbacks;
}

static public function ee_event_updated_callback($ee_event){
if($ee_event instanceof EE_EVENT){
error_log(“Event Updated Callback—“);
$tickets = $ee_event->tickets();
error_log(“Ticket Count:”. count($tickets));
}
}

Steps:
1) Create a new EE Event with one ticket. Hit the ‘Publish’ button.

On the console I get:
[09-Jul-2021 21:31:21 UTC] Event Updated Callback—
[09-Jul-2021 21:31:21 UTC] Ticket Count:0

2) Hit the publish button again (which now is labeled ‘Update’)

Console output:
[09-Jul-2021 21:31:57 UTC] Event Updated Callback—
[09-Jul-2021 21:31:57 UTC] Ticket Count:1

I should also note that neither the ‘trashed_post’ nor ‘wp_trash_post’ hooks fire when I delete an EE Event!

– Morgan


skillstat

July 9, 2021 at 4:03 pm

I should note, with my original code, I tried setting the priority number in the add_action() call to something really high…10000… and then 100000; That didn’t work either.


Tony

  • Support Staff

July 12, 2021 at 8:47 am

Hmm, strange.

I tested my snippet before I replied above and it works for me (just confirmed again to check), as long as you have a priority of 11 or more it should work on both event publish an update.

May I ask, how are you initializing the class above? I’ll try using the same method.

Creating a new event, empty debug log:
https://monosnap.com/file/ntayv9hd4OEJtCJ4uryzVzWCj0poJY

Hitting publish:
https://monosnap.com/file/a2HOK9mPOIgOwwpsobp8YWIdyymO6u

Sidenote – whilst add_action() will work as its basically an alias for add_filter() switching to add_filter() is a little clearer here as your hooking into a filter, not an action.


skillstat

July 13, 2021 at 6:59 am

So frustrating… I tried turning off all the plugins.. rewrote it without the class wrapper… rewrote it again… once more for good measure, lol. I finally figured it out!

So, I never actually tried ‘save_post’; I went straight to using ‘save_post_espresso_events’. It appears the tickets are available right away with ‘save_post’, but not with ‘save_post_espresso_events’!

Thanks for the quick responses and trying it yourself in code. Much appreciated.

– Morgan


Tony

  • Support Staff

July 13, 2021 at 7:37 am

Hmm, so, first… nice find!

I’m not sure why the FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks hook works for me but is still too late on you site.

However, I checked into save_post and it is because save_post_espresso_events fires before save_post and as EE hooks into save_post it makes sense for what you are trying to do above.

Also, a polite reminder (although I’m sure you already are) to make sure you check the post type when using save_post.

The support post ‘Introspecting ticket information upon event creation’ 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