Allow WooCommerce existing customers to checkout without being logged in
With WooCommerce, if an email is already associated with an account and the "create account" checkbox is ticked on the checkout page, WooCommerce will require the customer to login to his account before processing the payment.
It makes sense but it clearly hinders transformation rate. I'd rather have the customer complete the transaction and transparently associate the order with the email's matching account.
However, I have no clue how I could alter the default behaviour to make this possible.
Any idea?
Solution
Thanks to @Gabriel's answer, if finally found a way.
Gabriel pointed me to process_customer
function.
It turns out there's a hook before process_customer
is called. This hook is woocommerce_checkout_process
.
Knowing this, it's just a couple of lines:
- check if user isn't already logged in createaccount is checked
- if yes, check if a user account matches the provided email
- if not, do nothing, default behaviour is good
- if yes, then use
wp_set_current_user
to set the email matching account as the current user (won't make the user logged in!). That will have the effect of linking the order to the already existing acount (and updating shipping/billing info).
- if yes, check if a user account matches the provided email
add_action('woocommerce_checkout_process', function () {
if (!is_user_logged_in() $_POST['billing_email'] $_POST['createaccount'] == 1) {
$user = get_user_by('email', $_POST['billing_email']);
if ($user) wp_set_current_user($user-ID);
}
});
Topic woocommerce-offtopic account user-registration users Wordpress
Category Web