How to set customer data in httpContext and retrieve it in Magento 2.
When the page cache is enabled in Magento 2, it can be challenging to access customer data from the session and only the customer group can be obtained from httpContext. This poses a challenge for developers who need to customize certain functionalities for specific customers and require customer data such as customer ID. To address this issue, we have prepared an article that will guide you on how to set customer data in httpContext and retrieve it in Magento 2.
In order to archive this, we need to take the following steps:
- Define the plugin on the di.xml file.
- Create the plugin and set the customer ID on httpContext.
- Create the helper file and retrieve the customer ID from the httpContext to use it as needed.
Let's get started !!
Step 1: Create a di.xml
file in a module at app/code/Devhooks/Sample/etc/frontend/
and paste below code inside the file.
saveCopyzoom_out_map<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\ActionInterface">
<plugin name="Devhooks_Sample_Plugin_Magento_Framework_App_ActionInterface"
type="Devhooks\Sample\Plugin\CustomerDataContext"/>
</type>
</config>
Now we'll create the plugin file name with CustomerDataContext.php
and learn how to set the customer id on httpContext.
Step 2: Create a CustomerDataContext.php
file in a module at app/code/Devhooks/Sample/Plugin/
and paste below code inside the file.
saveCopyzoom_out_map<?php
namespace Devhooks\Sample\Plugin;
use Magento\Framework\App\ActionInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Http\Context;
class CustomerDataContext
{
/**
* @var Session
*/
private Session $customerSession;
/**
* @var Context
*/
private Context $httpContext;
/**
* Constructor
*
* @param Session $customerSession
* @param Context $httpContext
*/
public function __construct(
Session $customerSession,
Context $httpContext
) {
$this->customerSession = $customerSession;
$this->httpContext = $httpContext;
}
/**
* @param ActionInterface $subject
*/
public function beforeExecute(ActionInterface $subject)
{
$this->httpContext->setValue('customer_id', $this->customerSession->getCustomerId(), false);
}
}
In the above code, we have set the login customer_id
in the httpContext in the same way that Magento 2 did for the customer_group
. As a result, we can now access the customer_id
in the block/helper. To make things easier for future use, we will create a method in the helpr file that can be reused for multiple purposes.
Step 3: Create a Data.php
file in a module at app/code/Devhooks/Sample/Helper/
and paste below code inside the file.
saveCopyzoom_out_map<?php
namespace Devhooks\Sample\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Http\Context as HttpContext;
class Data extends AbstractHelper
{
protected $httpContext;
/**
* Data constructor.
* @param Context $context
* @param HttpContext $httpContext
*/
public function __construct(
Context $context,
HttpContext $httpContext
) {
$this->httpContext = $httpContext;
parent::__construct($context);
}
public function getIsCustomerLoggedIn()
{
return (bool)$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}
public function getCustomerId()
{
return $this->httpContext->getValue('customer_id');
}
}
In the code provided, two methods have been created: getIsCustomerLoggedIn
and getCustomerId
.
To clarify, the customer_logged_in
boolean value is retrieved by default from httpContext and then utilized in the getIsCustomerLoggedIn
method.
In addition, the recently added customer_id
is obtained through the getCustomerId
method using httpContext and then returned.
By using the above helper method, you can now obtain the customer_id
anywhere in the frontend.
Now, open Command line in folder root of magento and run the below commands.
saveCopyzoom_out_mapphp bin/magento s:up; php bin/magento s:s:d -f; php bin/magento c:c;
That's it!
Similarly, you have the ability to set personalized customer information on httpContext and access it from any location within the frontend.
We hope this Magento article helped you to know How to set customer data in httpContext and retrieve it in Magento 2.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this article with your team.
Review other articles maybe it'll help you too.
- How to Add Image Uploader on Store Configuration in Magento 2
- How to Add Custom Select/MultiSelect Options on Store Configuration in Magento 2
- How to Add Dynamic Row on Store Configuration in Magento 2
- How to Add Date & time Picker on Store Configuration in Magento 2
- How to Add Link on Store Configuration Fields Comment in Magento 2
- How to Use Javascript Mixins in Magento 2
- How to Add Custom Validation Rule in Magento 2
- How to Get Product Collection
- How to Setup Magento 2 PWA Studio
- How to Get Current Category
- How to Get Current Product
- Get Product Collection with Filters
- How to Create And Use Plugin In Magento 2