Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to pull the value for a custom question from user meta and set that as the default value used on the checkout registration form. #EE #checkout
<?php //Please do not include the opening PHP tag if you already have one
function tw_ee_custom_question_answers_from_user_meta($input_args, EE_Registration $registration = null, EE_Question $question = null, EE_Answer $answer = null) {
// Do nothing if we are in the admin.
if(is_admin()){
return $input_args;
}
// Get the current user
$current_user = wp_get_current_user();
// If we have an EE_Question object
// and it is NOT a system question
// and we have an EE_Registration object
// and that registration object is for the primary registrant
// and $current_user is an instance of WP_User
if( $question instanceof EE_Question &&
! $question->system_ID() &&
$registration instanceof EE_Registration &&
$registration->is_primary_registrant() &&
$current_user instanceof WP_User )
{
// Use a switch here so that you can set multiple different questions ID's to vaiour values.
switch( $question->ID() ) {
// Set your custom question ID here
case 93:
// Pull your custom value from user meta, be sure to change 'user_meta_key' to the meta key you used.
$emergency_contact = get_user_meta($current_user->ID, 'user_meta_key', true);
// Check we have a value.
if( $emergency_contact ) {
// Set the default value of the question to your user_meta value.
$input_args[ 'default' ] = $emergency_contact;
}
break;
}
}
// Return the $input_args
return $input_args;
}
add_filter('FHEE__EE_SPCO_Reg_Step_Attendee_Information___generate_question_input__input_constructor_args', 'tw_ee_custom_question_answers_from_user_meta', 10, 4);
@bryansayler

This comment has been minimized.

Copy link

@bryansayler bryansayler commented Jun 28, 2019

This also overwrites these fields when editing a registration as an admin in the backend. The submitted registration information is replaced by the logged-in Admin's information. How can I limit this to only the frontend registration checkout form?

@Pebblo

This comment has been minimized.

Copy link
Owner Author

@Pebblo Pebblo commented Jul 1, 2019

@bryansayler

Nice catch, I've updated the snippet with an is_admin() check so if the above runs in the admin it just returns $input_args as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.