John Davidson

php - Change Woocommerce My Account Tab name depending on User Role

0 comments
Message:


I am building a ecommerce with Woocommerce, and I would like to change My Account Tabs name depending on User role (some user roles have access to several products and some just one product so I need to implement plural/singular variations for the tab name). Is there a function to achieve this ? I've found some snippets that create more or less similar actions but can't find out how to make it work "my way".


Thanks for your help !



My Answer:
You can achieve this by using the `woocommerce_account_menu_items` filter hook in your theme's functions.php file. Here's an example code snippet to change the My Account tab name depending on the user role:

php
function custom_account_menu_items( $items ) {
// Get the current user role
$user = wp_get_current_user();
$user_roles = $user->roles;
$user_role = array_shift($user_roles);

// Change the tab name based on user role
if ( $user_role == 'customer' ) {
$items['downloads'] = 'My Downloads';
} elseif ( $user_role == 'subscriber' ) {
$items['downloads'] = 'My Download';
}

return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_account_menu_items' );


In this code snippet, we are checking the current user's role and changing the My Account tab name based on the user role. You can add more conditions and change the tab names as needed for different user roles.

Make sure to replace 'customer' and 'subscriber' with the actual user roles in your WordPress setup. You can find the user roles by going to Users > All Users in the WordPress admin dashboard.

After adding this code to your functions.php file, the My Account tab name should change dynamically based on the user role.

Rate this post

4 of 5 based on 9057 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us