Skip to main content

How to create login with phone number in woocommerce wordpress?

Create login with phone number in woocommerce wordpress.



function wooc_extra_register_fields() {?>
       <p class="form-row form-row-wide">
       <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
       </p>
   
       <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );




 function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {

if ( isset( $_POST['billing_phone'] ) ) {
   $hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']);
   if ( !empty($hasPhoneNumber)) {
     $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Mobile number is already used!.', 'woocommerce' ) );
   }
}
 
         return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );




 /**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
 
 

}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );




function otp_form(){
$slug = basename(get_permalink());
?>
<form action="" method="post">
<label>Enter Phone number <span class="required">*</span></label>
<input type="text" name="phone_number" class="phone_number">
<input type="hidden" name="slug" class="slug" value="<?php echo $slug; ?>">
<input type="submit" name="otp_submit" Value="Send Otp">
</form>
<?php
}
add_action('woocommerce_login_form_end','otp_form');



function otp_login(){

if(isset($_POST['otp_submit'])){

$customfield = $_POST['phone_number'];

$slug_name = $_POST['slug'];

if(!empty($customfield)){

$UsermetaData = get_users('meta_value='.$customfield);
//print_r($UsermetaData);
foreach($UsermetaData as $userdata)
{

$user = $userdata->ID;

}
if(is_null($user)){
wc_add_notice( 'You must enter registered phone number for otp!', 'error' );
return false;
}



if(!empty($user))
{
global $wpdb;
global $post;

if($slug_name=='checkout')
{

if (!is_wp_error($user))
{

wp_clear_auth_cookie();
wp_set_current_user($user);
wp_set_auth_cookie($user);
$redirect_to = wc_get_checkout_url();
wp_safe_redirect( wc_get_checkout_url() );
exit();
}
return $user;
}
else

{


if (!is_wp_error($user))
{
wp_clear_auth_cookie();
wp_set_current_user($user);
wp_set_auth_cookie($user);
$redirect_to = user_admin_url();
wp_safe_redirect( user_admin_url() );
exit();
}
return $user;


}

}
}
else
{

wc_add_notice( 'You must enter registered phone number for otp!', 'error' );
return false;
}
}
}
add_action('init','otp_login');




function add_billing_mobile_phone_to_edit_account_form()
 {
    $user = wp_get_current_user();
    ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="billing_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" />
</p>
    <?php
}
add_action( 'woocommerce_edit_account_form_start', 'add_billing_mobile_phone_to_edit_account_form' );


// Check and validate the mobile phone

function billing_mobile_phone_field_validation( $args )
{
if ( isset( $_POST['billing_phone'] ) )
{
   
$hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']);
global $current_user;
get_currentuserinfo();
$current_user_id = $current_user->ID;
$billing_phone = get_user_meta( $current_user_id, 'billing_phone', true );
   if ( !empty($hasPhoneNumber) && $billing_phone !== $_POST['billing_phone'])
{
$args->add( 'billing_phone_error', __( '<strong>Error</strong>: Mobile number is already used!.', 'woocommerce' ) );
}
}
}
add_action( 'woocommerce_save_account_details_errors','billing_mobile_phone_field_validation', 20, 1 );

// Save the mobile phone value to user data

function my_account_saving_billing_mobile_phone( $user_id )
{
if( isset($_POST['billing_phone']) && ! empty($_POST['billing_phone']) )
        update_user_meta( $user_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']) );
}

add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_mobile_phone', 20, 1 );



function show_login_option()
{
$slug = basename(get_permalink());
if($slug=='checkout')
{
if ( is_user_logged_in() )
{


}
else
{
?>
<script>
jQuery(document).ready(function($)
{
  $(window).load(function() {

$('.showlogin').click();
//alert('hello');
});
});

</script>

<?php
}
}
}
add_action('wp_head','show_login_option');


Comments

Popular posts from this blog

How to use inner html value or data in php from javascript(innerHTML)?

 use inner html value or data in php from javascript(innerHTML)? <html> <body> <p id="demo">use inner html value in php(innerhtml)</p> <script>   var jassi = document.getElementById("demo").innerHTML;   //document.write(jassi); </script> <?php $jassi = '<script>document.write(jassi);</script>'; echo $jassi; ?> </body> </html>

How to get store data from WCFM(Best Multi Vendor Marketplace) Plugin?

 Get store data from WCFM(Best Multi Vendor Marketplace) Plugin. global $WCFM, $WCFMmp, $wp, $WCFM_Query, $post; $store_id = ''; if ( isset( $attr['id'] ) && !empty( $attr['id'] ) ) { $store_id = absint($attr['id']); } if (  wcfm_is_store_page() ) { $wcfm_store_url = get_option( 'wcfm_store_url', 'store' ); $store_name = apply_filters( 'wcfmmp_store_query_var', get_query_var( $wcfm_store_url ) ); $store_id  = 0; if ( !empty( $store_name ) ) { $store_user = get_user_by( 'slug', $store_name ); } $store_id    = $store_user->ID; } $user_id = $store_id;  $vendor_data = get_user_meta( $user_id, 'wcfmmp_profile_settings', true ); $street_1 = $vendor_data['address']['street_1']; $street_2 = $vendor_data['address']['street_2']; $city     = $vendor_data['address']['city']; $zip      = $vendor_data['address']['zip&#