Support » Plugin: WCFM Membership - WooCommerce Memberships for Multivendor Marketplace » Assign user role based on input text field

  • Resolved lucasl9

    (@lucasl9)


    Hi. My woocommerce has 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 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WC Lovers

    (@wclovers)

    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?

    – You may achieve this but for the purpose you have to edit WooCommerce user registration process. WCFM does not assign this user roles, works only for vendor users.

    Thread Starter lucasl9

    (@lucasl9)

    Do you have any code or snippet to change this behavior of woocommerce registration?

    Plugin Author WC Lovers

    (@wclovers)

    Sorry, we do not have any such readymade code right now.

    Thread Starter lucasl9

    (@lucasl9)

    Here is the solution, in my case:

    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 );
    }
    }
    Plugin Author WC Lovers

    (@wclovers)

    Thanks for sharing here 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Assign user role based on input text field’ is closed to new replies.