How to Check if Customer is Logged in or Not
<?php
namespace Vendor\Extension\Helper;
class Data
{
public function __construct(
\Magento\Framework\App\Http\Context $httpContext
) {
$this->httpContext = $httpContext;
}
public function getLogin() {
return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}
?>
------------------------------------------------------------------------------------
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
try
{
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userContext = $objectManager->get('Magento\Framework\App\Http\Context');
$loggedin = $userContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($loggedin) {
echo "Customer is Logged-In";
}
}
catch(\Exception $e)
{
print_r($e->getMessage());
}
Comments
Post a Comment