9

app/code/Vendore/Module/Block

<?php
namespace Vendor\Module\Block;
class CustomerLink extends \Magento\Framework\View\Element\Template
{
    protected $_customersession;
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $session
    ) {
        parent::__construct($context);
        $this->_customersession=$session;
    }        
    public function sessionCheck()
    {
        return $this->_customersession->isLoggedIn();
    }

}

app/code/Vendore/Module/view/frontend/template

<?php
if($block->sessionCheck())
{
    echo 'logged in';
}
else
{
    echo 'logged out';
}
?>

It works only in customer page and checkout/cart page, remaining page it return false even customer logged in.

so I decided to use \Magento\Framework\App\Http\Context $httpContext this return right boolean value in entire website but how to get customer details using httpContext.

why \Magento\Customer\Model\Session is only working in customer page?

note: all caches are in enable mode.

2 Answers 2

22

To have access to real data in Magento 2 session you have to apply one of the following

  • Do it on non-cacheable page.
  • Disable full page cache.
  • Set $this->$_isScopePrivate = true for block where you are trying to access session.

Why:

Magento 2 is rendering public and private content for cacheable pages separately.

During cacheable page rendering Magento is cleaning all data that can be specific to a particular user (unsetting private data). Then separate ajax request is performed to load all private information and update blocks.

That's why session is empty during rendering.

The responsibility of cleaning private data lies on several depersonalization plugins. Customer session for example is cleaned by \Magento\Customer\Model\Layout\DepersonalizePlugin.

You can see conditions in which depersonalization will apply in \Magento\PageCache\Model\DepersonalizeChecker::checkIfDepersonalize

What are cacheable/non-cacheable pages:

In short words, cacheable page is any page that does not include non-cacheable blocks (blocks with cacheable="false" attribute in layout declaration). Correspondingly, pages with non-cacheable blocks are non-cacheable. :)

Here you can find the actual check: \Magento\Framework\View\Layout::isCacheable

Cacheable page examples: Category, Product view pages, CMS pages

Non-cacheable page examples: Customer account and checkotu pages

There is a more detailed Magento 2 Caching Overview by Alan Kent

6
  • great explanation! Commented Nov 15, 2016 at 14:38
  • @SergeyIvashchenko where I add this $this->$_isScopePrivate = true , I added this code on __construct but getting this error PHP Fatal error: Cannot access empty property in you mean $this->_isScopePrivate = true; instead ? Commented Nov 16, 2016 at 17:17
  • Yes, this was a typo. It should have been $this->_isScopePrivate. Note that this variable is deprecated and it should not be used. Instead, all Blocks should be made cacheable and the documented procedure of Private Content should be followed to add session-specific data to pages: magento.stackexchange.com/questions/155606/… Commented Jul 15, 2017 at 10:28
  • After disable the FPC , it work's fine but on live site we should enable the FPC. please suggest the solution Commented Jul 26, 2017 at 13:54
  • 1
    If you need this data for porpuses other than rendering - it's ok to just retrieve the data before depersonalization (i.e. before layout XML generation) Commented Sep 22, 2017 at 9:21
1

That does not seem to be an issue since Magento 2 cleans the session storage for cacheable requests in order to avoid caching of customer private content. In short, you should not access the customer session data, while processing the GET request intended to render the cacheable page.

I would also not recommend disabling the cache for the whole page via layout update as a workaround.

Magento 2 provides a special approach to work with a customer private content. You can find more details in Magento DevDocs: Private Content section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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