I’m creating a new booking form (ie – a registration form) for an upcoming event and need to collect information from each ticket purchased. I found information on creating Attendee Questions (https://wp-event-organiser.com/blog/tutorial/attendee-questions/) and one question requires clicking on a checkbox to accept a consent form. The checkbox is set to required but I can click the Book button to proceed without checking the box.
As an additional experiment I went back to the Booking form and added a required checkbox and that works correctly so there’s something about programmatically adding it as an Attendee Question that is a bug (or something I’m not following correctly).
Here is my code
add_action( ‘eventorganiser_get_event_booking_form’, ‘my_attach_attendee_questions’, 5 );
function my_attach_attendee_questions( $form ){
global $wp_query;
mylog($wp_query->post->post_name);
if($wp_query->post->post_name == 'signup-test') {
mylog("found signup-test");
//Define the field we want to add
$attendee_fields = array(
array(
'id' => '_competitor-consent',
'label' => 'test label',
'options' => array( 'A' => 'I agree to the consent agreement.'),
'type' => 'checkbox',
'required' => true,
),
);
//The attendee-questions element acts as a 'holder' for the elements
//we want repeated for each ticket
$attendee_questions = $form->get_element( 'attendee-questions' );
//If it doesn't exist, we'll create it
if ( ! $attendee_questions ) {
$ticketpicker = $form->get_element( 'ticketpicker' );
$position = intval( $ticketpicker->get( 'position' ) ) + 1;
$parent = $ticketpicker->get_parent();
$parent = ( $parent ? $parent->id : false );
//Create the attendee questions els
$attendee_questions = EO_Booking_Form_Element_Factory::create(array(
'id' => 'attendee-questions',
'type' => 'attendee-questions',
'elements' => $attendee_fields,
'ticket_label' => "Ticket: {{{ticketname}}}",
));
//Add the attendee questions right after the ticket picker
$form->add_element( $attendee_questions, array( 'at' => $position, 'parent' => $parent ) );
} else {
//Attendee questions field already exists, optional: over-ride with our elements
$attendee_questions->set( 'elements', $attendee_fields );
$attendee_questions->_modelElements->set( $attendee_questions->get('elements'), array( 'form' => $form ) );
}
}
}
Robert Stankey
Hi Robert,
I’ve tested this using the code from https://wp-event-organiser.com/blog/tutorial/attendee-questions/ by adding a require checkbox and it works as intended.
Looking at your code, I can see you have a $wp_query->post->post_name
condition. If that condition fails, then the fields are not added to the form. The assumption here is that $wp_query
is populated with the detail of the current event, but that is not necessarily the case (in fact, probably isn’t) when processing the booking which is done via REST API call. It will generally be true when rendering the form which is probably why you are seeing the fields ok.
Given your other post about missing data, I think what’s happening here is that when the booking is processed you’re conditional is false so the attendee fields are not added. This means the attendee data fields are neither validated (this issue) nor saved (this issue).
I appreciate that in the other issue you say you’ve used the code in the original blog post so I could be wrong.
However, if you want to target a particular event you can reliably get that event ID as the second argument to the eventorganiser_get_event_booking_form
callback:
function my_attach_attendee_questions( $form, $event_id ){
//...
}
add_action( 'eventorganiser_get_event_booking_form', 'my_attach_attendee_questions', 5, 2 );
Notice the additional 2
as the fourth argument to add_action
, and the additional $even_id
as a second argument to the callback function.
Stephen Harris
Thanks for solving my issue! I’m very much a newbie wordpress coder and didn’t realize the wp_query could fail in the manner you describe. I added the event_id as you suggested and I’m now seeing the results.
Regards,
Robert
Robert Stankey