Let's Simplify Your Checkout
There are quite a few layers of validation to the Woocommerce checkout.
The scenario I found myself in, is that I let users create an account on checkout to promote loyalty.
The problem is, if they come back to make a second purchase with that same email address they will not be able to proceed.
They will be faced by the following irritating error.
Fortunately, it’s pretty easy to fix.
The issue for me was that if a user leaves the checkbox ticked for “create account” which I had left by default and they try to check out using an email that exists, Woocommerce will try to create an account and then of course throw this error because the email exists.
Add This To Functions.php
/**
*
* Let the user checkout as guest even if they have an account
* @todo tie the order to an email
*
*/
add_filter( 'woocommerce_checkout_posted_data', 'ftm_filter_checkout_posted_data', 10, 1 );
function ftm_filter_checkout_posted_data( $data ) {
$email = $data['billing_email'];
if ( email_exists( $email ) ) $data['createaccount'] = 0;
return $data;
}
What Does This Do?
Before a checkout is submitted and begins processing, all of the data is passed through this filter.
This let’s you modify any of the checkout fields before they go through the usual WC flow.
Here, we are grabbing the submitted email address and checking if the user already has an account.
If they do, we are making sure they are not trying to create an account which is where WC will crack the dummy.
What Else?
I’ve also created a small bit of script that checks their email address as they type it in.
If the typed email address matches an account, a small field will appear below the email address input asking them if they want to log in.
Haven’t included it here, that’s something for another day.
Let me know in the comments how you go or if you have any other ideas for cool WC developments.
Cheers, Chris
A site that was working perfectly now shows “An account is already registered with your email address. Please log in.” for new emails that have not been used before. The snippet for functions.php has no effect. Any ideas what could be the issue?
Hmmmmmm could be many things If the snippet is not working it may be overridden elsewhere, which you can adjust by changing the priority which is the number”10″ in the snippet. Higher number higher priority, try changing 10 to 1000. I would be surprised if that worked though. If it’s definitely new email addresses causing the issue when placing an order then that’s a proper bug – there is no way that would be accidentally happening. This process only occurs (and again, we’re talking about people placing orders) when you’ve got your WC settings configured to create an account at… Read more »
This is just what i am after. We have an issue with people not logging in and then trying to complete the cart and of course they get the small error message about email already exists, but they don’t see it.
Where are you adding this code? in Functions.php?Thanks
Alastair
Hi Alastair, yes correct you can add it to functions.php
If you run into any dramas let me know 🙂
Thanks, Chris
Hi, could you please share the code snippet for “If the typed email address matches an account, a small field will appear below the email address input asking them if they want to log in.”? 🙂 Thanks.
I have actually done exactly that in my own website, I will write it out here Php as follows: /** * * Validate email input via ajax * */ add_action(‘wp_ajax_validate_email’, ‘validate_email_input’); add_action(‘wp_ajax_nopriv_validate_email’, ‘validate_email_input’); function validate_email_input() { // Don’t do anything if they are logged in if ( is_user_logged_in() ) : echo ”; endif; global $wpdb; $email = $_POST[‘billing_email’]; if ( email_exists( $email ) ) { echo ‘ Looks like you\’ve already got an account. Click here to login ‘; } else { echo ‘false’; } exit; } Javascript as follows: jQuery(document).ready(function() { $(‘input[name=billing_email]’).change(function() { var input_value = $(this).val(); $.post( validateEmail.ajaxurl,… Read more »