1

I've successfully added an additional tab on my woocomerce dashboard titles "Support" using the following code.

function tb_add_support_endpoint() {
add_rewrite_endpoint( 'support', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'tb_add_support_endpoint' );
function tb_support_query_vars( $vars ) {
$vars[] = 'support';
return $vars;
}
enter code here
add_filter( 'query_vars', 'tb_support_query_vars', 0 );

function tb_add_support_link_my_account( $items ) {
$items['support'] = 'Support';
return $items;
}

add_filter( 'woocommerce_account_menu_items', 'tb_add_support_link_my_account' );
function tb_support_content() {
echo '<h3>Support</h3><p>Have a question? Use the form below to get in touch.</p>';
echo do_shortcode( ' [wpforms id="XXX"] ' );
}

add_action( 'woocommerce_account_support_endpoint', 'tb_support_content' );

I would like to add another tab that is only visible to user with the user role "partnersupport"

I used the above code (replacing tb with tbvip) and tried adding:

if ( current_user_can( 'partnersupport' ) ) {

here:

if ( current_user_can( 'partnersupport' ) ) {
function tbvip_add_support_link_my_account( $items ) {
$items['support'] = 'VIP Support';
return $items; 
}
}

From what i read on similar thread this should work but it breaks everything for me. Please advise how to achieve my desired outcome.

icm
  • 71
  • 1
  • 12
  • What hook are you using? – Dev Jun 17 '22 at 08:59
  • Don't use `current_user_can` get the user object, and use `in_array( 'role', (array) $user->roles, true )` ... Also does this help solve your question? [Custom my account new menu item for a specific user role in Woocommerce](https://stackoverflow.com/questions/53598829/custom-my-account-new-menu-item-for-a-specific-user-role-in-woocommerce) – Howard E Jun 17 '22 at 09:44

1 Answers1

1

Add your conditional after the function name

function tbvip_add_support_link_my_account( $items ) {

if ( current_user_can( 'partnersupport' ) ) {

$items['support'] = 'VIP Support';

return $items;  

    }
}
Dev
  • 457
  • 6
  • 18
  • It displays the following: Warning: Invalid argument supplied for foreach() in /home/customer/www/domain.com/public_html/wp-content/plugins/woocommerce/templates/myaccount/navigation.php on line 27 I check the line in mentioned if that helps and it is: $label ) : ?> – icm Jun 17 '22 at 10:42
  • You need to add this function to a action hook. This is showing you how to add the conditional after the function. – Dev Jun 17 '22 at 15:08