简体   繁体   中英

WooCommerce / Wordpress - automatically use the entire email as username?

I'm finishing out a new WooCommerce build here and I figured I'd set it up to have customers use only their email addresses as login credentials with the hope that I could retire and then hide all references to the Wordpress usernames records. This is proving difficult!

My theme allows customers to sign-up at checkout only - so they tick "Create Account" near the end of the form and are either prompted for both an Account username (need to change this heading) and Password... or if WooCommerce is set to auto-generate the username, they're asked for a password only. I have disabled generate usernames from emails in settings as I wan't to have the full email address written into the username field in the database, not the truncated version WooCommerce uses

I see two approaches but would like to here suggestions on which might work.

One approach I thought of would be at checkout, to skim the email address entered further up the page and copy it into the Account Username field when the user ticks "Create Account". I imagine this would need some sort of javascript hack to work. I have manually typed full email addresses into the username field and it's accepted and written into the database username field correctly.

The second approach I can think of is to hide the "Account Username" field at checkout (or set WooCommerce to generate the usernames again which hides the field anyways)... and then to write the full email address into the username field in the database as the account is created. I don't know where something like this could be executed though (I imagine it's more than writing some code into functions.php) but I think an approach like this would be more reliable than the javascript hack I mentioned above.

I have tried going the long way around by replacing all textual references to "username" with "email address" across various login panes, error messages and sign-up and password reset emails but it's a poor approach. WooCommerce will actually tweak the references anyways if full email addresses are in use instead of usernames so I'm only creating unnecessary work. Getting email adddresses into use though is where I'm stuck.

Any ideas or suggestions folks about how I might go about this? I've read many qeustions and answers from others but most of those seem content with replacing a text reference here or there. I want to actually write full email addresses into the database username field... there-by nullifying usernames

Thx

To answer my own question... I went with using a Javascript hack on the checkout form to copy the contents of the email address field into the account "username" field when the customer ticks Create Account

It's by no means foolproof but it does what I need on both desktop and mobile. A non-initiated customer could still clear the auto-filled username field and input something other than their email address so I need to see can I disable the field somehow... grey it out

I went ahead also and replaced all references to Username and Email address or username with just Email address . This was harder to do than the Javascript hack!

The script for anyone interested:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        function onchange() {
            var box1 = $('#billing_email');
            var box2 = $('#account_username');
            box2.val(box1.val());
        }
        $('#billing_email').on('change', onchange);
});</script>

[woocommerce_checkout]

I inserted this directly into the Checkout page using the standard Wordpress editor. I'm unsure where I found the code but it may have been from this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM