• Resolved nagendragowda

    (@nagendragowda)


    Actually I want customers to add unique-phone numbers in the billing address of woo-commerce. The users should get an error if the phone number already exists.

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

     add_action( 'woocommerce_register_form', 'wooc_Phone_fields' );
    function wooc_phone_fields ($username, $email, $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;
    }

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi,

    Do I understand correctly that you want to validate the telephone field when registering? normally there is no telephone field there? how did you add this? because this can determine the method of validation.

    P.s. the woocommerce_register_form hook is to add fields, woocommerce_register_post can then be used to validate.

    Thread Starter nagendragowda

    (@nagendragowda)

    I want to validate the Customer billing phone number ( i want to make phone numbers unique ). Because customers going to login to the site using phone numbers. so I want to validate customers’ billing phone number.

    Thread Starter nagendragowda

    (@nagendragowda)

    I tried below code but it is giving WordPress error.

    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;
    }

    I know what your intention is, my question is how you added that phone field, because the validation will depend on it

    Thread Starter nagendragowda

    (@nagendragowda)

    Below is the custom phone field code. This code checks if the phone number exists or not during user registration. But also I want to check the user-profile also (if the phone number is already taken then it should not update there).

    ///Custom Registration Field
    ///////////////////////////////
    // 1. ADD FIELDS
     
    add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
     
    function bbloomer_add_name_woo_account_registration() {
        ?>
     
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="reg_phone"><?php _e( 'Phone Number', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="tel" class="input-text regular-text billing_phone" name="billing_phone" id="billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
        </p>
     
        <div class="clear"></div>
     
        <?php
    }
     
    ///////////////////////////////
    // 2. VALIDATE FIELDS
     
    add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );
     
    function bbloomer_validate_name_fields( $errors, $username, $email ) {
        if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
            $errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Mobile Number is required!.', 'woocommerce' ) );
        }
        
        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;
    }
     
    ///////////////////////////////
    // 3. SAVE FIELDS
     
    add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
     
    function bbloomer_save_name_fields( $customer_id ) {
        if ( isset( $_POST['billing_phone'] ) ) {
            update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
            update_user_meta( $customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']) );
        }
     
    }
    • This reply was modified 4 years, 10 months ago by nagendragowda.
    • This reply was modified 4 years, 10 months ago by nagendragowda.

    EDIT: question misread

    Note: instead of printing html in the woocommerce_register_form hook (Step 1) you can also use woocommerce_form_field(), this might be more recommended.

    See following example

    Regards

    • This reply was modified 4 years, 10 months ago by crslz.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to make Customer Billing Phone Number Unique in WordPress’ is closed to new replies.