fork download
  1. /**
  2.  * Additional fields for registration form
  3.  */
  4. function custom_form_registration_fields() {
  5. $billing_gender = ! empty( $_POST['billing_gender'] ) ? $_POST['billing_gender'] : '';
  6. woocommerce_form_field(
  7. 'billing_gender',
  8. 'label' => __( 'Gender', 'kadence-child' ),
  9. 'type' => 'select',
  10. 'options' => array(
  11. '' => __( 'Select gender', 'kadence-child' ),
  12. 'male' => __( 'Male', 'kadence-child' ),
  13. 'female' => __( 'Female', 'kadence-child' )
  14. ),
  15. 'required' => true,
  16. ),
  17. $billing_gender
  18. );
  19.  
  20. $billing_title = ! empty( $_POST['billing_title'] ) ? $_POST['billing_title'] : '';
  21. woocommerce_form_field(
  22. 'billing_title',
  23. 'label' => __( 'Title', 'kadence-child' ),
  24. 'type' => 'text',
  25. 'placeholder' => __( 'Title', 'kadence-child' ),
  26. 'required' => false,
  27. ),
  28. $billing_title
  29. );
  30.  
  31. $billing_first_name = ! empty( $_POST['billing_first_name'] ) ? $_POST['billing_first_name'] : '';
  32. woocommerce_form_field(
  33. 'billing_first_name',
  34. 'label' => __( 'First name', 'kadence-child' ),
  35. 'type' => 'text',
  36. 'placeholder' => __( 'First name', 'kadence-child' ),
  37. 'class' => array( 'form-row-first' ),
  38. 'clear' => true,
  39. 'required' => true,
  40. ),
  41. $billing_first_name
  42. );
  43.  
  44. $billing_last_name = ! empty( $_POST['billing_last_name'] ) ? $_POST['billing_last_name'] : '';
  45. woocommerce_form_field(
  46. 'billing_last_name',
  47. 'label' => __( 'Last name', 'kadence-child' ),
  48. 'type' => 'text',
  49. 'placeholder' => __( 'Last name', 'kadence-child' ),
  50. 'class' => array( 'form-row-last' ),
  51. 'clear' => true,
  52. 'required' => true,
  53. ),
  54. $billing_last_name
  55. );
  56. }
  57.  
  58. add_action( 'woocommerce_register_form_start', 'custom_form_registration_fields', 25 );
  59.  
  60.  
  61. function custom_validate_registration( $errors ) {
  62. if ( empty( $_POST['billing_gender'] ) ) {
  63. $errors->add( 'billing_gender_error', __( 'Choose a gender.', 'kadence-child' ) );
  64. }
  65.  
  66. if ( empty( $_POST['billing_first_name'] ) ) {
  67. $errors->add( 'billing_first_name_error', __( 'Enter a first name, this is a required field.', 'kadence-child' ) );
  68. }
  69.  
  70. if ( empty( $_POST['billing_last_name'] ) ) {
  71. $errors->add( 'billing_last_name_error', __( 'Enter last name, this is a required field.', 'kadence-child' ) );
  72. }
  73.  
  74. return $errors;
  75. }
  76.  
  77. add_filter( 'woocommerce_registration_errors', 'custom_validate_registration', 25 );
  78.  
  79.  
  80. function custom_save_fields( $user_id ) {
  81. if ( isset( $_POST['billing_gender'] ) && ! empty( $_POST['billing_gender'] ) ) {
  82. update_user_meta( $user_id, 'billing_gender', sanitize_text_field( $_POST['billing_gender'] ) );
  83. }
  84.  
  85. if ( isset( $_POST['billing_title'] ) ) {
  86. update_user_meta( $user_id, 'billing_title', sanitize_text_field( $_POST['billing_title'] ) );
  87. }
  88.  
  89. if ( isset( $_POST['billing_first_name'] ) ) {
  90. update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
  91. update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
  92. }
  93.  
  94. if ( isset( $_POST['billing_last_name'] ) ) {
  95. update_user_meta( $user_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
  96. update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
  97. }
  98.  
  99. }
  100.  
  101. add_action( 'woocommerce_created_customer', 'custom_save_fields', 25 );
Success #stdin #stdout 0.02s 26000KB
stdin
Standard input is empty
stdout
/**
 * Additional fields for registration form
 */
function custom_form_registration_fields() {
	$billing_gender = ! empty( $_POST['billing_gender'] ) ? $_POST['billing_gender'] : '';
	woocommerce_form_field( 
		'billing_gender', 
		array(
			'label' => __( 'Gender', 'kadence-child' ),
			'type' => 'select',
			'options' => array(
				'' => __( 'Select gender', 'kadence-child' ),
				'male' => __( 'Male', 'kadence-child' ),
				'female' => __( 'Female', 'kadence-child' )
			),
			'required' => true,
		),
		$billing_gender
	);

	$billing_title = ! empty( $_POST['billing_title'] ) ? $_POST['billing_title'] : '';
	woocommerce_form_field( 
		'billing_title', 
		array(
			'label' => __( 'Title', 'kadence-child' ),
			'type' => 'text',
			'placeholder' => __( 'Title', 'kadence-child' ),
			'required' => false,
		),
		$billing_title
	);

	$billing_first_name = ! empty( $_POST['billing_first_name'] ) ? $_POST['billing_first_name'] : '';
	woocommerce_form_field( 
		'billing_first_name', 
		array(
			'label' => __( 'First name', 'kadence-child' ),
			'type' => 'text',
			'placeholder' => __( 'First name', 'kadence-child' ),
			'class' => array( 'form-row-first' ),
			'clear' => true,
			'required' => true,
		),
		$billing_first_name
	);

	$billing_last_name = ! empty( $_POST['billing_last_name'] ) ? $_POST['billing_last_name'] : '';
	woocommerce_form_field( 
		'billing_last_name', 
		array(
			'label' => __( 'Last name', 'kadence-child' ),
			'type' => 'text',
			'placeholder' => __( 'Last name', 'kadence-child' ),
			'class' => array( 'form-row-last' ),
			'clear' => true,
			'required' => true,
		),
		$billing_last_name
	);
}

add_action( 'woocommerce_register_form_start', 'custom_form_registration_fields', 25 );


function custom_validate_registration( $errors ) {
	if ( empty( $_POST['billing_gender'] ) ) {
		$errors->add( 'billing_gender_error', __( 'Choose a gender.', 'kadence-child' ) );
	}

	if ( empty( $_POST['billing_first_name'] ) ) {
		$errors->add( 'billing_first_name_error', __( 'Enter a first name, this is a required field.', 'kadence-child' ) );
	}

	if ( empty( $_POST['billing_last_name'] ) ) {
		$errors->add( 'billing_last_name_error', __( 'Enter last name, this is a required field.', 'kadence-child' ) );
	}
 
	return $errors;
}

add_filter( 'woocommerce_registration_errors', 'custom_validate_registration', 25 );


function custom_save_fields( $user_id ) {
	if ( isset( $_POST['billing_gender'] ) && ! empty( $_POST['billing_gender'] ) ) {
		update_user_meta( $user_id, 'billing_gender', sanitize_text_field( $_POST['billing_gender'] ) );
	}

	if ( isset( $_POST['billing_title'] ) ) {
		update_user_meta( $user_id, 'billing_title', sanitize_text_field( $_POST['billing_title'] ) );
	}

	if ( isset( $_POST['billing_first_name'] ) ) {
		update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
		update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
	}

	if ( isset( $_POST['billing_last_name'] ) ) {
		update_user_meta( $user_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
		update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
	}
 
}

add_action( 'woocommerce_created_customer', 'custom_save_fields', 25 );