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 );
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 );
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
