cancel
Showing results for 
Search instead for 
Did you mean: 

Show logged in customer name on front page and in catalog

Show logged in customer name on front page and in catalog

I tried showing the name and number of the currently logged in customer on the frontpage (and in catalog view) with code like:

...

$customerSession = $objectManager->create('Magento\Customer\Model\Session');

...

echo $customerSession->getCustomer()->getName();

 

But with FPC enabled I got random names! After digging a bit deeper - if i understand correctly - it is like that: Certain pages (the "cacheable" ones like Category, Product view pages, CMS pages) get depersonalized in the backend before delivering, stripping all personal infos for performance gains. And in such pages, my code simply won't work.

 

The simple but ugly solution would be to turn off FPC.

Then there should be a way to solve this with "private content" ...


Question: Did anyone solve this problem and has code to share with me, how this works. Maybe as a nice little extension? Or does anyone know of a extension to achieve this?

3 REPLIES 3

Re: Show logged in customer name on front page and in catalog

Re: Show logged in customer name on front page and in catalog

No sorry, thats what I already tried.

 

$customerSession = $objectManager->create('Magento\Customer\Model\Session');

This call will give kind of "random" session on depersonalized pages. For example, calling getCustomer>getName on this session gave me the name of the latest login, could be the right one, was most times a wrong one (I could swap displayed customer name by refreshing the page)

This works perfectly it I disable FPC .. but this cant be the solution!

 

The problem explanation in more depth can be found here:

https://magento.stackexchange.com/questions/145851/customer-session-does-not-work-except-customer-pa...

 

regards

Re: Show logged in customer name on front page and in catalog

Hello @itcenter_at 

I think you need to implement hole punching through which you can show private content on FPC enabled pages.

You can also check how welcome message is implemented in header. Please refer the link https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching/private-content.html

https://stackoverflow.com/questions/9120413/how-do-i-include-a-dynamic-block-in-the-product-page-wit...

You can also check this awesome content:

https://www.slideshare.net/MeetMagentoIt/vinai-kopp-fpc-hole-punching-in-magento-2

I hope it will help you.

If it helps you, please accept it as solution and give kudos.

Regards.