• Resolved cimot456

    (@cimot456)


    Description

    a). Partner Information

    {
    Name : Robert Jr
    Role : um_partner
    regno_partner : P150K
    }

    Robert as agent in offering products to potential consumers/users always provides a unique code (P150K) as an effort to attract potential consumers to buy the product.

    This unique code will also be displayed on the subscriber profile tab with additional information in it about how to use the code after the user has successfully registered.

    As for the form registration that I have the default is as follows (default role as subscriber):

    Unique Code (No – required):
    First Name (required):
    Last Name (required):
    Mobile_number (required):
    Cities (required):
    Email (required):
    Password:
    Confirm Password:

    As you can see, I have inserted a unique code field on form registration.

    This unique code will be stored with the regno_subscriber meta key.

    What I want to ask, how do I validate a unique code field?

    For example when there are potential customers:

    1. Enter a unique code with a value of P150K > Continue registration > data matched

    2. Enter a unique code with the value X123W > Cancel registration and give a message in the field > The unique code you entered is invalid

    3. No entered in unique code > Continue registration > fill this unique code field (with regno_subscriber meta key) with value NUC01 (default value when unique code is not filled in subscriber).

    Is this possible?

    Please help,
    Thank You

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @cimot456

    This requires customization on your end. Is the value NUC01 incremental? For example, when a user registers, the NUC01 will be assigned to that user. And then the next user who will register will be assigned with NUC02?

    Regards,

    Thread Starter cimot456

    (@cimot456)

    @champsupertramp

    For now I need a fixed value, which is NUC01 for all registered users without entering a unique code.

    But if you don’t mind for sharing 2 in version, I’ll gladly accept it.

    Who knows in the future it will require different conditions, I already have it.

    Thank You

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @cimot456

    Here’s a code snippet that you can try. Ensure that the Unique Code field has a meta key “regno_subscriber”. This only sets a fixed default value “NUC01” when the Unique Code field is not filled.

    
    add_action("um_submit_form_register","um_021522_unique_code");
    function um_021522_unique_code( $post_form ){
        
        if(  isset( $post_form['regno_subscriber'] ) && ! empty( $post_form['regno_subscriber'] ) ){
           
            if( "P150K" !== $post_form['regno_subscriber'] ){
                UM()->form()->add_error('regno_subscriber', __( 'The unique code you entered is invalid', 'ultimate-member' ) );
    		}
    	
        }  
    }
    
    add_action( 'um_registration_complete', 'um_021522_set_default_unique_code' ,1 );
    function um_021522_set_default_unique_code( $user_id ){
       
        update_user_meta( $user_id, "regno_subscriber", "NUC01" );
    }

    You can add the above code to your theme/child-theme’s functions.php file or use the Code Snippet plugin to run the code.

    Regards,

    Thread Starter cimot456

    (@cimot456)

    @champsupertramp

    Thank you for helping me.

    Here is the explanation after I used the code snippet you provided.

    1. Register by entering the P150K code in the unique code field > Saving the NUC01 value > it should be “P150K”

    2. Register by entering the code X123W in the unique code field > Saving the NUC01 value > it should be cancel the registration and display a message: The code you entered is invalid

    3. Register without entering a unique code > Saving the NUC01 value > This is correct

    I was wrong explaining at the beginning, in the “Robert as agent” section it should be “Robert as partner”..

    So please let me explain again.

    Behind I have a dynamic number of users with partner roles (eg 30 people).

    Each partner has a different code and is stored with the meta key “regno_partner”.

    Name : Michael
    roles : um_partner
    regno_partner : P150K

    Name : Samantha
    role : um_partner
    regno_partner : PL25K

    etc. (according to the number of registered partners)

    Partners share regno_partner (hereinafter referred to as a unique code) to potential customers/users as one of the conditions to get a discount when they buy one of our products.

    A unique code must be entered when the customer registers.
    For users who do not enter a unique code, they cannot apply for a discount or will get another program.

    For that, I need to create a form that dynamically checks whether a unique code is available or not.

    So I guess it would look like this:

    1. Consumers enter P150K unique code > Unique code – matching (Owned by Michael) > Continue registration > P150K unique code will be stored in the regno_subscriber meta key

    2. Consumers enter PL25K unique code > Unique code – matching (Owned by Samantha) > Continue registration > PL25K unique code will be stored in the regno_subscriber meta key

    3. The consumer enters a random/any unique code (example: X123W) > The unique code does not match (no partner has the unique code) > Cancel registration > Display a message to the field > Your unique code is invalid

    4. Consumers do not enter unique code / unique code field is empty / not filled in > Continue registration > Fill in unique code field with default value : NUC01 > Save value in regno_subscriber meta key

    I hope this additional information helps you.

    Thread Starter cimot456

    (@cimot456)

    halo…

    @cimot456

    Thanks for your explanation,
    I will make a Custom Validation code snippet implementing your Rules.

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    Thank you in advance

    @cimot456

    Now it’s time for you to start testing this code snippet:

    Set the regno_subscriber UM Forms Designer Field to Custom Validation
    and Custom Action to unique_code_field

    add_filter( 'um_add_user_frontend_submitted', 'um_before_save_filter_unique_code_field', 10, 1 );
    add_action( 'um_custom_field_validation_unique_code_field', 'um_custom_validate_unique_code_field', 30, 3 );
        
        function um_before_save_filter_unique_code_field( $args ) {
    
            if( isset( $args['regno_subscriber'] ) && empty( $args['regno_subscriber'] )) { 
                $args['regno_subscriber'] = 'NUC01';   
                $args['submitted']['regno_subscriber'] = 'NUC01';
            }    
            return $args;
        }
        
        function um_custom_validate_unique_code_field( $key, $array, $args ) {
            
            global $wpdb;
    
            if ( isset( $args[$key] )) {            
                $code_value = sanitize_text_field( $args[$key] );
                if( !empty( $code_value )) {
                    $code_field = $wpdb->get_results( "SELECT * FROM $wpdb->usermeta WHERE meta_key='regno_partner' AND meta_value='{$code_value}'" );                
                    if ( empty( $code_field ) || count( $code_field ) == 0 ) {
                        UM()->form()->add_error( $key, __( 'Your unique code is invalid.', 'ultimate-member' ) );
                    }
                }
            } 
        }
    • This reply was modified 2 years, 9 months ago by missveronica.
    • This reply was modified 2 years, 9 months ago by missveronica.
    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    I will try it now

    @cimot456

    Verify also that you get the regno_subscriber meta key value in your “New User Notification” admin email and also in the WP -> Users -> Info Display with the submitted data.

    • This reply was modified 2 years, 9 months ago by missveronica.
    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    I have tested it.

    However I haven’t been able to get it to work.

    When I register with random code – K249D (no partner has that code – regno_partner), registration is still successful.
    K249D is stored in regno_subscriber.

    With this condition the registration should be canceled and a message appears that “The code you entered is not valid).

    Can I ask for your help to review the code again?

    Thank You

    @cimot456

    Make sure that you have made the Custom Validation.

    See this example with mobile number

    https://docs.ultimatemember.com/article/94-apply-custom-validation-to-a-field

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    omg…

    I’m really stupid.

    Not read carefully.

    Even though you have explained clearly in the previous answer.

    I am really sorry.

    Now, the code that you created I have retested and it works perfectly as I want.

    I really thank you for your time and your help, I really appreciate it.

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv @champsupertramp

    I’ve tried several conditions when registering.

    So far there seems to be no problem. (And hopefully not).

    So I reconfirmed, for accepted the answer and thanks for the help.

    For that, I would like to mark this thread as resolved.

    Regards

    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Thanks for letting us know.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Validate a unique code field?’ is closed to new replies.