Magento – Fullpage-Caching and Magento\Customer\Model\Session in Magento 2

customerfull-page-cachemagento2session

I would like to add a Block with different content when user is logged in or not. Content depends not on customer-data – just logged-in state.

class Grouped extends \Magento\GroupedProduct\Block\Product\View\Type\Grouped
{
    protected $customerSession;


    public function __construct(
       \Magento\Catalog\Block\Product\Context $context,
       \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
       array $data,
       \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->_isScopePrivate = true;
        parent::__construct($context, $arrayUtils, $data);
        $this->customerSession = $customerSession;
    }

    public function isLoggedIn()
    {
        return $this->customerSession->isLoggedIn();
    }

}

In my phtml-File a just call $this->isLoggedIn() but it is always "false" when fullpage-cache is enabled.

How I could get information about customer-logged-in-state when fullpage cache is enabled on product-view page? How I prevent it from caching or adding two different caching-tags? (not-logged-in or logged-in) I believed in $this->_isScopePrivate but doesn't help..

Pricing, which depends on customer-group, works well on product-view page but don't find any informations why.

Best Answer

Sorry, answering myself is a little bit ridiculous.

getChackeKeyInfo is only relevant for block-caching, fullpage-caching looks different :(

In case of depending on Customer\Session isLoggedIn is always "false". Same when cache is clean before calling product-view.

But

Instead of depending on Customer\Session I copied some code auf Review Module. It depends on \Magento\Framework\App\Http\Context $httpContext.

In this case I'm able to distinguish logged in or not by calling

$httpContent->getValue(Context::CONTEXT_AUTH);

Would be great to get some explanation why :)

Related Topic