1

This question already has an answer here:

So, I followed the steps in the 2nd answer (by Miroslav Petroff) on this question: How can i check whether user is logged in or not in magento 2.0

The if statement only works correctly when viewing pages like Wishlist, My Account, Cart, etc. Currently on the Homepage, Catalog pages, Product pages, and possibly others, my if statement is returning 'not-logged-in'.

if ( $helper->isLoggedIn() ):
echo ' logged-in';
else:
echo ' not-logged-in';
endif;

Anyone ideas what might make these pages return different results?

marked as duplicate by Fabian Schmengler, Rakesh Jesadiya, Raphael at Digital Pianism, Murtuza Zabuawala, David Manners Dec 22 '16 at 14:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

I ended up using this answer and now the if statement returns the correct value on all pages.

https://magento.stackexchange.com/a/108478/40091

Everyone says not to call the ObjectManager directly, but sometimes you gotta live life on the edge.

0

Try this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) 
{
// your code
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.