3

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.

4

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

|improve this answer|||||
  • just curious: is a cache key change not working at all with fpc? Was hoping for some automatic hole punching, but did not look into this yet – David Verholen Mar 31 '16 at 16:53
  • far shot on your question: I assume the http context considers a cookie set if the user is logged in – David Verholen Mar 31 '16 at 17:00
  • cool decision. helped me alot – Anna Mar 3 '17 at 15:48
0

the isScopePrivate switch sounds reasonable at first, but it seems not to be considered in the cache key generation: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/View/Element/AbstractBlock.php#L967

adding the customer group to the cache key by overriding the getCacheKeyInfo method should do the trick in your case: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/View/Element/AbstractBlock.php#L957

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.