I am making extra function for my own project.
function devsol_customer_login() {
if ( is_checkout() ) { ?>
<form method="post">
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" />
<button type="submit" class="woocommerce-Button button woocommerce-form-login__submit" name="login" value="<?php esc_attr_e( 'Log in', 'woocommerce' ); ?>"><?php esc_html_e( 'Log in', 'woocommerce' ); ?></button>
</form>
<?php
$user_phone = sanitize_text_field( $_POST['username'] );
if ( $user = get_user_by( 'login', $user_phone) ) {
$user_id = $user->ID;
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if ( $user_role === 'customer') {
echo 'Yes Customer';
if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $user_id ) ) {
wc_set_customer_auth_cookie( $user_id );
}
}
}
echo 'Home';
}
}
add_action('woocommerce_init', 'devsol_customer_login');
But the function is not executing on checkout page. Can someone help
I am making extra function for my own project.
function devsol_customer_login() {
if ( is_checkout() ) { ?>
<form method="post">
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" />
<button type="submit" class="woocommerce-Button button woocommerce-form-login__submit" name="login" value="<?php esc_attr_e( 'Log in', 'woocommerce' ); ?>"><?php esc_html_e( 'Log in', 'woocommerce' ); ?></button>
</form>
<?php
$user_phone = sanitize_text_field( $_POST['username'] );
if ( $user = get_user_by( 'login', $user_phone) ) {
$user_id = $user->ID;
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if ( $user_role === 'customer') {
echo 'Yes Customer';
if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $user_id ) ) {
wc_set_customer_auth_cookie( $user_id );
}
}
}
echo 'Home';
}
}
add_action('woocommerce_init', 'devsol_customer_login');
But the function is not executing on checkout page. Can someone help
At first I will notice that logging in with the username only sounds like a insecure solution .
You should not display the form in the init
action hook. Add form display function
to the action hook from checkout page (e.g. woocommerce_before_checkout_form
)
or overwrite the checkout template in the active theme and insert the form there.
if ( ! is_user_logged_in() )
{
?>
<form method="post" action="<?php echo esc_attr( admin_url('admin-post.php') ); ?>">
<input type="hidden" name="action" value="custsign" />
<?php wp_nonce_field( 'customer_signin' ); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text"
name="username" id="username" autocomplete="username"
value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" />
<button type="submit" class="woocommerce-Button button woocommerce-form-login__submit"
name="login" value="<?php esc_attr_e( 'Log in', 'woocommerce' ); ?>"><?php esc_html_e( 'Log in', 'woocommerce' ); ?></button>
</form>
<?php
}
Process the form (data validation, logging in, redirection) in the admin_post_nopriv_{action}
action hook. It is recommended to add nonce to the form.
add_action( 'admin_post_nopriv_custsign', 'customer_login');
function customer_login()
{
$return_url = wc_get_checkout_url();
if ( !isset( $_POST['username'] ) || empty($_POST['username']) )
wp_redirect( $return_url );
$nonce = wp_verify_nonce( $_POST['_wpnonce'], 'customer_signin' );
if ( false === $nonce )
{
// invalid nonce
wp_redirect( $return_url );
}
else {
$uname = sanitize_user( $_POST['username'], true );
$uid = username_exists( $uname );
//
// your permission checking code
//
if ( $uid !== false )
wp_set_auth_cookie( $uid );
}
// back to order
wp_redirect( $return_url );
}
By adding "nonce" to the form, which is sent by an unlogged user, you will probably need to use the nonce_user_logged_out
action hook for verification to work. Attach to this hook a function that returns a value instead of the user ID. Due to the Woocommerce, ID of the unlogged user when calculating the nonce will not be the same in admin_post_nopriv_{$action}
and displayed form.
add_filter ( 'nonce_user_logged_out', 'unlogged_user_nonce', 20, 2 );
function unlogged_user_nonce( $uid, $action )
{
if ( 'customer_signin' == $action)
{
// generate returned value, for example:
//
// $uid = hash( 'sha1', $action );
//
}
return $uid;
}
is_home()
,is_checkout()
, etc.) are only available after theparse_query
action hook, which in turn is executed later than theinit
hook (woocommerce_init
is executed byinit
). In other words: at the moment when thewoocommerce_init
hook is executed, the conditional tags do not yet work. – nmr Commented Jan 3, 2020 at 17:48