We have tried to get customer id in post https://sbdevblog.com/magento-2-how-to-get-the-current-customer-id/ . However, we are not able to get customer details like customer id, Log-in Status of visitor because we are getting NULL value each time we try to get customer details from session while FPC (Full Page cache) is enabled.
In any case, you can utilize \Magento\Framework\App\Http\Context all things being equal on the off chance that you don’t require more than customer_group and customer_not_logged_in values.
Create di.xml in order to define plugin.
Create di.xml in SbDevBlog/Customer/etc/Frontend directory
<?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="sbdevblog-defined-customer-details-httpcontext" type="SbDevBlog\Customer\Plugin\CustomerSessionContext" />
</type>
</config>
Create Plugin as defined in di.xml
Create Plugin inside SbDevBlog/Customer/Plugin Directory
<?php
/**
* @copyright Copyright (c) sbdevblog (https://www.sbdevblog.com)
*/
namespace SbDevBlog\Customer\Plugin;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Http\Context;
class CustomerSessionContext
{
/**
* @var Session
*/
protected Session $customerSession;
/**
* @var Context
*/
protected Context $httpContext;
/**
* Constructor
*
* @param Session $customerSession
* @param Context $httpContext
*/
public function __construct(
Session $customerSession,
Context $httpContext
) {
$this->customerSession = $customerSession;
$this->httpContext = $httpContext;
}
/**
* Set Customer Session while caching
*
* @param \Magento\Framework\App\ActionInterface $subject
* @param \Closure $proceed
* @param \Magento\Framework\App\RequestInterface $request
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
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_name',
$this->customerSession->getCustomer()->getName(),
false
);
$this->httpContext->setValue(
'customer_email',
$this->customerSession->getCustomer()->getEmail(),
false
);
return $proceed($request);
}
}
Create Preferred Class like Helper, Block Or Controller. In this case, I have created Service Class.
Create Service Class CurrentCustomerOnCache inside SbDevBlog/Customer/Services/
<?php
/**
* @copyright Copyright (c) sbdevblog https://www.sbdevblog.com)
*/
namespace SbDevBlog\Customer\Services;
use Magento\Framework\App\Http\Context as HttpContext;
class CurrentCustomerOnCache
{
/**
* @var HttpContext
*/
protected HttpContext $httpContext;
public function __construct(
HttpContext $httpContext
){
$this->httpContext = $httpContext;
}
/**
* @return int|null
*/
public function getCustomerId()
{
return $this->httpContext->getValue("customer_id");
}
/**
* @return string|null
*/
public function getCustomerEmail()
{
return $this->httpContext->getValue("customer_email");
}
/**
* @return string|null
*/
public function getCustomerName()
{
return $this->httpContext->getValue("customer_name");
}
}
Note: Please verify the code of this blog and the relevant git repository before using it in production.
🙂 HAPPY CODING 🙂
Hello, just wanted to say, I loved this article.
It was practical. Keep on posting!
Thank You Cheapest Book Store,
Thanks for your appreciation.
Please subscribe for post notifications 🙂