3

I want to modify the WooCommerce "My Account" left side navigation menu.

For that, I have made changes in the woocommerce/templates/myaccount/navigation.php. The problems with this approach are:

  • I can add the new items only at the first or last position in the menu. I'd need them at the 2nd and 3rd position instead....
  • If WC gets updated, it could change...

What is the best way to customize the WooCommerce "My Account" navigation menu at my convenience?

enter image description here

16
+50

For that, you do not need to modify the woocommerce/templates/myaccount/navigation.php.

The best way to customize the "My Account" navigation menu items is to use:

This way, by using woocommerce_account_menu_items filter hook, you integrate perfectly your own items to WC, indeed:

  • Possibility to redefine your own item endpoints via the WC "Account" settings page.
  • WC updates automatically the item link's URL when, for example, a modification is done to the permalink settings/structure.

Code example:

// Note the low hook priority, this should give to your other plugins the time to add their own items...
add_filter( 'woocommerce_account_menu_items', 'add_my_menu_items', 99, 1 );

function add_my_menu_items( $items ) {
    $my_items = array(
    //  endpoint   => label
        '2nd-item' => __( '2nd Item', 'my_plugin' ),
        '3rd-item' => __( '3rd Item', 'my_plugin' ),
    );

    $my_items = array_slice( $items, 0, 1, true ) +
        $my_items +
        array_slice( $items, 1, count( $items ), true );

    return $my_items;
}

Note 1: The link's url of your items is defined automatically by WC here. To do that, WC simply append the item endpoint defined in the filter above to the "My account" page URL. So define your item endpoints accordingly.

Note 2: In your question, it seems like you modified the WooCommerce template directly in core...
woocommerce/templates/myaccount/navigation.php
When you have to modify a WC template, the correct way to do it is to duplicate the template's path relative to the woocommerce/templates folder into your theme/plugin's woocommerce folder. For example in our case, you'd have to paste the template into:
child-theme/woocommerce/myaccount/navigation.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.