skip to Main Content

Actually I want customers to add unique-phone numbers in the billing address of woo-commerce. if any tries to add / update already existed phone numbers then it should throw an error.

I tried the below code but it is not working. Can anyone give me the correct solution for unique phone numbers in the Woocommerce billing address?

add_filter( 'update_user_meta', 'ts_unique_wc_phone_field');
function ts_unique_wc_phone_field( $errors ) {
    if ( isset( $_POST['billing_phone'] ) ) {
        $hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']);
            if ( !empty($hasPhoneNumber)) {
        $errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Mobile number is already used!.', 'woocommerce' ) );
    }
  }
    return $errors;
}

Customer Billing Form

2

Answers


  1. Your get_users call is wrong. Use

    $hasPhoneNumber = get_users(array(
            'meta_key' => 'billing_phone', 
            'meta_value' => $_POST['billing_phone'], 
        )
    );
    

    Careful: you did not mention your meta key in your post. This might be something else than ‘billing_phone’. Adapt it as necessary.

    This will however allow users to do shenanigans like adding a space/-/+ or something like that to the phone number, reusing it. This might need a function to filter out redundant characters upon meta value insertion, and apply the same function to $_POST['billing_phone'] before the meta query for get_users.

    Login or Signup to reply.
  2. I have setup on one of my sites the same code within two (2) functions – one for woocommerce -> my account, and one at the checkout which checks for validity of the phone number provided specific to my country, and the other to check if the phone number already exists.

    add_action( 'woocommerce_save_account_details_errors', 'wc_myaccount_validate_billing_phone', 20, 1); // My Account
    function wc_myaccount_validate_billing_phone( $args ){
    
        if ( isset ( $_POST['billing_phone'] ) && !empty ( $_POST['billing_phone'] ) ) { 
    
            if ( !preg_match( '/^04[0-9]{8}$/D', str_replace( ' ', '', $_POST['billing_phone'] ) ) ) {
    
                wc_add_notice( __( '<strong>Billing Mobile Phone</strong> is invalid (Example: 0412 345 678).' ), 'error' );
            }
    
            $existing_billing_phone = get_users( 'meta_value=' . str_replace( ' ', '', $_POST['billing_phone'] ) );
    
            $current_user = wp_get_current_user();
    
            if ( !empty ( $existing_billing_phone ) ) {
    
                if ( $current_user->billing_phone != str_replace( ' ', '', $_POST['billing_phone'] ) ) {
                    wc_add_notice( __( '<strong>Billing Mobile Phone</strong> already exists.' ), 'error' );
                }
                else { 
                    return;
                }
            }
        }
    }
    
    add_action('woocommerce_checkout_process', 'wc_checkout_validate_billing_phone'); // Checkout
    function wc_checkout_validate_billing_phone() {
    
        if ( isset( $_POST['billing_phone'] ) && !empty( $_POST['billing_phone'] ) ) { 
    
            if ( !preg_match('/^04[0-9]{8}$/D', str_replace(' ', '', $_POST['billing_phone'] ) ) ) {
                wc_add_notice( __( '<strong>Billing Mobile Phone</strong> is invalid (Example: 0412 345 678).' ), 'error' );
            }
    
            $existing_billing_phone = get_users( 'meta_value=' . str_replace(' ', '', $_POST['billing_phone'] ) );
    
            $current_user = wp_get_current_user();
    
            if ( !empty( $existing_billing_phone ) ) {
    
                if ( $current_user->billing_phone != str_replace(' ', '', $_POST['billing_phone'] ) ) {
                    wc_add_notice( __( '<strong>Billing Mobile Phone</strong> already exists.' ), 'error' );
                }
                else { 
                    return;
                }
            }
        }
    }
    

    As I want to save all phone numbers as 0412345678 (no spaces) and some people enter phone numbers as 0412 345 678, the str_replace() removes this prior to saving.

    add_action( 'woocommerce_checkout_update_user_meta', 'wc_checkout_save_billing_phone' );
    function wc_checkout_save_billing_phone( $user_id ) {   
    
        if ( $user_id && $_POST['billing_phone'] ) {
            update_user_meta( $user_id, 'billing_phone', str_replace(' ', '', $_POST['billing_phone'] ) );
        }
    }
    

    While I have not tested this next part yet, and this example is referenced from this link, if you are wanting to update the admin user area you may want to use something like this.

    add_action( 'show_user_profile', 'wc_checkout_validate_billing_phone', 10 );
    add_action( 'edit_user_profile', 'wc_checkout_validate_billing_phone', 10 );
    

    Below is a screenshot of the results of trying to change my phone number to one that already exists while in the woocommerce->my account section.

    enter image description here

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