Hi Andrew,
If you append ?eventorganiser[booking][occurrence_id]={occurrence-id}
to the end of the url it should automatically select that occurrence indicated.
So, if you edit the page templates to modify the event url (returned by get_permalink()
) then you can get the form to recognise which occurrence to select.
That’s a bit ugly though, so we can improve things with an endpoint:
add_action( 'init', 'my_register_endpoint', 15 );
function my_register_endpoint(){
add_rewrite_endpoint( 'occurrence', EP_PERMALINK );
}
You’ll need to flush the rewrite rules after this (just re-save your permalink settings). This allows use to use the url:
mysite.com/events/event/my-event/occurrence/{occurrence-id}
Next, we just need to tell the form:
add_action( 'eventorganiser_get_event_booking_form', 'my_set_booking_form_occurrence_id', 10, 2 );
function my_set_booking_form_occurrence_id( $form, $event_id ){
global $wp_query;
//Check if an occurrence is given
if( $wp_query->get( 'occurrence' ) ){
$occurrence_id = (int) $wp_query->get( 'occurrence' );
//Next, check that the booking form hasn't already been given an occurrence ID.
if( !$form->get_element( 'occurrence_id' )->get_value() ){
//If not, set the occurrence ID
$form->get_element( 'occurrence_id' )->set_value( $occurrence_id );
}
}
}
And then as before, just update the page templates to append /occurrence/{occurrence-id}
So rather than
get_permalink();
You might have
get_permalink() . '/occurrence/' . $post->occurrence_id;
There is a filter for the event calendar to change the link there too.
The reason that this can only be done by editing the template is that the filters inside get_permalink()
only pass the post ID, not the post object (which contains the occurrence ID). Therefore those filters are occurrence agnostic. I’d recommend creating the following function to replace get_permalink()
for event links:
function get_event_link(){
global $post;
$link = get_permalink();
if( 'event' == get_post_type() && $post->occurrence_id ){
$link = trailingslashit( $link ) . 'occurrence/' . intval( $post->occurrence_id );
}
return $link;
}