Customer session isLoggedIn() not working properly due to the Magento Cache
-
14-04-2021 - |
Question
I am checking if customer is logged in or not(also i have to display customer name) but sometimes it's not working and after flushing block_html cache it works.
I have tried all possible solutions:-
Tried following but still getting issue:-
Why Customer Session is not working on full page cache?
As i R&D and found throgh AJAX technique it can be resolved but not any idea of the same...here are the references:-
https://belvg.com/blog/customer-data-management-in-magento-2.html
So please help me for the same.
Solution
Take a look at this
https://github.com/DominicWatts/CustomerLogin
It's the plugin
$result['is_logged_in'] = $this->customerSession->isLoggedIn();
<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 -->
Take a look at the how I've used the customer binding and knockout
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange