skip to Main Content

I would like to know how can i rearrange the menu tabs on My-Account Page in WooCommerce.

I have added a new menu called "Affiliate Dashboard" using the following code below but i want to show it before the "Log Out" menu so that it appears in between "Account Details" & "Log Out" menus.

So the arrangement will be like this.

1- Dashboard
2- Orders
3- Coupons
4- Addresses
5- Account Details
6- Affiliate Dashboard
7- Log out

Please see screenshot.

//First hook that adds the menu item to my-account WooCommerce menu 
 
function affiliate_home_link( $menu_links ){
 
    // we will hook "womanide-forum" later
    $new = array( 'affiliate-home' => 'Affiliate Dashboard' );
 
    // 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, 5, true ) 
    + $new 
    + array_slice( $menu_links, 5, NULL, true );
 
 
    return $menu_links;
 
 
}
add_filter ( 'woocommerce_account_menu_items', 'affiliate_home_link' );

// Second Filter to Redirect the WooCommerce endpoint to custom URL
function affiliate_home_hook_endpoint( $url, $endpoint, $value, $permalink ){
 
    if( $endpoint === 'example-forum' ) {
       
        // This is where you add the custom URL, it could be external like, in this case, we need to go to my profile on the bbpress froum
        // I will use this function (bp_core_get_username( bp_loggedin_user_id() );) to get my profile user id and add it to the URL as shown below 
        
        $url = site_url() .'/affiliate-home/' . bp_core_get_username( bp_loggedin_user_id() );
 
    }
    return $url;
 
}
 
 
add_filter( 'woocommerce_get_endpoint_url', 'forum_example_hook_endpoint', 10, 4 );

enter image description here

Any help will be appreciated.

Thanks!

2

Answers


  1. You can rearrange your $menu_items in the same function like so:

    function affiliate_home_link( $menu_links ){
    
        // Remove the logout menu item, will re-add later
        $logout_link = $menu_links ['customer-logout'];
        unset( $menu_links ['customer-logout'] );
     
        $menu_links ['affiliate-home'] = __( 'Affiliate Dashboard', 'your-plugin-or-theme-textdomain' );
    
        // Insert back the logout item.
        $menu_links ['customer-logout'] = $logout_link;
       
        return $menu_links;          
    }
    add_filter ( 'woocommerce_account_menu_items', 'affiliate_home_link', 10, 1 );
    

    Tested working: https://i.imgur.com/DM1eMWH.png

    Login or Signup to reply.
  2. You can use a custom filter and set priorities to each endpoint with a default priority of zero for each. This was my approach when I needed to achieve tweak the menu for a number of custom items.

    public function filter_woocommerce_account_menu_items( $items ) {
        // Custom menu item
        $items['quick-order-form'] = 'Quick Order Form';
        
        // Sort based on priority
        uksort( $items, function ( $a, $b ) {
            $priority  = [
                'quick-order-form' => 4,
                'customer-logout'  => 5
            ];
    
            // Check if priority has been set otherwise set to zero
            $aPriority = $priority[ $a ] ?? 0;
            $bPriority = $priority[ $b ] ?? 0;
    
            // Equal priorities can stay where they are in the array
            if ( $aPriority == $bPriority ) {
                return 0;
            }
    
            // Adjust sort based on which endpoint has more priority
            return ( $aPriority < $bPriority ) ? - 1 : 1;
        } );
    
        return $items;
    }
    add_filter ( 'woocommerce_account_menu_items', 'filter_woocommerce_account_menu_items' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search