
Community
Administrator
Staff member
So I saw someone on a freelancer website asking for a function that checks to see if the user is already a customer when entering their email address at checkout.
And if the email matched an existing user, it hides the password field, allows checkout and applies the order to the existing user.
I thought this was a cool idea for some stores with alot of repeat custom. So I made the function. This also forces the email address field to be at the top of the form fields, if you don't wish to do this remove the first function.
You could also get it to populate the users data at checkout too, the function to do this is below. Although i would strongly advise against it as it does pose as security/data issue if someone was randomly trying email addresses to expose customer data.
Enjoy
And if the email matched an existing user, it hides the password field, allows checkout and applies the order to the existing user.
I thought this was a cool idea for some stores with alot of repeat custom. So I made the function. This also forces the email address field to be at the top of the form fields, if you don't wish to do this remove the first function.
PHP:
/**
* Reorder checkout fields to ensure email is at the top.
*/
add_filter('woocommerce_checkout_fields', 'force_email_field_to_top');
function force_email_field_to_top($fields){if (isset($fields['billing']['billing_email'])){$email_field = $fields['billing']['billing_email'];
unset($fields['billing']['billing_email']);
$fields['billing'] = array('billing_email' => $email_field) + $fields['billing'];
}
$fields['billing']['billing_email']['priority'] = 1;
return $fields;
}
/**
* Remove "email exists" error during checkout validation for existing users.
*/
add_action('woocommerce_after_checkout_validation', 'handle_existing_email_error', 10, 2);
function handle_existing_email_error($data, $errors){if (!empty($data['billing_email']) && email_exists($data['billing_email'])){// Remove WooCommerce's default email exists error
$error_messages = $errors->get_error_messages();
foreach ($error_messages as $key => $message){if ($message === 'An account is already registered with your email address. Please log in.'){$errors->remove($key);
}
}
}
}
/**
* Assign order to existing user without requiring login.
*/
add_action('woocommerce_checkout_create_order', 'assign_order_to_existing_user', 10, 2);
function assign_order_to_existing_user($order, $data){if (!is_user_logged_in() && !empty($data['billing_email'])){$user = get_user_by('email', $data['billing_email']);
if ($user){$order->set_customer_id($user->ID);
}
}
}
/**
* Prevent auto-login after account creation for new users.
*/
add_filter('woocommerce_checkout_registration_auth_new_customer', '__return_false');
/**
* AJAX handler to check if email exists.
*/
add_action('wp_ajax_check_email_exists', 'check_email_exists_callback');
add_action('wp_ajax_nopriv_check_email_exists', 'check_email_exists_callback');
function check_email_exists_callback(){check_ajax_referer('check-email', 'security');
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
wp_send_json(array('exists' => email_exists($email)));
wp_die();
}
/**
* Enqueue dynamic checkout script to show/hide password fields.
*/
add_action('wp_footer', 'add_checkout_script');
function add_checkout_script(){if (is_checkout() && !is_user_logged_in()){?>
<script type="text/javascript">
jQuery(document).ready(function($){var emailField = $('#billing_email');
var createAccountFields = $('.create-account');
var passwordField = $('#account_password');
var createAccountInput = $('input[name="createaccount"]');
// Reinitialize WooCommerce's password toggle functionality
function initPasswordToggle(){$(document.body).on('click', '.show-password-input, .display-password', function(){var $passField = $(this).closest('.form-row').find('input[type="password"]');
var currentType = $passField.attr('type');
$passField.attr('type', 'text' === currentType ? 'password' : 'text');
$(this).text('text' === currentType ? 'Hide' : 'Show');
return false;
});
}
initPasswordToggle();
function checkEmail(){var email = emailField.val();
if (email){$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'check_email_exists',
email: email,
security: '<?php echo wp_create_nonce('check-email'); ?>'
},
success: function(response){if (response.exists){// Existing user: hide password fields, disable account creation
createAccountFields.hide();
createAccountInput.val('0');
passwordField.prop('required', false);
} else {
// New user: show password fields, enable account creation
createAccountFields.show();
createAccountInput.val('1');
passwordField.prop('required', true);
initPasswordToggle();
}
}
});
}
}
emailField.on('input', checkEmail);
checkEmail();
});
</script>
<?php
}
}
You could also get it to populate the users data at checkout too, the function to do this is below. Although i would strongly advise against it as it does pose as security/data issue if someone was randomly trying email addresses to expose customer data.
PHP:
/**
* Force the email field to the top of the checkout page.
*/
add_filter('woocommerce_checkout_fields', 'force_email_field_to_top');
function force_email_field_to_top($fields){if (isset($fields['billing']['billing_email'])){$email_field = $fields['billing']['billing_email'];
unset($fields['billing']['billing_email']);
$fields['billing'] = array('billing_email' => $email_field) + $fields['billing'];
}
// Explicitly set the email field's priority
$fields['billing']['billing_email']['priority'] = 1;
return $fields;
}
/**
* Remove "email exists" error during checkout validation for existing users.
*/
add_action('woocommerce_after_checkout_validation', 'handle_existing_email_error', 10, 2);
function handle_existing_email_error($data, $errors){if (!empty($data['billing_email']) && email_exists($data['billing_email'])){// Remove WooCommerce's default email exists error
$error_messages = $errors->get_error_messages();
foreach ($error_messages as $key => $message){if ($message === 'An account is already registered with your email address. Please log in.'){$errors->remove($key);
}
}
}
}
/**
* Assign order to existing user without requiring login.
*/
add_action('woocommerce_checkout_create_order', 'assign_order_to_existing_user', 10, 2);
function assign_order_to_existing_user($order, $data){if (!is_user_logged_in() && !empty($data['billing_email'])){$user = get_user_by('email', $data['billing_email']);
if ($user){$order->set_customer_id($user->ID);
}
}
}
/**
* Prevent auto-login after account creation for new users.
*/
add_filter('woocommerce_checkout_registration_auth_new_customer', '__return_false');
/**
* AJAX handler to check if email exists and return user data.
*/
add_action('wp_ajax_check_email_exists', 'check_email_exists_callback');
add_action('wp_ajax_nopriv_check_email_exists', 'check_email_exists_callback');
function check_email_exists_callback(){check_ajax_referer('check-email', 'security');
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
$response = array('exists' => false);
if ($user = get_user_by('email', $email)){$response['exists'] = true;
$response['user'] = array(
'billing_first_name' => get_user_meta($user->ID, 'billing_first_name', true),'billing_last_name' => get_user_meta($user->ID, 'billing_last_name', true),'billing_company' => get_user_meta($user->ID, 'billing_company', true),'billing_address_1' => get_user_meta($user->ID, 'billing_address_1', true),'billing_city' => get_user_meta($user->ID, 'billing_city', true),'billing_postcode' => get_user_meta($user->ID, 'billing_postcode', true),'billing_country' => get_user_meta($user->ID, 'billing_country', true),'billing_state' => get_user_meta($user->ID, 'billing_state', true),'billing_phone' => get_user_meta($user->ID, 'billing_phone', true)
);
}
wp_send_json($response);
wp_die();
}
/**
* Enqueue dynamic checkout script to show/hide password fields and auto-populate user data.
*/
add_action('wp_footer', 'add_checkout_script');
function add_checkout_script(){if (is_checkout() && !is_user_logged_in()){?>
<script type="text/javascript">
jQuery(document).ready(function($){var emailField = $('#billing_email');
var createAccountFields = $('.create-account');
var passwordField = $('#account_password');
var createAccountInput = $('input[name="createaccount"]');
// Fields to auto-populate
var fieldsToAutoFill = {
'billing_first_name': '#billing_first_name',
'billing_last_name': '#billing_last_name',
'billing_company': '#billing_company',
'billing_address_1': '#billing_address_1',
'billing_city': '#billing_city',
'billing_postcode': '#billing_postcode',
'billing_country': '#billing_country',
'billing_state': '#billing_state',
'billing_phone': '#billing_phone'
};
// Reinitialize WooCommerce's password toggle functionality
function initPasswordToggle(){$(document.body).on('click', '.show-password-input, .display-password', function(){var $passField = $(this).closest('.form-row').find('input[type="password"]');
var currentType = $passField.attr('type');
$passField.attr('type', 'text' === currentType ? 'password' : 'text');
$(this).text('text' === currentType ? 'Hide' : 'Show');
return false;
});
}
initPasswordToggle();
// Auto-fill fields with user data
function autoFillFields(userData){Object.entries(fieldsToAutoFill).forEach(([metaKey, selector]) => {
const value = userData[metaKey] || '';
$(selector).val(value).trigger('input');
});
// Handle country/state fields
if (userData.billing_country){$('#billing_country').val(userData.billing_country).trigger('change');
setTimeout(function(){if (userData.billing_state){$('#billing_state').val(userData.billing_state).trigger('change');
}
}, 500);
}
}
function checkEmail(){var email = emailField.val();
if (email){$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'check_email_exists',
email: email,
security: '<?php echo wp_create_nonce('check-email'); ?>'
},
success: function(response){if (response.exists){// Existing user: hide password fields, disable account creation
createAccountFields.hide();
createAccountInput.val('0');
passwordField.prop('required', false);
// Auto-fill fields with user data
if (response.user) autoFillFields(response.user);
} else {
// New user: show password fields, enable account creation
createAccountFields.show();
createAccountInput.val('1');
passwordField.prop('required', true);
initPasswordToggle();
}
}
});
}
}
emailField.on('input', checkEmail);
checkEmail();
});
</script>
<?php
}
}
Enjoy