3

Magento 2 header custom block not cache-able for customer session for example

<referenceContainer name="header-wrapper">
                 <block class="Company\Genaral\Block\Genaral" name="order.dropdown" before="minicart" template="Magento_Theme::html/custom.phtml" cacheable="false" />
</referenceContainer>

I write code in phtml

<?php $helperUrl = $this->helper('Company\Genaral\Helper\Data'); ?>
<?php $ckcustomer = $helperUrl->getcustomerSession(); ?>
 <?php if($ckcustomer !=1): ?>
   <li class="item link-login">
                <a href="<?php /* @escapeNotVerified */ echo $helperUrl->getAccountLoginUrl() ?>">
                  <div>
                      <i class="rb-icon rb-icon-personal-information"></i>
                      <span><?php /* @escapeNotVerified */ echo __('Login'); ?></span>
                  </div>
                </a>
                </li>
<?php else: ?>
<li class="item link-logout"><a href="<?php /* @escapeNotVerified */ echo $helperUrl->getLogoutUrl() ?>"><?php /* @escapeNotVerified */ echo __('Logout'); ?></a></li>
 <?php endif; ?>

Anyone know why it's not working.

3
  • Did you find any solution for this? Jan 10, 2017 at 11:24
  • yes i found solution.
    – Pratik
    Jan 11, 2017 at 5:02
  • Please post it as answer Jan 11, 2017 at 5:03

1 Answer 1

2

In Your Helper

    protected $isCustomerLoggedIn;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->isCustomerLoggedIn = $httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        parent::__construct($context);
        $this->_isScopePrivate = true;
    }

    public function getcustomerSession()
    {
        return $this->isCustomerLoggedIn;
    }

In Phtml

<?php $helperUrl = $this->helper('Company\Genaral\Helper\Data'); ?>
<?php $ckcustomer = $helperUrl->getcustomerSession(); ?>
<?php if ($ckcustomer == 1): ?> 

<?php endif; ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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