Skip to content

Custom checkout fields

Allyson Alves de Souza edited this page Mar 27, 2017 · 1 revision

Registrations for WooCommerce, since version 1.0.7, let the developers to easily extend and add more participant fields to the checkout. If you want, for example, request the birthday, document number, telephone, etc, it's possible using the plugin hooks to add that fields to checkout and display them correctly in order meta.

Put these code on your theme functions.php or plugin.

Adding Nickname checkout field to participants

registrations_display_participant_fields

Create and display the new field in WooCommerce checkout, for each participant. Receives two parameters: $checkout (the current checkout object) and $current_participant (the current participant number to be used in field creation and prevent repeated names). It's recommended to use the woocommerce_form_field function to generate the fields.

function registrations_display_participant_fields( $checkout, $current_participant ) {
	woocommerce_form_field( 'participant_nickname_' . $current_participant , array(
		'type'          => 'text',
		'class'         => array('participant-nickname form-row-wide'),
		'label'         => __( 'Nickname', 'my-theme-textdomain' ),
		'placeholder'   => __( 'Nickname', 'my-theme-textdomain'),
	), $checkout->get_value( 'participant_nickname_' . $current_participant )
	);
}
add_action( 'registrations_display_participant_fields', 'registrations_display_participant_fields', 10, 2 );

registrations_checkout_process_fields

Use the registrations_checkout_process_fields hook to validate your custom fields, asserting they're defined, or use only numbers or letters, or whatever rule you want. The $countparameter refers to the participant number to be validated.

function registrations_proccess_checkout_participant_fields( $count ) {
	// Check if field is set, if it's not set add an error.
	if ( ! $_POST['participant_nickname_' . $count] ) {
		wc_add_notice( sprintf( __( 'Please enter a correct nickname to participant #%u ', 'my-theme-textdomain' ), $count ), 'error' );
	}
}
add_action( 'registrations_checkout_process_fields', 'registrations_proccess_checkout_participant_fields', 10, 1 );

registrations_checkout_fields_order_meta_value

Use the registrations_checkout_fields_order_meta_value filter to get your participant defined field value sanitized, to be stored and saved as order meta. Receives two parameters: $participant (the current participant fields array); $count (the current participant number to be processed).

function registrations_custom_checkout_fields_meta_value( $participant, $count ) {
	if ( ! empty( $_POST['participant_nickname_' . $count ] ) &&  ! empty( $participant ) ) {
		$participant['nickname'] = sanitize_text_field( $_POST['participant_nickname_' . $count] );
	}
	return $participant;
}
add_filter( 'registrations_checkout_fields_order_meta_value', 'registrations_custom_checkout_fields_meta_value', 10, 2 );

registrations_admin_order_meta_participant_fields

At the end, display the new field value on order metas if you want the data to be visible to store manager in order management. Use the registrations_admin_order_meta_participant_fields filter, they have one parameter: $participant (the current participant fields array to be displayed).

function registrations_admin_display_participant_fields( $participant ) {
	echo sprintf( __( 'Nickname: %s' , 'twentyseventeen' ), $participant['nickname'] );
}
add_action( 'registrations_admin_order_meta_participant_fields', 'registrations_admin_display_participant_fields', 10, 1 );