You can achieve this by using a hook to modify the checkout fields based on the user’s role. Here’s a custom function that you can add to your WordPress theme’s functions.php file. This function checks the user’s role and conditionally modifies the required status of certain checkout fields such as phone and email. Just as an example.
function customize_checkout_fields_for_roles( $fields ) {
// Check if the user is logged in
if ( is_user_logged_in() ) {
// Get the current user
$user = wp_get_current_user();
// Check if the user has the 'wholesale' role
if ( in_array( 'wholesale', (array) $user->roles ) ) {
// Make phone field not required
$fields['billing']['billing_phone']['required'] = false;
// Make email field not required
$fields['billing']['billing_email']['required'] = false;
}
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'customize_checkout_fields_for_roles' );
Here’s what this code does:
1.) The function customize_checkout_fields_for_roles is defined to modify the checkout fields.
2.) It checks if a user is logged in and retrieves the current user.
3.)If the user has the ‘wholesale’ role, it sets the required property of the billing_phone and billing_email fields to false.
4.)The function is hooked to woocommerce_checkout_fields, which allows it to modify the checkout fields before they are displayed.
You can add more fields or conditions based on other user roles by expanding the conditions within this function. Just ensure that the role names match exactly what is configured in your WordPress site and that the field keys (billing_phone, billing_email, etc.) correspond to the actual IDs used by WooCommerce.
Hope that helps