Hi Stephen
I have the following code in my event-meta-event-single.php template
$tickets = eo_get_event_tickets_on_sale( $post->ID, $post->occurrence_id );
$ticket = array_shift( $tickets );
$ticket_name = $ticket['name'];
$raw_price = $ticket['price'];
$formatted_price = eo_format_price( $raw_price, true ); //Format price with currency
I just need to loop through all the possible ticket names for the event and display them in an unordered list.
I’ve tried a foreach loop as follows:
foreach($tickets as $ticket) {
echo '' . $ticket_name . '';
}
but it only produces one ticket name instead of the list I need.
Can you help please

Paul Oaten
Hi Paul,
The forum ate your code :/, but does this not work?
$tickets = eo_get_event_tickets_on_sale( $post->ID, $post->occurrence_id );
echo '<ul>';
foreach( $tickets as $ticket ){
printf( '<li> %s </li>', esc_html( $ticket['name'] ) );
}
echo '</ul>';

Stephen Harris
It almost does.
Here’s what I have now:-
<?php
foreach( $tickets as $ticket ){
printf( '- %s
', esc_html( $ticket['name'] . ' - ' . $formatted_price ) );
}
?>
However, the first ticket name doesn’t get printed in the list and also the $formatted_price for each ticket name appears as zero.
Is this something to do with array_shift($tickets); ?
Can I override somehow. Continued help appreciated as ever!

Paul Oaten
array_shift()
removes the element from the beginning of the array and returns it.
So you can try,
$tickets = eo_get_event_tickets_on_sale( $post->ID, $post->occurrence_id );
echo '<ul>';
foreach( $tickets as $ticket ){
$raw_price = $ticket['price'];
$formatted_price = eo_format_price( $raw_price, true ); //Format price with currency
printf( '<li> %s </li>', esc_html( $ticket['name'] . ' - ' . $formatted_price ) );
}
echo '</ul>';

Stephen Harris
So I amended the array line to this:
$ticket = array( $tickets );
and now it works fine. Does it matter that I took out the ‘shift’ instruction?

Paul Oaten
The array_shift()
was from another thread, and I used it because I thought there was only one ticket available (or you just wanted the price of the first ticket).
The thing to be aware of is that array_shift()
modifies the original array.
$array = array( 'a', 'b', 'c' );
echo array_shift( $array ); //'a';
//$array is now array( 'b', 'c' );
My previous post should list all tickets (and price) for an event.

Stephen Harris