Support » Plugin: User Role Editor » Assign user role by text field

  • Resolved lucasl9

    (@lucasl9)


    Greetings my friends. In my woocommerce I have a field called “TYPE” and I need that when a new user registers and fills in their billing details, a role must be defined according to what is written in that field (TYPE).

    In this case it is very simple, if the field is not filled in, the new user is in the “role customer”, if he has written something the user will be added to the role “special”. Any function suggestions to do this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Vladimir Garagulia

    (@shinephp)

    Look if this hook woocommerce_new_customer_data will work for you. You can realize own logic to change new registered user role, what one you wish.

    Example of its usage is available here.

    Thread Starter lucasl9

    (@lucasl9)

    Hi Vlad. I’ve tryied this:

    add_filter('woocommerce_new_customer_data', 'wc_assign_custom_role', 10, 1);
    
    function wc_assign_custom_role($args) {
    
      if ( !empty($output['billing_inscrico_estadual'])) WC()->customer->set_is_vat_exempt( true );
      parse_str( $args, $output );
      if ( !empty($output['billing_inscrico_estadual'])) $args['role'] = 'ie';
      
      
      return $args;
    }

    Dont works. Any tips?

    Thread Starter lucasl9

    (@lucasl9)

    I tried this code but still didn’t work

    add_action( 'user_registration_after_register_user_action', 'adiciona_role_ie', 9999, 3 );
    function adiciona_role_ie( $valid_form_data, $form_id, $user_id ) {
    	$billing_ie = '';
    	if ( isset( $valid_form_data['billing_ie'] ) && ! empty( $valid_form_data['billing_ie']->value ) ) {
    		$billing_ie = $valid_form_data['billing_ie']->value;
    	}
    	if ( ! empty( $billing_ie ) ) {
        add_filter('woocommerce_new_customer_data', 'wc_assign_custom_role', 10, 1);
    
        function wc_assign_custom_role($args) {
          $args['role'] = 'ie';
          
          return $args;
        }
    		//update_user_meta( $user_id, 'ie', $role );
    	}
    }
    Thread Starter lucasl9

    (@lucasl9)

    Here is the solution:

    add_action( 'user_registration_after_register_user_action', 'ur_update_role', 10, 3 );
    function ur_update_role( $valid_form_data, $form_id, $user_id ) {
    global $table_prefix;
    $assign_roles_list = array();
    if( isset( $valid_form_data['billing_ie']) && !empty( $valid_form_data['billing_ie']->value ) ) {
    array_push( $assign_roles_list, 'ie' );
    }
    if ( ! empty( $assign_roles_list ) ) {
    // Re-ordering roles according to priority.
    $user_roles_list = ur_get_default_admin_roles();
    foreach ( $user_roles_list as $key => $value ) {
    if ( ! in_array( $key, $assign_roles_list, true ) ) {
    unset( $user_roles_list[ $key ] );
    } else {
    $user_roles_list[ $key ] = true;
    }
    }
    $field_name = $table_prefix . 'capabilities';
    update_user_meta( $user_id, $field_name, $user_roles_list );
    }
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Assign user role by text field’ is closed to new replies.