Okay
  Public Ticket #4038333
Enforcing Unique Registration Fields
Closed

Comments

  •  1
    Lindsey started the conversation

    We have a site that we have people registering with their Employee ID. How do we enforce that only one person can sign up per ID?

  •  1
    Lindsey replied

    This is our registration form.

    Attached files:  Settings-‹-Ward-Gear-—-WordPress-06-04-2025_11_49_AM.png

  •  2,460
    WebWizards replied

    Hi Lindsey,

    I believe I could write a custom code snippet that checks if the Employee ID already exists in the system before allowing a new registration. If someone tries to register with an ID that's already been used, they would receive an error message.

     

    1. Are there any specific requirements for the Employee ID format (like a certain number of characters or a particular pattern)?
    2. Could you please share the URL of your registration page? 

     

    Kind regards,
    Stefan

  •  1
    Lindsey replied

    Hi Stefan, thanks so much for your quick response.

    This is the registration page: https://www.wardgear.com/my-account/

    The employee IDs are numbers only and the longest one we've had is 5-digits.

  •  2,460
    WebWizards replied

    Thank you for clarifying,

     

    I am not able to access the site itself for some reason - not sure if the site is down or there's a firewall. In any case, I wrote a code snippet that you can add to the site functions.php:

    add_action('woocommerce_register_post', 'validate_unique_employee_id', 10, 3);
    
    function validate_unique_employee_id($username, $email, $validation_errors) {
        $employee_id_field_number = '1234';
        
        $field_name = 'b2bking_custom_field_' . $employee_id_field_number;
        
        // Check if the Employee ID field was submitted
        if (isset($_POST[$field_name]) && !empty($_POST[$field_name])) {
            $employee_id = sanitize_text_field($_POST[$field_name]);
            
            // Query to check if any user already has this Employee ID
            $existing_users = get_users(array(
                'meta_key'     => $field_name,
                'meta_value'   => $employee_id,
                'meta_compare' => '=',
                'number'       => 1, // We only need to know if at least one exists
                'fields'       => 'ID' // Only return user IDs for efficiency
            ));
            
            // If we found any existing users with this Employee ID
            if (!empty($existing_users)) {
                $validation_errors->add(
                    'employee_id_exists', 
                    __('This Employee ID is already registered. Each Employee ID can only be used once.', 'textdomain')
                );
            }
        }
    }
    

     

    It is necessary to replace 1234 with the unique id of that "employee id" field.

    To find that ID, go to B2BKing -> Registration Fields -> Employee ID, and check the number in the URL (e.g. ?post=123 - that number is the ID you need).

     

    Let me know if that works for you,

     

    After adding that, it should throw an error during registration if another user is already registered with the same employee ID.

     

    Kind regards,

    Stefan

  •  1
    Lindsey replied

    Thank you so much Stefan!