Support » Plugin: WooCommerce » Assign user role by text field content

  • 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 7 replies - 1 through 7 (of 7 total)
  • Plugin Support maykato

    (@maykato)

    Hello,

    By default, a new user is automatically assigned to the “Customer” role. You might want to consider using a third-party plugin to modify user roles: https://docs.woocommerce.com/document/roles-capabilities/#section-3
    Or otherwise, we recommend getting in touch with a web developer or on of our experts here to achieve you requirement.

    Thread Starter lucasl9

    (@lucasl9)

    Hi Maykato! Thanks for your help!

    I’ve created the role that i need. Now i need a filter to apply this role to a new user only if Field Called person_type is not null.

    Plugin Support maykato

    (@maykato)

    Hi @lucasl9

    Thank you for your reply 🙂 I’m going to close this post now. If you have any other questions, please feel free to open a new topic.

    Thread Starter lucasl9

    (@lucasl9)

    Hi, i need a filter to apply one role to a new user, but only if Field Called person_type is not null.

    Thread Starter lucasl9

    (@lucasl9)

    I tried this code but it didn’t work. Any help?

    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 );
    	}
    }
    Plugin Support maykato

    (@maykato)

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.
    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce. You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter lucasl9

    (@lucasl9)

    Here is a code:

    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 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Assign user role by text field content’ is closed to new replies.