Join

Add a Custom Text Field to WooCommerce Registration Form

Don't want to mess with code snippets? Request for this to be a feature of MyListing Pro.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

This code snippet allows you to add a custom text field on the WooCommerce registration form. 

For this specific example, the user is asked to enter a special offer code. If the field is empty, a custom error is shown. If validated properly, the text field is saved to the database as user meta and shown in the user profile editor in the WordPress Dashboard.

// Create the custom text field
add_action( 'woocommerce_register_form', 'wptu_register_text_form_field' );
function wptu_register_text_form_field() {
	woocommerce_form_field(
		'special_offer_code',
		array(
			'type'        => 'text',
			'required'    => true, // If true show an asterisk (*)
			'label'       => 'Special Offer Code',
			'description' => 'Enter your offer code',
		),
		( isset($_POST['special_offer_code']) ? $_POST['special_offer_code'] : '' )
	);
}

// Show an error if the special offer code text field is not filled out when registering
add_action( 'woocommerce_register_post', 'wptu_validate_special_offer_code', 10, 3 );
function wptu_validate_special_offer_code( $username, $email, $errors ) {
	if ( empty( $_POST['special_offer_code'] ) ) {
		$errors->add( 'special_offer_code_error', 'We can\'t send your bonus without your special offer code' );
	}
}

// Save the special offer code text field as User Meta
add_action( 'woocommerce_created_customer', 'wptu_save_special_offer_code_field' );
function wptu_save_special_offer_code_field( $customer_id ){
	if ( isset( $_POST['special_offer_code'] ) ) {
		update_user_meta( $customer_id, 'special_offer_code', wc_clean( $_POST['special_offer_code'] ) );
	}
}

// Show special offer code text field in the User Editor
add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');

function custom_user_profile_fields( $user ) { ?>
<h2>Custom user meta fields</h2>
    <table class="form-table">
        <tr>
            <th>
                <label for="special_offer_code"><?php _e( 'Special Offer Code' ); ?></label>
            </th>
            <td>
                <input type="text" name="special_offer_code" id="special_offer_code" value="<?php echo esc_attr( get_the_author_meta( 'special_offer_code', $user->ID ) ); ?>" class="regular-text" />
            </td>
        </tr>
    </table>
<?php
}

// Save new special offer code text field if edited by admin
add_action( 'personal_options_update', 'update_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
function update_extra_profile_fields( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) )
        update_user_meta( $user_id, 'special_offer_code', $_POST['special_offer_code'] );
}

Video Tutorial