Question

I have a virtual store with woocommerce where I use the USER REGISTRATION plugin to register new customers. This plugin allows the creation of custom fields. I created a custom text field called "STATE REGISTRATION". In this field the user can write something or leave it blank.

My demand is that when creating a new user, wordpress will read this "STATE REGISTRATION" field and see if there is any content filled in it.

If you have something filled out, I need the user to have a second "ROLE" added to their profile.

I created this role before and it's called "IE".

I'm try this, but doesnt works!

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 ) ) {
        echo $billing_ie = $valid_form_data['billing_ie']->value;
    }
    if ( ! empty( $billing_ie ) ) {
    
    add_filter('user_registration_after_register_user_action', 'wc_assign_custom_role', 10, 1);

    function wc_assign_custom_role($args) {
      $args['role'] = 'ie';
      
      return $args;
    }
        //update_user_meta( $user_id, 'ie', $role );
    }
}

enter image description here

Was it helpful?

Solution

Here is the code:

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 );
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top