User logged in or not checking not works in other pages such as Product, Category and other static pages but its works well on customer pages like Dashboard, Address book, Orders etc...

Here is my helper: app/code/MyVendor/MyUserModule/Helper/Data.php

        namespace MyVendor\MyUserModule\Helper;
        class Data extends \Magento\Framework\App\Helper\AbstractHelper

            public function __construct(
                \Magento\Framework\App\Helper\Context $context,
                \Magento\Customer\Model\Session $customerSession
            ) {
                $this->customerSession = $customerSession;

            public function isLoggedIn()
                return $this->customerSession->isLoggedIn();
            public function userLoggedDetails()
                return $this->customerSession->getCustomer();

And phtml like this

    $customerSession = $this->helper('MyVendor\MyUserModule\Helper\Data');
    if($customerSession->isLoggedIn()) {
        echo "Welcome ".$customerSession->userLoggedDetails()->getName();
        echo "Login";

Also I noticed logged in or not checking works well on all pages while call ObjectManager directly, like this way

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

but this method not recommended by experts, so should follow Constructor injecting proper method, Anyone know what I missed, please.

  • If you enable full page cache, it stills working well? – Khoa TruongDinh Sep 22 '16 at 12:31
  • @KhoaTruongDinh I already enabled all caches from admin, is any other cache place have to enable? – Riyas Muhammed Sep 22 '16 at 15:34

Finally I got the answer, the problem with block cache, to get real Magento session, specific block cache must be false!

I did following changes and worked well:

  1. Added $this->_isScopePrivate = true; in my __construct
  2. Added attribute cacheable="false" to my block which belongs-to my template .phtml
|improve this answer|||||
  • Where you add the cacheable="false" ? I have same issue with Magento 2.1 but I do not know where to add attribute and how should look complete file. – George George Apr 4 '17 at 18:44
  • 1
    @GeorgeGeorge add cacheable="false" in your layout xml for the curresponding block. In my case on this file, /app/design/frontend/My_vendor/my_theme/Magento_Theme/layout/default.xml I have to add this block <block class="Magento\Framework\View\Element\Template" name="usermenu.us.li" template="Magento_Theme::html/user_nav_us_li.phtml" before="-" cacheable="false"/> – Riyas Muhammed Apr 6 '17 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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