How to make custom sign up form in Woocommerce
Sometimes you think about if your customers would like to place their birthday, city etc as sign up information. Later you can wish them on their birthday or you can campaign new products in an area based. There are lots of things can be happened. Let’s see how we can add a custom sign up form in Woocommerce.
/** * Add new register fields for WooCommerce registration. * * @return string Register fields HTML. */ function wooc_extra_register_fields() { ?> <p class="form-row form-row-first"> <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label> <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" /> </p> <p class="form-row form-row-last"> <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label> <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" /> </p> <div class="clear"></div> <p class="form-row form-row-wide"> <label for="reg_billing_dob"><?php _e( 'dob', 'woocommerce' ); ?> <span class="required">*</span></label> <input type="text" class="input-text" name="billing_dob" id="reg_billing_dob" value="<?php if ( ! empty( $_POST['billing_dob'] ) ) esc_attr_e( $_POST['billing_dob'] ); ?>" /> </p> <?php } add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' ); /** * Validate the extra register fields. * * @param string $username Current username. * @param string $email Current email. * @param object $validation_errors WP_Error object. * * @return void */ function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) { if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) { $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) ); } if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) { $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) ); } if ( isset( $_POST['billing_dob'] ) && empty( $_POST['billing_dob'] ) ) { $validation_errors->add( 'billing_dob_error', __( '<strong>Error</strong>: Birthday is required!.', 'woocommerce' ) ); } } add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 ); /** * Save the extra register fields. * * @param int $customer_id Current customer ID. * * @return void */ function wooc_save_extra_register_fields( $customer_id ) { if ( isset( $_POST['billing_first_name'] ) ) { // WordPress default first name field. update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); // WooCommerce billing first name. update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); } if ( isset( $_POST['billing_last_name'] ) ) { // WordPress default last name field. update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); // WooCommerce billing last name. update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); } if ( isset( $_POST['billing_dob'] ) ) { // WooCommerce billing dob update_user_meta( $customer_id, 'billing_dob', sanitize_text_field( $_POST['billing_dob'] ) ); } } add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
Just add this code to your functions.php file.
Then Go to Woocommerce settings page to active the sign-up option. Follow the image below.
After logging out(in case you are logged in), go to “My Account page”, then you will see this:
You can also customize this code by adding or removing new fields. You need to add your custom fields in three steps. First, add form fields in HTML, then validate the form data, then save it in woocommerce. Done!