for a WooCommerce shop, the roles customer and corporate_customer are primarily used.
As soon as a customer registers and enters a tax number in their billing address in the optional field (ID: _billing_eu_vat_number), we want to add the corporate_customer role to their existing role (customer).
Automatically adding the corporate_customer role and sending us an e-mail would be ideal.
During my search I came across this post: Assign user role by text field in Wordpress (Woocommerce)
Unfortunately we could not successfully adapt the code for ourselves. What have I done wrong?

function ur_update_role( $valid_form_data, $form_id, $user_id ) {
    global $table_prefix;

    $assign_roles_list = array();

    if ( isset( $valid_form_data['billing_eu_vat_number']) && ! empty( $valid_form_data['billing_eu_vat_number']->value ) ) {
        array_push( $assign_roles_list, 'corporate_customer' );

    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 );

Can someone help me to solve this problem here?

Thanks very much! Johannes

1 Answer 1


Add the following code snippet to your WordPress theme's functions.php file.

Define the new user role and capabilities (you can customize this based on your needs)

function add_custom_roles() {
    add_role( 'corporate_customer', 'Corporate Customer', array(
        'read' => true,
        'edit_posts' => true,
        'delete_posts' => false,
add_action( 'init', 'add_custom_roles' );

A function to update the user role based on the custom field value

function custom_user_role_based_on_tax_number( $user_id ) {
    // Get the user's billing tax number from the user meta
    $billing_tax_number = get_user_meta( $user_id, 'billing_eu_vat_number', true );
    // Check if the user has entered a tax number
    if ( ! empty( $billing_tax_number ) ) {
        // Add the custom user role 'corporate_customer'
        $user = new WP_User( $user_id );
        $user->add_role( 'corporate_customer' );
add_action( 'woocommerce_created_customer', 'custom_user_role_based_on_tax_number' );

When a customer registers and enters a tax number in their billing address during the WooCommerce checkout process, they will be assigned the custom user role 'corporate_customer'.

Please note that this code assumes that you have a custom user meta field with the meta key 'billing_eu_vat_number' in the user's profile. If you don't have this field, you will need to add as user meta.

Hope this will help!

  • Hello Vishal, many thanks for your response. Since the corporate_customer role already exists, I think the first block of code isn't necessary. I've tried every constellation, but it doesn't work. I entered the code variants as a code snippet... I also changed billing_eu_vat_number to _billing_eu_vat_number with no success. Any idea how to solve it? We would like a registered user to get an additional role as soon as they enter a UID number in their billing address, i.e. before checkout Thanks
    – Johannes
    Jul 24 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.