• I’m trying to add 2 custom fields to the Woo My Accounts registration area. The fields need to be *required and added to the user data

    I found the sample code below (added to functions.php), which I modified from a post by karbanovich about a year ago. This was my test to see if I could get the first custom field to work. The code adds the custom field to the registration area, but it (1) doesn’t trap the error if the custom field is blank and (2) doesn’t appear to add the custom field to the user data.

    Any help would be appreciated.

    //Adding Registration fields to the form

    add_filter( ‘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;
    echo ‘<div class=”form-row form-row-wide”><label for=”reg_stid”>’.__(‘Sales Tax ID’,

    ‘woocommerce’).’ <span class=”required”>*</span></label>
    <input type=”text” class=”input-text” name=”stid” id=”reg_stid” size=”30″ value=”‘.esc_attr

    ($_POST[‘stid’]).'” /></div>’;

    }

    //Validation registration form after submission using the filter registration_errors
    add_filter(‘registration_errors’, ‘registration_errors_validation’, 10,3);
    function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
    global $woocommerce;
    extract($_POST); // extracting $_POST into separate variables
    if($stid == ” ) {
    $woocommerce->add_error( __( ‘Please, fill in all 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, ‘stid’, $stid);

    // can also do multiple fields like that
    update_user_meta($user_id, ‘stid’, $stid);

    }

    https://www.ads-software.com/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Woocommerce registration custom fields’ is closed to new replies.