In this blog, we will learn how to get logged customer data using httpContext when cache is enabled in Magento 2.
If the cache is enabled then you can’t get customer data from the session. I request you please look at my previous blog about How to check customer is logged in or not in Magento 2, so you will get a better idea in the current post.
As per our previous blog, you can check a customer is logged in or not using httpContext. But you can’t get customer id, name, email, phone, and other customer attributes because it’s not defined in httpContext. For info, httpContext defined only customer_group and customer_not_logged_in by default.
If you want to get other customer attributes that are not defined in httpContext then we need to add custom development.
Please follow the below steps to defined other customer attributes and used them as per your need.
- Define plugin
- Create a plugin as you defined in a di.xml
- Use httpContext data in block/helper
Step – 1: Define Plugin
Please create a di.xml file in your custom module at path app/code/Mageclues/CustomerData/etc/frontend folder.
<?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\Action\AbstractAction">
<plugin name="mageclues-customer-data-defined-to-context" type="Mageclues\CustomerData\Plugin\CustomerDataContext" />
</type>
</config>
Step – 2: Create a plugin class
Please create CustomerDataContext.php file in app/code/Mageclues/CustomerData/Plugin folder.
<?php
namespace Mageclues\CustomerData\Plugin;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Http\Context;
class CustomerDataContext
{
protected $_customerSession;
protected $_httpContext;
/**
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\App\Http\Context $httpContext
*/
public function __construct(
Session $customerSession,
Context $httpContext
) {
$this->_customerSession= $customerSession;
$this->_httpContext = $httpContext;
}
public function aroundDispatch(
\Magento\Framework\App\ActionInterface $subject,
\Closure $proceed,
\Magento\Framework\App\RequestInterface $request
) {
$this->_httpContext->setValue(
'customer_id',
$this->_customerSession->getCustomerId(),
false
);
$this->_httpContext->setValue(
'customer_email',
$this->_customerSession->getCustomer()->getEmail(),
false
);
return $proceed($request);
}
}
As you have seen, we have to get customer attribute data like customer_id and customer_email from customer session and set it in httpContext using the around the plugin. You can set other customer attributes in httpContext in the same way.
Step – 3: Use httpContext data by Block/Helper
Please create helper and inject \Magento\Framework\App\Http\Context
<?php
namespace Mageclues\CustomerData\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class CustomerHelper extends AbstractHelper
{
protected $_httpContext;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Http\Context $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');
}
public function getCustomerEmail()
{
return $this->_httpContext->getValue('customer_email');
}
}
Now, you can get customer_id and customer_email from the helper function in the frontend.
I hope this blog is useful for you. Please leave a comment for query and review.
Thank you!