Magento2: Unable to get customer data from customer session

Created on 3 Oct 2015  路  12Comments  路  Source: magento/magento2

I have DI in constructor to inject customer session to my class \Magento\Customer\Model\Session $customerSession, But the injected session object does not contain customer id or customer reference.I also checked the injected session on Magento\Catalog\Block\Product\View it also does not contain customer id or reference

public function __construct(
        TemplateContext $context,
        Session $customerSession,
        CustomerRepositoryInterface $customerRepository,
        HttpContext $httpContext,
        array $data = []
    ) {
        $this->customerSession = $customerSession;
        $this->customerRepository = $customerRepository;
        $this->httpContext = $httpContext;
        $this->scopeConfig = $context->getScopeConfig();
        parent::__construct($context, $data);

        // customer session does not contain customer data
        $cid=$this->customerSession->getId(); // returns null
    }

Most helpful comment

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.

All 12 comments

After some investigation we found that Magento/Customer/Model/Layout/DepersonalizePlugin.php::afterGenerateXml() is clearing the data from customer session when cache is enabled

public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result)
    {
        if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
            $this->visitor->setSkipRequestLogging(true);
            $this->visitor->unsetData();
            $this->session->clearStorage();
            $this->customerSession->clearStorage();
            $this->session->setData(\Magento\Framework\Data\Form\FormKey::FORM_KEY, $this->formKey);
            $this->customerSession->setCustomerGroupId($this->customerGroupId);
            $this->customerSession->setCustomer($this->customerFactory->create()->setGroupId($this->customerGroupId));
        }
        return $result;
    }

Can this issue be closed?

@bgkavinga and how did you fix your issue ? I have a similar problem with a block which needs customer information. Due to the plugin which depersonalize customer data in Customer Module, I cannot get the customer data when the cache is enabled. This depersonalizeChecker method is called before the block is instantiated.

Looks like that you may find the answer here: http://magento.stackexchange.com/questions/86674/block-with-cachable-false-not-rendered-on-product-view-page/90130#90130

The block must not be in saved in cache (which is quite logic) however it must also be the property of the block must be set to _isScopePrivate = true

@diglin My first solution was disable cache for that block in layout file. But it slows down the page load. When it comes to Varnish it always do esi update for that block. So I had to get customer session data directly from the $_SESSION until they come up with solution. May be this has been fixed in GA release.

@bgkavinga thanks for your feedback. The solution from stack exchange worked for me. Do not forget to clean your cache.

Any updates for this issue on latest magento2 version? I tried $_SESSION but its not working for category and product pages.

I think this issue is directly related to #3572

It seems that this issue is still exist even in the 2.1.6. Any workaround is appreciated.
$_SESSION['customer_base']['customer_id'] is also null

Finally below workaround worked for me. (Clue from : https://github.com/magento/magento2/issues/3294 post by @saravananvelu)
$customerSession = $this->_objectManager->create('Magento\Customer\Model\SessionFactory')->create(); return $customerSession->getCustomer()->getId();

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.

As @dmytro-ch said you have to use a special Magento approach to work with the private content. Here is an example https://github.com/adnnor/customer-session-sections

Was this page helpful?
0 / 5 - 0 ratings