2

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
    Duplicate: [Reorder menu items in Woocommerce My Account section](https://stackoverflow.com/questions/47889365/reorder-menu-items-in-woocommerce-my-account-section) – 7uc1f3r Dec 26 '20 at 15:30

2 Answers2

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.stack.imgur.com/RWmgQ.png

Tami
  • 686
  • 4
  • 8
  • Adding this code says that i cannot redeclare the same function "affiliate_home_link". So what function should i use? – Hayder Allawi Dec 26 '20 at 15:56
  • @HayderAllawi have you replaced your affiliate_home_link function with mine above? That is what you should do! Mine is doing the same thing you were trying to achieve: Add the Affiliate Dashboard buttons and position it right before Logout. – Tami Dec 27 '20 at 13:36
  • I couldn't get what you mean. There is something wrong with the code as i cannot redeclare the same function --> affiliate_home_link. I have tried to change the function but the same error occurs. – Hayder Allawi Dec 27 '20 at 20:52
  • "i cannot redeclare the same function" => This can only happen if you are trying to use the exact function "name" (in this case affiliate_home_link) more than once. – Tami Dec 28 '20 at 00:14
  • If you remove your affiliate_home_link function and use the one I posted above instead, this redeclare issue should not happen. Besides, with the one above, you would not need yours any longer. Does this make better sense? – Tami Dec 28 '20 at 00:14
  • i have changed the function on my original code & placed your code, it didn't declare an error but it didn't work. – Hayder Allawi Dec 28 '20 at 09:36
  • That's strange @HayderAllawi, I've used this many times and for sure works. But, I wrote the function above without testing it. I will test it properly and update the answer if it really isn't working. – Tami Dec 28 '20 at 12:16
  • @HayderAllawi my function works perfectly as is. Added it to a WooCommerce site and this is the result, with the custom Affiliate Dashboard link right above the Log Out link: https://i.imgur.com/DM1eMWH.png – Tami Dec 28 '20 at 12:25
0

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' );
SaRiD
  • 1,020
  • 11
  • 15