Support

Home Forums Event Espresso Premium How to hide the Register Now button for those already registered

How to hide the Register Now button for those already registered

Posted: May 14, 2018 at 2:53 pm


mdeyerinkc

May 14, 2018 at 2:53 pm

Hi,

I’m posting a solution to a feature I needed but was not able to find on the forum for the benefit of others who may also need it: removing the ability to register again / create a duplicate registration for an event. NOTE: I believe this solution only works for member-based sites. If you don’t require your registrants to be logged in prior to registering for an event, this solution may not work for you.

On my site we don’t actually sell tickets, but rather use EE to limit the registrations/available seats for internal organizational trainings we conduct. As a result, employees must be logged in prior to registering for a class. But even then I noticed that a user who was already registered for an event could open that event page and register again (i.e. “purchase” another ticket using the “Register Now” button), thus creating a situation where a person could have duplicate registrations. It would not be uncommon for someone to register for a class, forgot they did, and then register again.

My goal was to remove this possibility, so after some searches on the forum, I started with code by Josh Feck that checks the user record that I found here: https://gist.github.com/joshfeck/f73483e9df1f5ccc55f15a43fcecf463, and then added a line of code to the very bottom (which actually hides the section where the Register Now button would normally appear). So the whole thing looks like:

add_action( 
  'AHEE_event_details_before_post',
  'my_add_content_event_if_logged_in_registered', 
  11
);
function my_add_content_event_if_logged_in_registered( $post ) {
  if ( is_user_logged_in() && is_singular() ) {
  
    $user = wp_get_current_user();
    if ( ! $user instanceof WP_User ) {
      return;
    }
    //is there an attached EE_Attendee?
    $att_id = get_user_option( 'EE_Attendee_ID', $user->ID );
    if ( empty( $att_id ) ) {
      return; //bail, no attached attendee_id.
    }
    //grab contact
    $contact = EEM_Attendee::instance()->get_one_by_ID( $att_id );
    //if no contact then bail
    if ( ! $contact instanceof EE_Attendee ) {
      return;
    }
    // get events for this user
    $events = $contact->get_many_related( 'Event' );
    // build an array of event IDs
    foreach ( $events as $event ){
      $user_event_ids[] = $event->get( 'EVT_ID' );
    }
    // get this event's ID
    $this_event_id = $post->ID;
    // look for a match
    if( in_array( $this_event_id, $user_event_ids ) ) {
      echo '<span style="color:red"><h3>You are registered for this event!</h3><p>If you need to cancel or make any changes to your registration, please contact the course administrator.</p></span>';
	}
{
    echo '<style>div.event-tickets{ display: none; }</style>';
}
}
}

Josh might be able to offer a cleaner version of this, but thought I’d share for others who might need to achieve this in the meantime.


mdeyerinkc

May 14, 2018 at 2:55 pm

PS – you would put this in your function.php file (presumably in your child folder to avoid being lost after and update.


mdeyerinkc

May 14, 2018 at 4:15 pm

…well, I guess I’m not as clever as I thought. That last line of code hides the register button whether the person is already registered for the event or not.

Josh – what did I miss?


mdeyerinkc

May 14, 2018 at 4:19 pm

…I think I figured it out. Had an extra-set brackets I didn’t need. so the last part should look like…

if( in_array( $this_event_id, $user_event_ids ) ) {
      echo '<span style="color:red"><h3>You are registered for this event!</h3><p>If you need to cancel or make any changes to your registration, please contact the course administrator.</p></span>';
    echo '<style>div.event-tickets{ display: none; }</style>';
}
}
}


Tony

  • Support Staff

May 15, 2018 at 4:12 am

Hi there,

There’s a couple of changes I would recommend making to that snippet (although it should work just fine as is) as we now have some methods built into EE that will reduce the workload needed for this.

$events = $contact->get_many_related( 'Event' );
// build an array of event IDs
foreach ( $events as $event ){
    $user_event_ids[] = $event->get( 'EVT_ID' );
}

That’s fairly inefficient for what you are trying to do, it pulls all events related to the contact, loops over them all to pull the ID’s and builds an array of ID that is used later to search through.

You only need to know if the user has just one registration on that event, right? So you can do something like this:

$registration = $contact->get_most_recent_registration_for_event($post->ID);

Pull the most recent registration for the current post (which in this case will be an event), if that function returns an EE_Regisitration object then the user has at least one registration on the event, meaning you can do:

if( $registration instanceof EE_Registration ) {
    //Output some text here and remove the ticket selector.
}

Try something like this:

https://gist.github.com/Pebblo/10669adfeea322ade3d96a38857fb46b

Which should also cover cases where you also display the ticket selector on the event list itself and not just the single event page. It removes the action used to add the ticket selector from the post rather than hiding the tickets after they have been added, which is what the additional code at the bottom is for.

As mentioned, Josh’s code will work fine as is with your changes, the above just uses some newer methods to pull the data easier so its up to you which one you prefer to use 🙂


mdeyerinkc

May 30, 2018 at 4:12 pm

Tony – I went ahead and replaced what I had with this version. Thank you. But during testing I discovered that if I cancel the registration, this message doesn’t really account for that. So I’m looking for some kind of tweak that incorporates the actual status of the registration; meaning that if the user’s registration is changed to “canceled,” is there a way to filter for that and have the return text incorporate the registration status? I.e. instead of “You are registered for this event!” maybe “You have already registered for this event, and your current registration status is X. If you need to request any changes to your registration, please contact the course administrator.”


Tony

  • Support Staff

May 31, 2018 at 3:07 am

Sure, you have the EE_registration object so you ca use the pretty_status() method on that to output the current status.

I’ve updated my gist to include it:

https://gist.github.com/Pebblo/10669adfeea322ade3d96a38857fb46b


mdeyerinkc

May 31, 2018 at 10:53 am

Works great – thank you!

The support post ‘How to hide the Register Now button for those already registered’ 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