0

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?

https://aureatelabs.com/magento-2/how-to-get-customer-session-data-when-a-cache-is-enabled-in-magento-2/

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

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/public-content.html#configure-page-variations

So please help me for the same.

1 Answer 1

1

Take a look at this

https://github.com/DominicWatts/CustomerLogin

It's the plugin

https://github.com/DominicWatts/CustomerLogin/blob/357bf798252c32517307c2775572c332652f510f/Plugin/Magento/Customer/CustomerData/Customer.php#L43

    $result['is_logged_in'] = $this->customerSession->isLoggedIn();

https://github.com/DominicWatts/CustomerLogin/blob/357bf798252c32517307c2775572c332652f510f/view/frontend/templates/index/index.phtml#L1-L11

<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

4
  • thanks a ton...it's working for me. Jun 9, 2020 at 9:44
  • For the Mega Menu Section:- i am showing some "static menu" in the Mega Menu section through phtml for logged-in customer only...but for that i have to include your '<div class="greet welcome" data-bind="scope: 'customer'">' before my menu <li> due to that design get scattered...so is there any way to avoid using this div scope ? if m not using this div then your code doesn't work. Jun 9, 2020 at 9:48
  • You can use it on other elements. But beyond that you might have to have two versions the menu maybe Jun 9, 2020 at 15:31
  • Okay, i got that element must be there and i have to adjust it some where else..thankyou so much. Jun 10, 2020 at 4:08

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.