Add fields to woocommerce registration form : follow up
-
Hey all,
Had just read this post today after looking for this same thing, and even tho it’s closed for comments, I wanted to share the solution that a colleague helped me come up with (thanks @rarst).
The following code pasted into your theme will add both a First Name and a Last Name field to the registration form, both of which will also be saved to the Billing and Shipping names fields as well. Additionally, there is a notice if the fields aren’t filled in.
This has been tested and is working great for me. Hope it can help some of you. Enjoy ??
//Adding Registration fields to the form add_action( 'register_form', 'adding_custom_registration_fields' ); function adding_custom_registration_fields( ) { //lets make the field required so that i can show you how to validate it later; $firstname = empty( $_POST['firstname'] ) ? '' : $_POST['firstname']; $lastname = empty( $_POST['lastname'] ) ? '' : $_POST['lastname']; ?> <div class="form-row form-row-wide"> <label for="reg_firstname"><?php _e( 'First Name', 'woocommerce' ) ?><span class="required">*</span></label> <input type="text" class="input-text" name="firstname" id="reg_firstname" size="30" value="<?php echo esc_attr( $firstname ) ?>" /> </div> <div class="form-row form-row-wide"> <label for="reg_lastname"><?php _e( 'Last Name', 'woocommerce' ) ?><span class="required">*</span></label> <input type="text" class="input-text" name="lastname" id="reg_lastname" size="30" value="<?php echo esc_attr( $lastname ) ?>" /> </div><?php } //Validation registration form after submission using the filter registration_errors add_filter( 'woocommerce_registration_errors', 'registration_errors_validation' ); /** * @param WP_Error $reg_errors * * @return WP_Error */ function registration_errors_validation( $reg_errors ) { if ( empty( $_POST['firstname'] ) || empty( $_POST['lastname'] ) ) { $reg_errors->add( 'empty required fields', __( 'Please fill in the required fields.', 'woocommerce' ) ); } return $reg_errors; } //Updating use meta after registration successful registration add_action('woocommerce_created_customer','adding_extra_reg_fields'); function adding_extra_reg_fields($user_id) { extract($_POST); update_user_meta($user_id, 'first_name', $firstname); update_user_meta($user_id, 'last_name', $lastname); update_user_meta($user_id, 'billing_first_name', $firstname); update_user_meta($user_id, 'shipping_first_name', $firstname); update_user_meta($user_id, 'billing_last_name', $lastname); update_user_meta($user_id, 'shipping_last_name', $lastname); }
- The topic ‘Add fields to woocommerce registration form : follow up’ is closed to new replies.