Support

Home Forums Event Espresso Premium Hook / Code to add user to Buddyboss group on event registration

Hook / Code to add user to Buddyboss group on event registration

Posted: March 9, 2022 at 3:25 pm

Viewing 5 reply threads


c.barthe@futurelearning.fr

March 9, 2022 at 3:25 pm

I am trying to add a user to a Buddyboss group when they successfully register for an event. I have used ACF to create a field for the event – called ‘event_sub_group’. This allows the admin to choose a BuddyBoss group on the event page.

This is set to return the post id of the selected group.

I then have the following in my site plugin:


add_action('AHEE__EE_Registration_Processor__trigger_registration_update_notifications',
function($registration, $additional_details) {
// check if group is not null
//If so, add the user to the group
global $bp, $wpdb;
$user_id = wp_get_current_user();

if ( $registration instanceof EE_Registration ) {
//get event id from registration object
$event_id = $registration->event()->ID();
}

$subgroup = get_field('event_sub_group', $event_id);

if (!empty($subgroup)) {
if ( ! groups_is_user_member($user_id, $subgroup )) {
$bp->groups->current_group = groups_get_group(array('group_id' => $subgroup));
groups_join_group($subgroup, $user_id);
}
}
},
10,
4
);

I have also tried using the hook ‘AHEE__EE_Registration__set_status__after_update’ with no success. I know the code to add a user to a group works as I use it elsewhere

Am I getting something totally wrong? (I’m not overly experienced!)

Thanks in advance!


Tony

  • Support Staff

March 10, 2022 at 4:36 am

Hi there,

Before going into details for this a little further, I need to ask:

I am trying to add a user to a Buddyboss group when they successfully register for an event.

Can you define what you mean by ‘successfully register for an event’, please?

It means different things to different people and the hook you use will depend on your definition.

For example are the tickets paid or free?

If paid, is a successful registration only after the registration has been paid for and the registration status set to Approved or should the above group be set as soon as they start a registration?

Just some notes to help with your code when using the models:

—–

if ( $registration instanceof EE_Registration ) {
    //get event id from registration object
    $event_id = $registration->event()->ID();
}

First, instanceof checks are always a good idea, nicely done.

The EE_Registration class has a method for grabbing the event id:

$event_id = $registration->event_ID();

Why is that different? Because $reg->event()->ID() pulls the event object from the registration using the event_id, then the runs the ID() method on that object.

event_id() just pulls the event_id value from the EE registration (which is what was used above to pull the event) and uses the value. Just more efficient in this case as you aren’t using the Event object for anything more than the ID πŸ™‚

$subgroup = get_field('event_sub_group', $event_id);

$event_id wont exists if $registration wasn’t an EE_Registration, granted there are bigger problems if thats the case but if you taking the time to battle harden your code you may as well do it throughout.

Either declare it somewhere or, what I would do is something like:


if (! $registration instanceof EE_Registration ) {
    // No EE_Registration, get out
    return;
}

//From this point on we know $registration is an EE_Registration object and can use it as such without issue.
$event_id = $registration->event_ID();
$subgroup = get_field('event_sub_group', $event_id);

Small change but exists out of your code if it doesn’t have the data it needs (no point running any of the above group code if you don’t have an event_id).

The rest looks fine assuming you need to set the global current group, but I don’t use BuddyBoss so won’t comment further on that code, if its working elsewhere, great.

Have you checked that code is actually running when you expect it to? You mentioned your code works elsewhere, have you confirmed to see if any of the above is actually running?


c.barthe@futurelearning.fr

March 10, 2022 at 2:18 pm

Thanks for the detailed reply Tony. These particular tickets are free, but I can see a use case in the future for paid tickets – so probably a hook that comes after ‘payment’? Is there one that fires for either a free or paid ticket only after the registration has been paid for and the registration status set to Approved?

I’ll fix up the other areas you pointed out – thanks!


Tony

  • Support Staff

March 10, 2022 at 2:39 pm

There is a hook which fires when the registration status is set to Approved:

AHEE__EE_Registration__set_status__to_approved

Which is an action:

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

From there you function would be:

function($registration, $old_STS_ID, $new_STS_ID, $context) {}

You compare the old and new status values if needed to then only do something when for specific status changes, but the above should be enough for most registration types to work.

Try using that hook and output something to a log file to confirm your code is actually running when expected, then move onto trying to set the BuddyBoss groups.


c.barthe@futurelearning.fr

March 10, 2022 at 9:54 pm

Thankyou. It’s working, but the issue was actually with the Buddyboss part of the code. I can get the Post ID for the group, but this is not the Group ID, so I need to figure out a way to get the Group ID from the Post ID. Thanks for your help – on to the next problem through the Buddypress forums πŸ™‚


Tony

  • Support Staff

March 11, 2022 at 2:50 am

Ah, great πŸ™‚

I’m glad you found the problem, if I can help with anything else just let me know.

Viewing 5 reply threads

The support post ‘Hook / Code to add user to Buddyboss group on event 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