• Resolved sander105

    (@sander105)


    In the User Registration form I want to add a custom text field where the user can fill in a role(or any other text)

    I’m using these 2 functions to display the field and then save it to the User. Right now the field displays but the billing_role doesn’t get saved to the User.

    I’m testing this in another function where all the User data gets shown

    <?php
    function wooc_extra_register_fields() {
        $billing_role = ! empty( $_POST['billing_role'] ) ? intval( $_POST['billing_role'] ) : '';
        ?>
           <p class="form-row form-row-wide">
           <label for="billing_role"><?php _e( 'billing_role', 'woocommerce' ); ?></label>
           <input type="text" class="input-text" name="billing_role" id="billing_role" value="<?php echo esc_attr( $billing_role ); ?>" />
           
    
           <div class="clear"></div>
           <?php
     }
     add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
    
    /**
    * save extra field.
    */
    function wooc_save_extra_register_fields($customer_id) {
    	echo $customer_id;
        if ( ! empty( $_POST['billing_role'] ) ) {
            update_user_meta( $customer_id, 'billing_role', sanitize_text_field( $_POST['billing_role'] ) );
        }
    }
    add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

    What is the correct way to do this? Thanks in advance

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Custom field in Customer Registration form’ is closed to new replies.