Give alerts or important notes to your users by adding custom messages when they visit your shop page to help them out. Here we are going to add a custom message for the subscriber role which you can change with your desired role. In the below image there is no such message present.
Now let’s display the message after applying the snippet. Simply add this to your function.php file in your child theme.
function wps_custom_message() {
//you can change the desire user role to show a message
if (is_user_logged_in()) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
if (in_array("subscriber", $roles)) {
echo esc_html ('Welcome back,' .$user->display_name. ' (Subscriber) we hope all is going good','text-domain');
}
}
}
//you can change woocommerce_archive_description to other hooks, like
//woocommerce_before_shop_loop, woocommerce_after_shop_loop
add_action( 'woocommerce_archive_description', 'wps_custom_message', 9 );
In the above function first, we are checking if the user is logged in or not. If Yes, then we are taking details of the user in the $user variable and also getting the role in the $role variable.
Then we are matching the given user role to the $role variable to display a custom message to the desired user.