Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get customer data from customer session #2010

Closed
bgkavinga opened this issue Oct 3, 2015 · 14 comments
Closed

Unable to get customer data from customer session #2010

bgkavinga opened this issue Oct 3, 2015 · 14 comments

Comments

@bgkavinga
Copy link

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
    }
@bgkavinga
Copy link
Author

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;
    }

@antonkril
Copy link
Contributor

Can this issue be closed?

@sylvainraye
Copy link
Contributor

@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.

@sylvainraye
Copy link
Contributor

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

@bgkavinga
Copy link
Author

@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.

@sylvainraye
Copy link
Contributor

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

@ankita-agrawal
Copy link

ankita-agrawal commented Jun 13, 2016

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

@sengaigibon
Copy link

I think this issue is directly related to #3572

@mudithae
Copy link

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

@mudithae
Copy link

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

magento-engcom-team pushed a commit that referenced this issue Jan 30, 2018
[plankton] MAGETWO-86454: [Backport for 2.1.x] Update copyright year
@dmytro-ch
Copy link
Contributor

dmytro-ch commented May 28, 2018

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.

@adnnor
Copy link

adnnor commented Jan 17, 2019

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

@erikffflabel
Copy link

erikffflabel commented Nov 3, 2022

when cache is enabled and you have session->getCustomerId = null issue, you just need to add Factory pattern
Magento/blabla/Customer/SessionFactory

private SessionFactory $customerSessionFactory;

construct(SessionFactory $customerSessionFactory) {
$this-customerSessionFactory = $customerSessionFactory;
}

public function someFunction() {
$sessionModel = $this->customerSessionFactory->create();
return $sessionModel->getCustomer()->getId()
}

@duckchip
Copy link
Contributor

duckchip commented Jan 9, 2023

@erikffflabel thanks for the tip, this helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants