Loading...

How To Identify Logged In Customers

Emilian F. Published on 22 September, 2014

Many store owners would like to display a message only for logged in users. The problem is that there is no easy way to do that in a template. The only alternative is to use use some Javascript to differentiate between guests and logged in customers.

I will show you two methods to do that: one relies on finding a specific text inside of the top menu, and one relies on the first name variable. Both need Javascript to work. Pick whichever method you think is easiest to implement!

Instructions

  1. Login to your control panel and navigate to Design > Themes > Edit HTML & CSS and open the Footer.html file.

  2. Add the following code to the file:

    
    <script type="text/javascript">
    $(document).ready(function(){
    var $loggedIn = ("#TopMenu a:contains('Sign out')");
    
    if($loggedIn) {
      // Customer is logged in
    } else {
      // Guest
    }
    
    });
    <script>
    
    
    
  3. Save the changes.

And here is the second method:

  1. Login to your control panel and navigate to Design > Themes > Edit HTML & CSS and open the TopMenu.html file. You can place the code in any template file because this variable is truly global.

  2. Add the following code to the file:

    
    <script type="text/javascript">
    
    var firstName ='%%GLOBAL_CurrentCustomerFirstName%%';
    
    if ((firstName!='') && (firstName !='Guest'))
    {
      // Customer is logged in
    } else {
      // Guest
    }
    <script>
    
    
    
  3. Save the changes.

How It Works

The first method looks to the top menu to find the sign out text. If the text is there then we know the customer is logged in, since only a logged in customer would have a sign out link. The drawback to this method is that the script runs only after the document has been loaded.

The second method does not have to wait until the document is loaded. It uses a global variable named %%GLOBAL_CurrentCustomerFirstName%% and compares it against two values: a blank value, and a 'Guest' value. Either one of those values denotes a guest. Of course, it could also mean that you have a customer with Guest as the first name but that's a rarity.

I hope this article has shown you exactly what you need to do in order to identify logged in customers. You can then build on top of these scripts to show them custom messages. Of course, you can also do the opposite: you can display messages to your guests to persuade them to create an account, or give them a coupon code/

The possibilities are only limited by your imagination, so get started customizing your store today and increase your sales!

Emilian F. Published on 22 September, 2014