0

I am trying to save some temporary data into the customer session object ( \Magento\Customer\Model\Session ), which I need to retain until a quote is generated. I have tried two methods to save data in session, $session->setData('Att',$value); and $session->setAttValue($value); on a temporary bases. These getter and setter methods reside in a class which I created to handle session related operations and are called from observers.

The problem is : Once I store the value in the session and try to get it back, I am getting NULL value when Full Page Cache is enabled; however if the FPC is not enabled, the accessor method returns expected value from session.

PS: I found an issue which might be similar to what I am facing here, but in the issue, the solution suggested was hole-punching the block, but in my situation, I am not using any block, I am using observers only and I could not find a way to hole-punch function for observer. Any help appreciated.

Thanks in Advance,

2 Answers 2

0

Use Session factory instead.

public function __construct(
       \Magento\Customer\Model\SessionFactory $sessionFactory)
        {
           $this->sessionFactory = $sessionFactory;
        }

Now you can use customer session with FPC enabled.

$customerSession = $this->sessionFactory->create();

if ($customerSession->isLoggedIn()) {
    // customer is logged in;
}
1
  • 1
    Can you tell me why this would work with FPC and not session object dependency injection not? This will create a new session whenever the observer is called overwriting information already stored in previous session. Sep 11, 2017 at 13:27
0

I believe your issue is coming from Magento's private/public content and the depersonalization that happens on cacheable pages.

Here is an Atwix article that goes into some detail about the depersonalization that happens:

Atwix - Session Depersonalization

When FPC is enabled and a customer lands on a fully cacheable page, Magento clears/wipes the customer's session data (private content) to prevent any of their information from being outputted and cached publicly on the page. If this were to happen other users visiting that cached page could potentially see the original customer's information (first name, last name, email, reward points, etc...).

I think that even though you might be doing this in observers, the customer is still eventually going to land on a cached page and wipe out the data you've set.

The Magento dev docs go into detail of how to store private content client side for the customer and the mechanisms for invalidation the private content and refreshing it (customer actions):

Magento Dev Docs - Private Content

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.