Pre-populate Fields in Booking confirm form

Hi, we added name, telephone and email as attributes to the bookings and we want them to be auto populated in the confirm page with the user´s information so the user doesnt have to type them again.
We do this so it is easier to identify which person has which booking, now you need to do many click the see the user´s name telephone and email.

I tried:

add_filter(
	'hivepress/v1/forms/booking_update',
	function ( $form ) {
			$form['fields']['nombre']['value'] = 'asdass';


		return $form;
	},
	1000
);

but it doesnt work…

if I use a placeholder it works but of course the data wont be saved:


add_filter(
	'hivepress/v1/forms/booking_update',
	function ( $form ) {
		$user = get_userdata( get_current_user_id() );
		$nombre_hp = $user->first_name;
		$apellido_hp = $user->last_name;
		$email_hp = $user->user_email;
		$telef_hp = substr($user->hp_telefono, 3);
		
			$form['fields']['nombre']['placeholder'] = $nombre_hp . ' ' . $apellido_hp;
			$form['fields']['emailb']['placeholder'] = $email_hp;
			$form['fields']['telef']['placeholder'] = $telef_hp;


		return $form;
	},
	1000
);

thank you in advance

Hi, try something like this:

add_filter(
	'hivepress/v1/forms/booking_update',
	function ( $form ) {
		$user = get_userdata( get_current_user_id() );
		if ( $user ) {
			$nombre_hp = $user->first_name;
			$apellido_hp = $user->last_name;
			$email_hp = $user->user_email;
			$telef_hp = substr($user->hp_telefono, 3);

			if ( isset( $form['fields']['nombre'] ) ) {
				$form['fields']['nombre']['value'] = $nombre_hp . ' ' . $apellido_hp;
			}

			if ( isset( $form['fields']['emailb'] ) ) {
				$form['fields']['emailb']['value'] = $email_hp;
			}

			if ( isset( $form['fields']['telef'] ) ) {
				$form['fields']['telef']['value'] = $telef_hp;
			}
		}

		return $form;
	},
	1000
);

Thank you for your reply, and you can see in the first code on my post ‘value’ didnt work
You are adding an “if isset” but I dont think that will make any difference

What do you mean? I just want to pre-populate the form

Thank you for reply,
Your code (which is the same as the code I posted in my first post) did not work. Like I told you before: ‘value’ doesnt work.

Please try this PHP code snippet. In this way, it is possible to display a user’s first name in the booking confirm form. It is possible to display other user data in this way.

add_filter(
	'hivepress/v1/forms/booking_confirm',
	function($form_args, $form){
		$booking = $form->get_model();
		
		if(!$booking){
			return $form_args;
		}
		
		$user = $booking->get_user();
		
		if(!$user){
			return $form_args;
		}
		
		$form_args['fields']['nombre']['value'] = $user->get_first_name();
		$form_args['fields']['nombre']['_separate'] = true;
		
		return $form_args;
	},
	1000,
	2
);

it worked! thank you very much!
One question I used my code instead of your code to get the values:

add_filter(
	'hivepress/v1/forms/booking_confirm',
	function ($form_args) {
		$user = get_userdata( get_current_user_id() );
		$nombre_hp = $user->first_name;
		$apellido_hp = $user->last_name;
		$email_hp = $user->user_email;
		$telef_hp = substr($user->hp_telefono, 3);
		
		$form_args['fields']['nombre']['value'] = $nombre_hp . ' ' . $apellido_hp;
		$form_args['fields']['nombre']['_separate'] = true;
		$form_args['fields']['emailb']['value'] = $email_hp;
		$form_args['fields']['emailb']['_separate'] = true;
		$form_args['fields']['telef']['value'] = $telef_hp;;
		$form_args['fields']['telef']['_separate'] = true;
		
		return $form_args;
	},
	1000,
	2
);

is there any difference using my code instead of yours? like in performance for example?

Thank you very much again!

Regards

There is only the difference in the ease of use. If you use our solution, it is possible, for example, to get a user’s name depending on the option you have chosen in the HivePress/Settings/Users/Display name setting in this way.

$user->get_display_name();

instead of your variant

$nombre_hp . ' ' . $apellido_hp;

1 Like

Oh, I understand, thsnk ypu very much for your help, it works perfect!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.