0

I am working on a Magento 2.4.1 extension that has some special requests where I need your advice on how to handle some technical aspects in a better way.

Basically, when a user logs in or a visitor visits the store for the first time, I need to set up some session parameters that will be injected into the product page. These variables come from the admin area and are updated every hour (into the session). For example, if I just accessed the store, I got my variables in the customer session. If the admin does some changes in the backend area around the same time, it will update my variables into the session in ~1 hour.

The problem I am facing is related to FPC because if I try to print the session on the product page, it won't show those variables. However, if I will print the session in a different controller, it will show those variables. I read some topics and articles that mentioned this particular situation is strongly connected to FPC and Private Content, but I would love to see your thoughts on this situation and on my two following questions:

  1. Following my description, do you think it's a good idea for me to keep storing those variables in the customer's session for both logged-in customers and visitors or do you have a better idea (ex. using cookies)? Please consider also that some variables are strongly related to different customer-related variables such as customer group, country, store view, etc.
  2. If it will be to continue using the customer's session, how can I treat these special customer session variables as private/non-cacheable so that I can access them when I want without having them removed from the session due to FPC/Private Content, even if the product page is cached?

Looking forward to hearing back from you.

Thanks!

1 Answer 1

0

Please Try Below Code For FPC

protected $httpContext;
protected $customerSession;
public function __construct(
    ...
    \Magento\Framework\App\Http\Context $httpContext,
    \Magento\Customer\Model\Session $customerSession,
    ...
) {
    $this->httpContext = $httpContext;
    $this->customerSession = $customerSession;
    
}
public function excute(){
    
    $this->httpContext->setValue(
        'customer_id',
        $this->customerSession->getCustomerId(),
        false
    );
}
6
  • Thank you! It is working, but the values are constantly updated on each page load. That's my main problem now because I need to follow this scenario: a customer logs in and I immediately set up some context variables. Among them, there is a variable that shows when those variables were set up (eg. 5:30 PM). On each page load, I need to check if more than 60 minutes have elapsed. If TRUE, then re-set the variables with a new set time, if FALSE, keep them as they were added. Right now, it constantly updates my variables including the set time. Is this how context works?
    – iupaem
    Feb 12, 2022 at 17:00
  • There are no. of events that can help you to call frontend page load. Event name: controller_action_layout_render_before File: vendor/magento/framework/App/View.php Event name: controller_front_send_response_before File: vendor/magento/framework/App/Http.php Event name: controller_action_layout_render_before_ File: vendor/magento/framework/App/View.php Feb 12, 2022 at 17:43
  • I am already using "controller_action_predispatch" because I need to run that on every page load. However, I don't know why values are constantly updating. I set up the variable now, reload the page and the variable is there. If I am putting the code block that sets up the variable between comments and reloading the page, I won't see the variable anymore. Is the HTTP Context cleared on every page load exactly as customer session variables in order to prevent caching private content?
    – iupaem
    Feb 12, 2022 at 18:37
  • which place set context variables? Feb 12, 2022 at 18:55
  • I need to set up context variables in different moments such as: customer login, customer logout, store view switch, and when more than 60 minutes have elapsed from the last time I set up those variables. Right now, my main problem is that the variables are set up on each controller load because the setup time is constantly updating. When I used the customer session for this, it worked.
    – iupaem
    Feb 13, 2022 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.