Dynamic switch My Account / Login menu state – Block Theme
-
Hey Woo
I am trying to get “My Account” menu item to switch to “Login” when user is not signed in. I tried this snippet that will not work as is, so I am guessing it’s non compatible with block theme nav menu.
/** * @snippet Rename "My Account" Link @ WooCommerce/WP Nav Menu * @how-to Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 4.5 * @community https://businessbloomer.com/club/ */ add_filter( 'wp_nav_menu_items', 'bbloomer_dynamic_menu_item_label', 9999, 2 ); function bbloomer_dynamic_menu_item_label( $items, $args ) { if ( ! is_user_logged_in() ) { $items = str_replace( "My Account", "Login", $items ); } return $items; }
Also there is one more snippet and I am confused if both are required, but when pasting both into functions I get an error. Snippet 2:
/** * @snippet Rename Custom Link Label @ WooCommerce/WP Nav Menu * @how-to Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 4.5 * @community https://businessbloomer.com/club/ */ add_filter( 'wp_nav_menu_items', 'bbloomer_dynamic_menu_item_label', 9999, 2 ); function bbloomer_dynamic_menu_item_label( $items, $args ) { if ( ! is_user_logged_in() ) { $items = str_replace( "Account", "Login", $items ); } return $items; }
I have renamed “My Account” page to “My Space”, so is that a conflict for the block theme implementation?
All help is appreciated
Thx
-
Hi @hebhansen,
The code snippets you’ve shared are indeed meant to do just that. However, if you’re using a block theme, it might not work as expected due to how block themes handle navigation menus.
For the issue with the second snippet, it looks like you’re trying to declare the same function twice, which would cause an error. If you want to use both snippets, you’ll need to rename one of the functions to something unique.
However, I’d recommend looking into block theme compatible solutions for dynamic menu items. There are plugins available that can help with this, such as Block Visibility, or you might need to customize the theme code directly.
Please note that writing or providing custom code is not within the scope of our support policy. If you still need assistance, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack or the WooCommerce Advanced Facebook group. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:
- A local web developer
- Codeable.io
- WooExperts
I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.
I would prefer a snippet to avoid tons of plugins for anything and all. Theme is TT4, so must work for blocks
Hi @hebhansen,
Please note that this query is not specific to WooCommerce (changing a menu depending on login status would be a basic WordPress query, not necessarily tied to Woo), and even more the way you require to do it through custom code, means customization. Both things mean this is outside of our support scope, I’m afraid.
However, I trust a quick Google Search can lead you in the right direction, though. And this thread will be open to answers for a while longer in case someone in the community can provide a ready to go solution for you!
Cheers!
-OPHere is what I find, such as this thread, but no solution
also… should this not be a core woo feature?
- This reply was modified 2 months, 2 weeks ago by hebhansen.
Hello @hebhansen,
also… should this not be a core woo feature?
Just as my colleague had earlier shared above, this is not a Woo feature, and therefore we are unable to provide support.
However, you could try running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code.
Hope it helps!
- You must be logged in to reply to this topic.