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!