4

I'm using WooCommerce with a child theme of Storefront.

I have a secondary menu named "Desktop secondary top right EN" where there's a link to "my account".

My question is: how to change the "my account" item wording if the customer is logged in ?

I'd like to do this without having to create another menu in the back office and without installing a plugin of course.

enter image description here

enter image description here

I should be able to use something similar to:

function menu_add_admin_buttons( $items, $args ) {
  if( 'secondary' == $args['theme_location'] ) {
    $btn_format = '<li><a href="%s">%s</a></li>';
        if ( is_user_logged_in() ) {
            $btn = sprintf($btn_format, admin_url('profile.php'), __('Your Profile') );
        } else {
            $btn = sprintf($btn_format, wp_login_url(), __('Log In') );
        }
        return $items . $btn;
    }
}
$menu_filter = 'wp_nav_menu_' . sanitize_title("Desktop secondary top right EN") . '_items';
add_filter($menu_filter, 'menu_add_admin_buttons', 20, 2);

but it has to be customized to my needs. How can I do this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Louis
  • 2,548
  • 10
  • 63
  • 120

2 Answers2

2

You can try another wordpress hook wp_nav_menu_items:

function menu_add_admin_buttons( $items, $args ) {
    $btn = '';

    if ( $args->theme_location === 'secondary' ) {
        $btn_format = '<li><a href="%s">%s</a></li>';
        if ( is_user_logged_in() ) {
            $btn = sprintf($btn_format, admin_url( 'profile.php' ), __( 'Your Profile' ) );
        } else {
            $btn = sprintf($btn_format, wp_login_url(), __('Log In') );
        }
    }

    return sprintf( '%s%s', $items, $btn );
}
add_filter( 'wp_nav_menu_items','menu_add_admin_buttons', 10, 2 );

Could you please to check the documentation.

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
1

I did this instead, and it seems to work fine. I use the standard hook : wp_setup_nav_menu_item to filter the menu items. I check if it's not the back office (admin mode) and if woocommerce is active. Then, if the url is the url of the "my account" menu, I make the change according to the current language.

add_filter( 'wp_setup_nav_menu_item','my_account_setup' );
function my_account_setup( $item ) {
    if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
        if ( $item->url == esc_url( wp_login_url() ) || strpos($item->url, '/my-account-2-2/') !== false ){
            if ( is_user_logged_in() ) {
                if(get_locale() == 'fr_FR') {
                    $item->title = 'MON COMPTE';
                } else {
                    $item->title = 'MY ACCOUNT';
                }
            } else {
                if(get_locale() == 'fr_FR') {
                    $item->title = 'LOGIN';
                } else {
                    $item->title = 'LOGIN';
                }
            }
        }
    }
    return $item;
}
Louis
  • 2,548
  • 10
  • 63
  • 120
  • I just added comments. Thanks. – Louis Jul 06 '20 at 07:54
  • 2
    This is an extremely bad code practice, while this particular example accomplished your needs, generally you should use your own theme/child theme text domain and WP [localization fuctions](https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#localization-functions) for that. – Ivan Shatsky Jul 08 '20 at 04:47