Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login with only email and password in WooCommerce

Plugin Name: WooCommerce

plugins\woocommerce\templates\global\form-login.php

<label for="username"><?php _e( 'Email', 'woocommerce' ); ?> <span class="required">*</span></label>

It display email instead of username or email which i wanted

includes\class-wc-form-handler.php

if ( empty( $username ) ) {
    throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Email is required.', 'woocommerce' ) );
}

It display validation for email correct but it login with username also

I want it to login only with email and password before checkout

like image 533
Ghugu Avatar asked Dec 29 '25 05:12

Ghugu


1 Answers

If you look closely at wp-content/plugins/woocommerce/includes/class-wc-form-handler.php at line num 878 there is a filter woocommerce_process_login_errors, which accepts validations error, username and password. So you can overwrite this by this filter help.

Here is the code:

// 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 ID.', 'woocommerce' ) );
    }
    return $validation_error;
}

// add the filter 
add_filter('woocommerce_process_login_errors', 'filter_woocommerce_process_login_errors', 10, 3);

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works. in WooCommerce 2.6.X

Hope this helps!

like image 144
Raunak Gupta Avatar answered Dec 30 '25 21:12

Raunak Gupta