• Resolved Rose

    (@thorned-rose)


    I need WooCommerce to only use email address and password for customer registration and login (no username). I have the following code snippet to set customers’ email addresses as usernames:

    add_filter( 'woocommerce_new_customer_data', function( $data ) {
    $data['user_login'] = $data['user_email'];

    return $data;
    } );

    However, the issue then is that on the my-account page, the login form shows “Username or email address” and the error text shows as “ERROR: The username or password you entered is incorrect. Lost your password?” See here:

    How can I alter the login form so it instead reads “Email address” and the error as “ERROR: The email address or password you entered is incorrect. Lost your password?” so it would look like this:

    Thanks! ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @thorned-rose,

    Thanks for reaching out.

    Helping out with custom coding of this nature is outside the scope of support that our support staff can help out with here, although I would recommend the following:

    1. Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;
    2. Checking whether there are existing plugins in the WordPress plugin repository that might be doing that already.
    3. Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions): https://woo.com/community-slack/

    Hope it helps!

    Thread Starter Rose

    (@thorned-rose)

    1. There is no way I’m relying on Chat-GPT to output correct code, especially on a website that handles people’s private data.
    2. I already did that, I searched everywhere and couldn’t find any hints or solutions, hence why I asked here
    3. I’d rather not create yet another account to ask what I would have thought was a simple question about WooCommerce’s built-in hooks/filters. ??

    WooCommerce has hooks for this reason, why can support staff not answer questions on WC’s own code?

    Thread Starter Rose

    (@thorned-rose)

    In case anyone else comes across this issue. Here’s what I’ve muddled together. It’s likely very unoptimised and may be the totally wrong way to go about this but since I have Cognitive Impairment I have a hard time making sense of things and without any further help from here, this is the best I could do.

    This goes in functions.php or, what is more recommended to do, use a code snippets plugin.

    // First set the email address as the username for all new customer accounts created
    add_filter( 'woocommerce_new_customer_data', function( $data ) {
    $data['user_login'] = $data['user_email'];

    return $data;
    } );

    // Next remove "Username" from "Username or email address" on the WooCommerce My Account page login so it reads "Email address"
    function filter_woocommerce_my_account_login_text( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
    case 'Username or email address' :
    $translated_text = __( 'Email address', 'woocommerce' );
    break;
    }
    return $translated_text;
    }
    add_filter( 'gettext', 'filter_woocommerce_my_account_login_text', 20, 3 );

    // Finally we want to remove any references to a username from error validation
    // define the woocommerce_process_login_errors callback
    function filter_woocommerce_process_login_errors($validation_error, $post_username, $post_password)
    {
    //if (strpos($post_username, '@') == FALSE)
    if (!filter_var($post_username, FILTER_VALIDATE_EMAIL)) //<--recommend option
    {
    throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Please enter a valid email address.', 'woocommerce' ) );
    }
    return $validation_error;
    }
    add_filter('woocommerce_process_login_errors', 'filter_woocommerce_process_login_errors', 10, 3);

    References:

    https://www.checkoutwc.com/documentation/how-to-force-woocommerce-to-use-email-address-as-username-when-registering-a-new-account/

    https://gist.github.com/BFTrick/7040403

    https://stackoverflow.com/questions/43630347/how-to-login-with-only-email-and-password-in-woocommerce

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @thorned-rose

    WooCommerce has hooks for this reason, why can support staff not answer questions on WC’s own code?

    I understand your concerns about customizing the login form and error message to suit your needs.

    While our support staff may not provide custom code solutions, I can direct you to the right path. The login form and error messages can be altered via translation files or by using a plugin like Loco Translate. Using this plugin, you can adjust the wording to your preference.

    However, if you want to try using custom code, you can use the WordPress login_errors filter. More info:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @thorned-rose

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, you were able to find a solution to your problem!

    If you have further questions, please feel free to open a new topic.

    Thanks!

    Thread Starter Rose

    (@thorned-rose)

    I’ve been away and only just got back. Unfortunately the answer you gave me is more orientated towards the WordPress logins, not the Woocommerce customer account page.

    This remains unresolved and I’ve given up trying to fix it.

    Hey, @thorned-rose!

    I’m sad to hear you gave up in trying to fix this.

    I understand that it not working it frustrating.

    As my colleagues mentioned, we are not able to provide support for custom code in this forum.
    I understand you don’t want to create another account, but the best place to get help with this would be the slack channel my colleague mentioned. We have a great community there that might be able to help you if you change your mind and decide to continue to look for a solution. If you have a Facebook account, you can also visit the WooCommerce Facebook group.

    I hope this helps ??

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Customer registration/login with email only (no username)’ is closed to new replies.