Site logo

[Resolved] Basic dynamic WooCommerce login/my account menu bar item

Home Forums Support [Resolved] Basic dynamic WooCommerce login/my account menu bar item

Home Forums Support Basic dynamic WooCommerce login/my account menu bar item

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2508578
    Brian

    Hello!

    Goal: add dynamic WooCommerce menu bar item in navigation with hook in custom plugin.

    I’ve added the following code to a hook element using generate_menu_bar_items with the execute PHP option checked:

    <?php
    
    function add_loginout_link() {
       if ( !is_user_logged_in() ) {
       $item = '<a class="my-account-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '"> ➤ Log in </a>';
       }
       elseif ( is_user_logged_in() ) {
       $item = '<a class="my-account-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '"> My Account </a>';
       }
       echo $item;
    }
    
    add_loginout_link();
    ?>

    It works pretty much exactly how I’d like, however this site defines DISALLOW_FILE_EDIT as true for security purposes which does not allow me to execute PHP in a hook element (I temporarily changed the config setting to test on a staging site which is how I know the code works for my needs).

    I have all site mods in a custom plugin.

    How can I write this same function to still apply to generate_menu_bar_items but in my custom plugin instead of a hook element in GP?

    #2508605
    David
    Staff
    Customer Support

    Hi there,

    you could use the add_action function to hook in your callback.

    So instad of:

    add_loginout_link();

    you can do:

    add_action( 'generate_menu_bar_items', 'add_loginout_link' );

    #2508823
    Brian

    Bingo!

    That worked perfect, thank you so much!

    This might be for a separate thread, but in the off chance I wanted the “logged in state” only to have a dropdown submenu (to show direct links to the Dashboard, Orders, Downloads, etc), is that a relatively easy add in? Could that dropdown menu pull from a menu that was setup in Appearance > Menus?

    Thanks again!

    #2509559
    David
    Staff
    Customer Support

    OK, if it has to be a button in the menu bar items.
    Then an alternative solution would be to use the GP Block element:

    https://docs.generatepress.com/article/block-element-hook/

    You can add a core Navigation Block.
    And add it the menu_bar_items hook.

    Set the Display Rules > Location to Entire Site
    And the User > Logged In.

    That menu can contain the sub menu items

    Then repeat that for the User > logged Out.

    Is that an option ?

    #2511218
    Brian

    Oh I very much like that as an option—you’re good!

    I’ll play around a bit but I’ll likely just start with no dropdown and the first option we spoke about but will definitely keep the Block Element idea in the back of my mind as well.

    Thanks so much!

    #2511790
    David
    Staff
    Customer Support

    You’re welcome!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.