I have Googled and searched and wept and screamed, trying to figure out how to display content in two different ways – one way to a logged in user and a different way to a not logged in user. In theory, it is all very simple PHP scripting that would take care of it, and it really is – IF you know how to do it – which I did not.
In my case, I wanted to implement some code that would – through CSS, JS, and HTML – display a link in my Magento header for “Quick Log In.” The BASIC function of this was to get rid of the log in page and thus decrease HTML requests to my server. Also, it seemed a little pointless to load a separate page for logging in when the same functionality could be integrated right into EVERY OTHER Magento page. However, I only wanted this “Quick Log In” link to display to those who were not logged in – for (hopefully) obvious reasons. However, I had added another page to my site that allowed a logged in customer to add products to their cart by simply entering the SKU and the quantity. This is my “Quick Add To Cart” link. I wanted this link to display on the page to logged in users IN PLACE of the “Quick Log In” link. (See images below – top is logged in)
links
The big question became – HOW? Well, I am familiar with PHP, so I thought that it wouldn’t be too hard. I was a little mistaken, although in the end it really did turn out to be not too hard; it just took a little while to get there. Here’s the break down.
This is what I expected it to be, but of course, didn’t work.
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()) {
<code here for logged in users>
} else {
<code here for NOT logged in users>
} ?>
Using the above code, I would always get an error saying something like “unexpected ‘<’ in /home/*******/public_html/magento/app/blah…blah…blah on line 61.” I couldn’t figure it out! Why in the world would it NOT expect code in there. Well, it just wouldn’t work, so I had to bust out a couple hours of Google-Fu. Answer below.
To get Magento to display code to logged in users and different code to not logged in users, implement the following code into your template where you need that functionality.
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()): ?>
<place code for logged in users here - can be PHP, HTML, whatever - I used both>
<?php else: ?>
<place code for NOT logged in users here - can be PHP, HTML, whatever>
<?php endif; /* if ($this->isCustomerLoggedIn()): */ ?>
That’s all there is to it. Nothing more, nothing less. Works like a charm – everytime. I am sure that this can be extended to display certain static content to one customer group and show other content to other customer groups. Using Magento PHP like getData() and getAttributeText() with this, you could customize unlimited amounts of blocks and code and whatnot. Remember, your page is about USER FUNCTIONALITY – give them what they want – but also give them what they need. Not everyone is going to need to know that users of a specific group get special pricing. Ok – I’m starting to brainstorm now instead of blog, so I gotta go.


