magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Cannot get customer session data

Open Nuranto opened this issue 10 years ago • 35 comments

Event : catalog_product_get_final_price

I need the current logged in customer id.

On Observer :

    public function __construct(\Magento\Customer\Model\Session $customerSession)
    {
        var_dump($customerSession->getData());
    }

=>

  &array(5) {
    ["customer_group_id"]=>
    string(1) "1"
    ["customer_id"]=>
    NULL
    ["default_tax_billing_address"]=>
    NULL
    ["default_tax_shipping_address"]=>
    NULL
    ["customer_tax_class_id"]=>
    NULL
  }

But I'm logged in in front, this is confirmed when using
$this->_httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH):

array(2) {
  ["customer_group"]=>
  string(1) "1"
  ["customer_logged_in"]=>
  bool(true)
}

Is this a bug ? Or am I wrong somewhere ?

(Note : if I dump $_SESSION, customer_id is NULL too :()

Nuranto avatar Feb 05 '16 08:02 Nuranto

Internal ticket ID MAGETWO-49202

sevos1984 avatar Feb 15 '16 13:02 sevos1984

I can confirm this bug, when I'm logged in fronted

When I use this :

$ObjectManager = \Magento\Framework\App\ObjectManager::getInstance();
$session =  $ObjectManager->get('Magento\Customer\Model\Session');
return $session->isLoggedIn();

My return is false...

But, when I used this, it's work fine :

$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
return $isLoggedIn;

quienti avatar May 04 '16 16:05 quienti

Experiencing the same problem. When I disable cache bin/magento cache:disable I can get if a customer is loged in from the Session object.

SophiaKtv avatar May 26 '16 22:05 SophiaKtv

Experiencing the same problem on version 2.0.2. Any updates for this issue or it is solved in any latest version ? I tried $_SESSION but its not working for category and product pages.

deepanshu27193 avatar Jun 22 '16 08:06 deepanshu27193

Related to #3572

sengaigibon avatar Jul 29 '16 10:07 sengaigibon

Experiencing the same problem on version 2.0.8. The session object disappeared sometimes. Need to disable the cache to make it work.

zzpaul avatar Aug 12 '16 17:08 zzpaul

I have installed Magento 2.1.1 latest version still facing this issue which is not able to retrieve customer data from session in product view page, when cache is enabled

saravananvelu avatar Sep 15 '16 06:09 saravananvelu

Also bug in Magento 2.1. I noticed that having all types of cache except Full page cache getting data from session works normally. So I guess the problem is in Full page cache.

I think it's related issue: https://github.com/magento/magento2/issues/6392

bartoszkubicki avatar Sep 15 '16 13:09 bartoszkubicki

$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance(); $context = $ObjectManager->get('Magento\Framework\App\Http\Context'); $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); return $isLoggedIn;

This thing worked for checking customer is logged in or not when page cache is enable We need to page cache for showing count for compare product , wishlist , cart count initially when customer gets logged in

KalaiarasanSeetharaman avatar Oct 04 '16 14:10 KalaiarasanSeetharaman

Hi @KalaiarasanSeetharaman, from this how to get customer ID i.e. how to get which customer is logged in.

deepanshu27193 avatar Oct 05 '16 05:10 deepanshu27193

First of all I also would consider this a bug as it is really annoying and therefore should be fixed / handled better imho.

However I doubt the Mage Devs see it as bug as they always declare the code parts were they use \Magento\Customer\Model\Session themselves as not cacheable. So it looks like \Magento\Customer\Model\Session is not intended to work with cache. I'm not sure if it is true for all problems with \Magento\Customer\Model\Session but I guess it is for a lot of them.

We came across this problem in a custom block and could "fix" it easily be also declaring it as not cacheable in its layout config XML. So instead of <block class="OurCompany\OurExtension\Block\Name\Index" name="our-block-name" template="OurCompany_OurExtension::blocktemplate.phtml"/>
we needed <block class="OurCompany\OurExtension\Block\Name\Index" name="our-block-name" template="OurCompany_OurExtension::blocktemplate.phtml" cacheable="false"/>
in our XML. Maybe some other problems can be solved in a similar way.

@deepanshu27193 You cannot do this in an easy way, CONTEXT_GROUP and CONTEXT_AUTH are the only attributes provided by \Magento\Customer\Model\Context directly. There may be another way but it would probably be a little bit more complicated.

tehXor avatar Oct 15 '16 23:10 tehXor

@tehXor what another way are you talking about , any suggestion ?

and yes regarding your cacheable suggestion, i have one point, i already implemented this to avoid error but i revert back to normal because after that we experience page speed issue.

deepanshu27193 avatar Oct 18 '16 11:10 deepanshu27193

I have tried like this and got the customer session object when the cache is enabled.

<?php

namespace Test\Stack\Model\Test;

use Magento\Framework\ObjectManagerInterface;

/**
 * @var \Magento\Framework\ObjectManagerInterface
 */
protected $_object;

class Test{

     public function __construct(ObjectManagerInterface $interface)
     {
        $this->_object = $interface;
     }

     public function getCustomerSession(){
        $customerSession = $this->_object->create('Magento\Customer\Model\SessionFactory')->create();
        return $customerSession;     
     }
}

saravananvelu avatar Oct 20 '16 05:10 saravananvelu

@tehXor Thanks for the tip on flagging the layout as cachable=false.

That worked like a charm for me!

davidstillson avatar Nov 15 '16 01:11 davidstillson

To all the people here suggesting to mark a block as non-cacheable: keep in mind that one non-cacheable block in a layout will make the entire layout non-cacheable (talking about full page cache here).

The consequence is that the page in question will never be cached by the FPC, and if your non-cacheable block happens to be in a page header/footer that you use on all your pages, then full-page caching will be completely broken for your entire website. That is a big performance price to pay.

pantaoran avatar Dec 23 '16 02:12 pantaoran

@pantaoran have you come up with a solution around this? I haven't been to get anything to work. I almost got using SessionFactory to work, but it kept sending back the same session values even after I called session->setdata with new values.

JstrzAtW2M avatar Dec 23 '16 03:12 JstrzAtW2M

@jstrez77 I'm not sure what exactly you're trying to solve. What I described in my last comment is exactly what happened to me until I realized how much worse I made it. I'm now trying to fix it, and it seems that the solution is to load user-specific data through javascript instead of php, this way the server page can still be cached and the client will still see relevant info for their account.

First I though that would have to be AJAX, but actually Magento already loads a bunch of data that you can just access without further calls to the server. Look at the example at https://github.com/magento/magento2/blob/develop/app/code/Magento/Theme/view/frontend/templates/html/header.phtml

Also, what helped me a lot in understanding all this is: https://inviqa.com/blog/how-full-page-cache-works-magento-2

pantaoran avatar Dec 26 '16 07:12 pantaoran

See customerSessionFactory for fix above- it doesn't contain full customer data but the ID is there - you can. manually l load customer object after grabbing the ID!

southerncomputer avatar Aug 11 '17 10:08 southerncomputer

I noticed that having all types of cache except Full page cache getting data from session works normally. So I guess the problem is in Full page cache.

This is NOT a problem in full page cache. It is supposed to work this way saving all necessary data in HttpContext so that when you serving cached page there is even no need to start PHP session.

See http://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/public-content.html#configure-page-variations for details.

@Nuranto, http://devdocs.magento.com/guides/v2.1/extension-dev-guide/cache/page-caching/private-content.html explains how to pass customer-specific data properly.

orlangur avatar Sep 11 '17 08:09 orlangur

@orlangur What have we to do to retrieve customer's email for logged user with FPC enabled? It is absurd that there is no easy way to do so. Have we to follow all these steps http://devdocs.magento.com/guides/v2.1/extension-dev-guide/cache/page-caching/private-content.html?

lucscu avatar Sep 12 '17 10:09 lucscu