Is there a way to get a user’s bookings in a specific category of event using the eo_get_user_booking_history function? Or if not, is there another way to do it?
Thanks in advance.
Jon Packman
Hi Jon,
There’s no direct way of doing this,
eo_get_user_booking_history()
is just a wrapper for eo_get_bookings()
, specifically:
eo_get_bookings( array(
'bookee_id' => $user_id
));
That, itself, is a wrapper for a WP_Query
and the (post) ID of the event associated with a booking is stored in _eo_booking_event_id
. So you could try the following:
$user_id = get_current_user_id();
//Get an array of event (post) IDs in a specific category
$event_ids = eo_get_events( array(
'group_events_by' => 'series',
'fields' => 'ids',
'event-category' => 'my-cat-slug'
) );
//Get bookings whose event ID is in that array
eo_get_bookings( array(
'bookee_id' => $user_id,
'meta_query' => array(
array(
'key' => '_eo_booking_event_id',
'value' => $event_ids,
'compare' => 'IN'
)
),
));
Please note that if $event_ids
is an empty array, then all bookings (for that user) would be returned. This is the default behaviour of meta_query
when ‘compare’ is ‘IN’ and the value is an empty array.
Hope that helps,
Stephen Harris
Thanks Stephen, that worked perfectly. I didn’t need get_current_user_id as I’m adding these bookings as extra profile fields, so user_id on its own does the job.
Jon Packman
Final (I hope!) question on the same subject – how would I get the date of the event instead of the date the event was booked (replacing eo_get_booking_date below)?
foreach( $bookings as $booking ){
printf(
'
You booked %1$d tickets for %2$s on %3$s ',
eo_get_booking_meta( $booking->ID, 'ticket_quantity' ),
'
ID, 'event_id' ) ).'">'.get_the_title( eo_get_booking_meta( $booking->ID, 'event_id' ) ).'',
eo_get_booking_date( $booking->ID, 'jS F Y' )
);
}
Jon Packman
You can get the event ID and occurrence ID as follows,
$event_id = eo_get_booking_meta( $booking->ID, 'event_id' );
$occurrence_id = eo_get_booking_meta( $booking->ID, 'occurrence_id' );
and then use eo_get_the_start()
. If you are ‘booking by series’ then the occurrence ID will always be 0.
foreach( $bookings as $booking ){
$event_id = eo_get_booking_meta( $booking->ID, 'event_id' );
$occurrence_id = eo_get_booking_meta( $booking->ID, 'occurrence_id' );
printf(
'You booked %1$d tickets for %2$s on %3$s',
eo_get_booking_meta( $booking->ID, 'ticket_quantity' ),
get_the_title( $event_id ),
eo_get_the_start( 'jS F Y', $event_id, null, $occurrence_id );
);
}
See codex on eo_get_booking_meta()
and eo_get_the_start()
for details / examples
Stephen Harris