0

I use this code, on .phtml file,

if($customerSession->isLoggedIn()) {}

for check if the user is logged. But, when I active the cache, this code does not work fine. How I can resolve this problem?

Sumit
  • 4,875
  • 2
  • 19
  • 35
Infocurci
  • 85
  • 8

1 Answers1

0

Utilising knockout is another approach

<div class="greet welcome" data-bind="scope: 'customer'">
    <!-- ko if: customer().is_logged_in  -->
    <span class="logged-in"
            data-bind="text: new String('<?= $block->escapeHtml(__('Welcome back %1', '%1')) ?>').replace('%1', customer().firstname)">
    </span>
    <!-- /ko -->
    <!-- ko ifnot: customer().is_logged_in  -->
    <span class="not-logged-in"
            data-bind='html:"<?= $block->escapeHtml(__("Please <a href='%1'>login</a>", $block->getUrl('customer/account/login'))) ?>"'>
    </span>
    <!-- /ko -->
</div>

https://github.com/DominicWatts/CustomerLogin

Dominic Pixie
  • 7,520
  • 4
  • 17
  • 56