Wordpress Woocommerce remove account username validation

Viewed 735

I want to user 10 digit mobile number in my wordpress woocommerce registration page.

While using mobile number as username, I am getting an error message:

Error: Please enter a valid account username.

I am also adding screenshots for the above: screen short[1] 2nd Image

I am searching since yesterday, with no luck.

1 Answers

As far as woo-commerce there is not default option to login with mobile number.

You have add custom functions in your functions.php file with action and hooks to modify your existing code to be able to log in via mobile number.

Refer to this link for details: https://medium.com/@omarkasem/login-with-phone-number-in-woocommerce-wordpress-f7d6d07964d8

function.php

Add custom field

function wooc_add_phone_number_field() {
    return apply_filters( 'woocommerce_forms_field', array(
        'wooc_user_phone' => array(
            'type'        => 'text',
            'label'       => __( 'Phone Number', ' woocommerce' ),
            'placeholder' => __( 'Your phone number', 'woocommerce' ),
            'required'    => true,
        ),
    ) );
}
add_action( 'woocommerce_register_form', 'wooc_add_field_to_registeration_form', 15 );
function wooc_add_field_to_registeration_form() {
    $fields = wooc_add_phone_number_field();
    foreach ( $fields as $key => $field_args ) {
        woocommerce_form_field( $key, $field_args );
    }
}

Save to DB

add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
function wooc_save_extra_register_fields( $customer_id ) {
    if (isset($_POST['wooc_user_phone'])){
        update_user_meta( $customer_id, 'wooc_user_phone', sanitize_text_field( $_POST['wooc_user_phone'] ) );
    }
}
Related