Woocommerce

Woocommerce is the most popular ecommerce plugin in the WordPress ecosystem. However, it has a bit of a nagging usability issue. By default, Woocommerce doesn’t show if a user is logged in or not to your ecommerce site.

Add a Woocommerce login logout link to the Woocommerce menu

At times, this can be confusing to your customers. Since most of the popular ecommerce stores (say ebay or Amazon) show a Login button in their menu, customers may expect the same in your ecommerce stores too.
However, in case of a Woocommerce powered store, a user will need to visit the ‘my account’ page to create an account or check whether he is logged in or not.

woocommerce my account - logout link

In this tutorial, we’ll explain how to add Login/Logout option to the top menu of your Woocommerce store. This is one of the more popular tips and tricks by CollectiveRay.com - so stay tuned if you want to learn more tips and tricks!

Create a Top Primary Menu

Step 1: Before proceeding any further, make sure you have added a menu as a primary menu of your theme. Also make sure you have made a full Wordpress backup incase something goes wrong
If you have not added a menu as a primary menu, log in to your WordPress admin, go to Appearance >> Menu. Under the Menu Settings create a menu and check the “Top primary menu” box.

woocommerce menu

Top tip: If you're not comfortable doing code changes, you might want to consider hiring a WordPress developer. Click here to learn how to hire the ideal WordPress developer: https://www.collectiveray.com/wordpress/wordpress-tips-and-tricks/wordpress-developers-for-hire.html

Step 2: Add the below code to your functions.php file of your theme.

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );

function add_loginout_link( $items, $args ) {

  if (is_user_logged_in() && $args->theme_location == 'primary') {

   $items .= '<li><a href="'. wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) .'">Log Out</a></li>';

  }

  elseif (!is_user_logged_in() && $args->theme_location == 'primary') {

   $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Log In</a></li>';

  }

  return $items;

}

Download the list of 101 WordPress tricks every blogger should know

101 WordPress tricks

Click here to Download Now

UPDATE: We have just been adviced by a helpful reader of the website who said that wc_get_page_id should now be used instead of the (now deprecated) woocommerce_get_page_id

Once you've changed the functions.php to add the Woocommerce login / logout links, it's time to result the result of your changes. If you have done the changes correctly, you'll see the result below.

woocommerce login / logout links

You’re done! Now you can see that the Login/Logout option is being displayed in the top menu of your store.

Download the list of 101 WordPress tricks every blogger should know

101 WordPress tricks

Click here to Download Now

Featured On

Inc Magazine Logo  

Sitepoint logo  

CSS Tricks logo   

webdesignerdepot logo   WPMU DEV logo   

and many more!

Where are we hosted?

This site is proudly powered by FAST VPS InMotion Servers and given an insane speed thanks to MaxCDN!

Web Hosting MaxCDN - Speed up your website

Advertise on CollectiveRay.com

CollectiveRay (formerly known as DART Creations) is interested in developing partnerships with mutual benefit. If you like the stuff we publish and would like to develop a relationship, we'd be happy to hear from you. Go on - drop us a line - we'd love to hear from you :-)

New! DIVI 3.1 WordPress Template + PageBuilder

You'll surely create a perfect website with Divi 3.1!

Get an exclusive 20% OFF for CollectiveRay visitors until

Divi 3

 

The Outstanding HungryJPEG Bundles

AWESOMENESS! Bundles of premium font + graphic packs at more than 96% OFF!  Get this bundle for just $29 - This month ONLY!

The Hungry JPEG Awesome font bundles

 

Disclosure: CollectiveRay is funded personally out of pure passion for helping people working with websites. We do however generate some income through recommendations of products. This means if you click on a link and purchase an item we link to, we will receive a small sum out of that sale.

who are we?

CollectiveRay is run by David Attard - working in and around the web design niche for more than 12 years, we provide actionable tips for people who work with and on websites. We also run DronesBuy.net - a website for drone hobbyists.

David attard

Follow us on Social

Follow us on Facebook   Follow us on Google+   Subscribe to our RSS Feed   Follow us on Twitter