skip to Main Content

so I’ve implemented a phone login and register option in WooCommerce with the code:

function wooc_add_phone_number_field() {
    return apply_filters( 'woocommerce_forms_field', array(
        'wooc_user_phone' => array(
            'type'        => 'text',
            'pattern'     => '[0][0-9]{9}',
            'label'       => __( 'Phone Number', ' woocommerce' ),
            'placeholder' => __( 'MUST START WITH 0 e.g 081 123 4567', 'woocommerce' ),
            'required'    => true,
        ),
    ) );
}
add_action( 'woocommerce_register_form', 'wooc_add_field_to_registeration_form', 15 );
function wooc_add_field_to_registeration_form() {
    $fields = wooc_add_phone_number_field();
    foreach ( $fields as $key => $field_args ) {
        woocommerce_form_field( $key, $field_args );
    }
}


//Store
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
function wooc_save_extra_register_fields( $customer_id ) {
    if (isset($_POST['wooc_user_phone'])){
        update_user_meta( $customer_id, 'wooc_user_phone', sanitize_text_field( $_POST['wooc_user_phone'] ) );
    }
}

//Check against phone
function wooc_get_users_by_phone($phone_number){
    $user_query = new WP_User_Query( array(
        'meta_key' => 'wooc_user_phone',
        'meta_value' => $phone_number,
        'compare'=> '='
    ));
    return $user_query->get_results();
}


//Authenticate 
add_filter('authenticate','wooc_login_with_phone',30,3);
function wooc_login_with_phone($user, $username, $password ){
    if($username != ''){
        $users_with_phone = wooc_get_users_by_phone($username);
        if(empty($users_with_phone)){
            return $user;
  }
  $phone_user = $users_with_phone[0];
  
  if ( wp_check_password( $password, $phone_user->user_pass, $phone_user->ID ) ){
   return $phone_user;
  }
    }
    return $user;
}


//Login label
add_filter( 'gettext', 'wooc_change_login_label', 10, 3 );
function wooc_change_login_label( $translated, $original, $domain ) {
    if ( $translated == "Username or email address" && $domain === 'woocommerce' ) {
        $translated = "Username or email address or phone";
    }
    return $translated;
}

Which works perfectly fine however what I want to do is then populate the phone field in the Billing Address with the value that is stored here so when a user goes to Billing Form theyll find the same details they entered when registering.

2

Answers


  1. Chosen as BEST ANSWER
    add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
    function wooc_save_extra_register_fields( $customer_id ) {
        if (isset($_POST['wooc_user_phone'])){
            
            update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['wooc_user_phone'] ) );
        }
    }
    

  2. You do this by updating the users account meta:

    update_user_meta( $user_id, 'billing_phone', $phone_number);
    

    https://developer.wordpress.org/reference/functions/update_user_meta/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search