Skip to content
Advertisement

WooCommerce: Display My Account link only for specific user role

I want to display the subscription link in the WooCommerce My Account navigation only to a specific user role. But I couldn’t figure out how to change the navigation in that way.

I found a solution to change the order of the menu items. And I found a solution to add custom links to the navigation. But unfortunately I couldn’t combine these two to a working snippet.

Here’s the code to change the order:

 function my_account_menu_order() {
    $menuOrder = array(
        'orders'             => __( 'Orders', 'woocommerce' ),
        'downloads'          => __( 'Download', 'woocommerce' ),
        'edit-address'       => __( 'Addresses', 'woocommerce' ),
        'edit-account'      => __( 'Account Details', 'woocommerce' ),
        'customer-logout'    => __( 'Logout', 'woocommerce' ),
        'dashboard'          => __( 'Dashboard', 'woocommerce' ),
    );
    return $menuOrder;
 }
 add_filter ( 'woocommerce_account_menu_items', 'my_account_menu_order' );

Because the links are in an array, I couldn’t use if/else to check the user role.

Here’s an example to add custom links to the navigation: (from here: https://rudrastyh.com/woocommerce/my-account-menu.html#add_with_url)

add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );
function misha_one_more_link( $menu_links ){

    // we will hook "anyuniquetext123" later
    $new = array( 'anyuniquetext123' => 'Gift for you' );

    // or in case you need 2 links
    // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );

    // array_slice() is good when you want to add an element between the other ones
    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );


    return $menu_links;


}

add_filter( 'woocommerce_get_endpoint_url', 'misha_hook_endpoint', 10, 4 );
function misha_hook_endpoint( $url, $endpoint, $value, $permalink ){

    if( $endpoint === 'anyuniquetext123' ) {

        // ok, here is the place for your custom URL, it could be external
        $url = site_url();

    }
    return $url;

}

But the subscription endpoint already exists. So I don’t know how to add it in this way.

Any ideas?

Advertisement

Answer

Sorry, I found a solution.

The following code does the trick:

add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );
function misha_one_more_link( $menu_links ){

    // we will hook "anyuniquetext123" later
    $new = array( 'subscriptions'   => __( 'Subscriptions', 'woocommerce' ), );

    // or in case you need 2 links
    // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );

    // array_slice() is good when you want to add an element between the other ones
    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );


    return $menu_links;


}

I just need to add the existing endpoint.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement