• Resolved KramBie

    (@krambie)


    On the event grid page I am using the conditional placeholder is_user_attendee to show a text like “Your reservation is confirmed”. That works fine. Now I would also show the name of the ticket(s) this user has ordered. I made a custom placeholder like this:

    add_filter('em_event_output_placeholder','my_em_ticket_string',1,3);
    function my_em_ticket_string($replace, $EM_Event, $result){
    ? ? if( $result == '#_TICKETSTRING' ){
    ? ? ? ? $replace = '';
    ? ? ? ? $EM_Bookings = $EM_Event->get_bookings();
    ? ? ? ? if( count($EM_Bookings->bookings) > 0 ){
    ? ? ? ? ? ? foreach( $EM_Bookings as $EM_Booking){
    ? ? ? ? ? ? ? ? $ticket_name = "";
    ? ? ? ? ? ? ? ? ? ? foreach ( $EM_Booking->get_tickets() as $EM_Ticket ) {
    ? ? ? ? ? ? ? ? ? ? ? ? if ($ticket_name == "") {
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? $ticket_name = $EM_Ticket->name;
    ? ? ? ? ? ? ? ? ? ? ? ? } else {
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? $ticket_name = $ticket_name . ', ' . $EM_Ticket->name;
    ? ? ? ? ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? }
    ? ? ? ? }
    ? ? ? ? $replace = $ticket_name;
    ? ? }
    ? ? return $replace;
    }

    Unfortunate this is working good. Yes is shows ticket name(s), but not always from the logged in user. Instead it shows the ticket name of the first user who ordered a ticket. So my code is somehow missing a check for the logged in user. How can I improve this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • joneiseman

    (@joneiseman)

    I think the following should work:

    add_filter('em_event_output_placeholder','my_em_ticket_string',1,3);
    function my_em_ticket_string($replace, $EM_Event, $result){
    if( $result == '#_TICKETSTRING' ){
    $replace = '';
    $EM_Bookings = $EM_Event->get_bookings();
    if( count($EM_Bookings->bookings) > 0 ){
    foreach( $EM_Bookings as $EM_Booking){
    if ( $EM_Booking->person_id == get_current_user_id() ) {
    $ticket_name = "";
    foreach ( $EM_Booking->get_tickets() as $EM_Ticket ) {
    if ($ticket_name == "") {
    $ticket_name = $EM_Ticket->name;
    } else {
    $ticket_name = $ticket_name . ', ' . $EM_Ticket->name;
    }
    }
    break;
    }

    }
    }
    $replace = $ticket_name;
    }
    return $replace;
    }
    Thread Starter KramBie

    (@krambie)

    Yes it works ??
    Thank you once again.

    joneiseman

    (@joneiseman)

    Here’s a corrected version of the code snippet:

    add_filter('em_event_output_placeholder','my_em_ticket_string',1,3);
    function my_em_ticket_string($replace, $EM_Event, $result){
    if( $result == '#_TICKETSTRING' ){
    $EM_Bookings = $EM_Event->get_bookings();
    if( count($EM_Bookings->bookings) > 0 ){
    foreach( $EM_Bookings as $EM_Booking){
    if ( $EM_Booking->person_id == get_current_user_id() ) {
    foreach ( $EM_Booking->get_tickets() as $EM_Ticket ) {
    return $EM_Ticket->name;
    }
    }
    }
    }
    return "";
    }
    return $replace;
    }

    Thread Starter KramBie

    (@krambie)

    This one works also, but the version of yesterday was already fine. I guess the version of today is slightly faster in single ticket modus. Thanks, once again.

    The problem with the previous version is that it had the following line:

    $replace = $ticket_name;

    The variable $ticket_name may not have been set (in the case when there was no bookings) which would result in a fatal PHP error.

    Thread Starter KramBie

    (@krambie)

    OK that makes sense. I modified the new code a bit because I like to get all ticket names if a person has ordered more than on ticket in one booking:

    add_filter('em_event_output_placeholder', 'my_em_ticket_string', 1, 3);
    function my_em_ticket_string($replace, $EM_Event, $result) {
        if ($result == '#_TICKETSTRING') {
            $replace = '';
            $EM_Bookings = $EM_Event->get_bookings();
            if (count($EM_Bookings->bookings) > 0) {
                foreach ($EM_Bookings as $EM_Booking) {
                    if ($EM_Booking->person_id == get_current_user_id()) {
                        $ticket_name = "";
                        foreach ($EM_Booking->get_tickets() as $EM_Ticket) {
                            if ($ticket_name == "") {
                                $ticket_name = $EM_Ticket->name;
                            } else {
                                $ticket_name = $ticket_name . ', ' . $EM_Ticket->name;
                            }
                        }
                        return $ticket_name;
                    }
                }
            }
            return "";
        }
        return $replace;
    }

    Thank you for pointing out that possible PHP error. I am not a very experienced PHP programmer.
    BTW The limitation if a user has ordered multiple tickets with the same name it only shows that once is no problem. I need to see if the user has ordered different tickets for the same event.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.