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