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

Cannot get customer session data #3294

Closed
Nuranto opened this issue Feb 5, 2016 · 30 comments

Comments

@Nuranto
Copy link

commented Feb 5, 2016

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 :()

@sevos1984

This comment has been minimized.

Copy link
Contributor

commented Feb 15, 2016

Internal ticket ID MAGETWO-49202

@quienti

This comment has been minimized.

Copy link
Contributor

commented May 4, 2016

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;
@SergeiKutanov

This comment has been minimized.

Copy link

commented May 26, 2016

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.

@deepanshu27193

This comment has been minimized.

Copy link

commented Jun 22, 2016

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.

@sengaigibon

This comment has been minimized.

Copy link

commented Jul 29, 2016

Related to #3572

@vkorotun vkorotun removed the CS label Aug 4, 2016
@zzpaul

This comment has been minimized.

Copy link

commented Aug 12, 2016

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

@saravananvelu

This comment has been minimized.

Copy link
Contributor

commented Sep 15, 2016

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

@bartoszkubicki

This comment has been minimized.

Copy link
Contributor

commented Sep 15, 2016

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:
#6392

@KalaiarasanSeetharaman

This comment has been minimized.

Copy link

commented Oct 4, 2016

$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

@deepanshu27193

This comment has been minimized.

Copy link

commented Oct 5, 2016

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

@tehXor

This comment has been minimized.

Copy link

commented Oct 15, 2016

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.

@deepanshu27193

This comment has been minimized.

Copy link

commented Oct 18, 2016

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

@saravananvelu

This comment has been minimized.

Copy link
Contributor

commented Oct 20, 2016

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

This comment has been minimized.

Copy link

commented Nov 15, 2016

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

That worked like a charm for me!

@pantaoran

This comment has been minimized.

Copy link

commented Dec 23, 2016

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.

@JstrzAtW2M

This comment has been minimized.

Copy link

commented Dec 23, 2016

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

@pantaoran

This comment has been minimized.

Copy link

commented Dec 26, 2016

@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

@southerncomputer

This comment has been minimized.

Copy link
Contributor

commented Aug 11, 2017

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!

@orlangur

This comment has been minimized.

Copy link
Contributor

commented Sep 11, 2017

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.

@LucScu

This comment has been minimized.

Copy link

commented Sep 12, 2017

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

@orlangur

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2017

What have we to do to retrieve customer's email for logged user with FPC enabled?

Please use Magento Stack Exchange for questions. You can do it similar to "Welcome, %USERNAME%!" block.

It is absurd that ...

Feel free to suggest a better implementation.

@korostii

This comment has been minimized.

Copy link

commented Sep 12, 2017

Feel free to suggest a better implementation.

The $_isScopePrivate approach was perfect from extension developer point of view, but I believe it's now considered deprecated and discouraged by the core developers (not sure about the motivation for this decision, might be exactly because of some issues with cache) - it's even mentioned in the docs.

IMHO developer shouldn't need to go through that amount of effort in order to work around the caches. I wonder if maybe a way to fix\improve that $_isScopePrivate feature somehow is the way to go.

The most awkward part is that when you take glance into the core, looking for a working example and all you see are these $_isScopePrivate and cacheable=false. Not sure if that changed in 2.2.

@orlangur

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2017

@korostii passing pieces of HTML via AJAX in 2k17, really? :)

The $_isScopePrivate is flawed by design (one small mistake and your customer private data is cached for everyone). Too bad this M1-style FPC mechanism was not eliminated at all (just because it requires a lot of refactoring).

and cacheable=false. Not sure if that changed in 2.2.

Didn't check 2.2 yet as well. For the 2.0 GA there was a separate effort to make all product/category/CMS pages not using the obsolete FPC mechanism. Some pages, like My Account, do not really suffer from being not cacheable.

Main problem is that developers still think in terms of generating some HTML thus getting an unexpected system behavior. I do agree that CustomerData mechanism could be a bit easier to use, hiding all unneeded implementation details under the hood.

@LucScu

This comment has been minimized.

Copy link

commented Sep 12, 2017

@orlangur
I try to find a way by myself and after find it i will reply here (for anyone have some hints) https://magento.stackexchange.com/questions/152010/how-to-get-customer-email-from-javascript?noredirect=1#comment266673_152010

@orlangur

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2017

@LucScu great! Let's use different services as intended.

@korostii

This comment has been minimized.

Copy link

commented Sep 12, 2017

passing pieces of HTML via AJAX in 2k17, really? :)

Well, Magento2 still has Zend framework 1 and prototype.js inside. In 2017. What's your point? :D

Too bad this M1-style FPC mechanism was not eliminated at all (just because it requires a lot of refactoring).

Well how would you expect extension and integrator developers to care about it, if it's not even done for the core yet? Looks like double standards to me.

I really hope the blocks cache\tags\variations\local storage system becomes the center of Magento 2's frontend fireworks. But I just don't see right now how could the outside developers transition to it en masse while it's still such a work in progress. Maybe, some other day :)

@orlangur

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2017

Magento2 still has Zend framework 1 and prototype.js inside

ZF1 is in process of elimination and some reminants of prototype.jsonly left in Admin UI, it would be eliminated if needed just like it was done for storefront (a good one for 'up for grabs', by the way, last time I grepped there were like ~80 occurrences of it in require section of components).

if it's not even done for the core yet?

It was done for crucial pages, sometimes regressions happened like review block on product page, some third-party developers may be unaware and accidentally make such pages uncacheable :( That's why such ajaxified FPC need to be completely eliminated for good.

@kazimnoorani8704

This comment has been minimized.

Copy link

commented Nov 24, 2018

You can try by creating an object of objectManager and should not use objectManager directly.

Use something like,

class Example extends \Magento\Framework\View\Element\Template
{
    private $_objectManager;
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectmanager
    ){
        $this->_objectManager = $objectmanager;
    }

    public function getExample()
    {
        $customerSession = $this->_objectManager->create("Magento\Customer\Model\Session");
        if ($customerSession->isLoggedIn()) {
            $customerData = $customerSession->getCustomer()->getData();
            /*Your logic*/
        }
    }
}
@orlangur

This comment has been minimized.

Copy link
Contributor

commented Nov 24, 2018

@kazimnoorani8704 this is still using ObjectManager and NOT RECOMMENDED as well. Looks how customer session in injected in core classes.

pdohogne-magento pushed a commit that referenced this issue Nov 28, 2018
Fixed issues:
- MAGETWO-92170: Redundant File Names for Quote Attachments
- MAGETWO-90725: Wrong session messages behavior
- MAGETWO-94173: Disable backups by default
@alamzaib

This comment has been minimized.

Copy link

commented Sep 3, 2019

How i got it

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

now $isLoggedIn will give us weather customer is logged in or not
if($isLoggedIn){
// code
}

and then
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
var_dump($customerSession->getCustomer()->getData());

to get customer information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.