Magento 2 Get Customer Logged Session

Magento 2 Get Customer Logged Session

Today I have discussed you about Magento 2 Customer login session code. In magento theme development or module development, we need to get customer session on customer related extension. below is code that help you to get customer session in phtml file or block/controller file.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get(‘Magento\Customer\Model\Session’);
?>
<?php if($customerSession->isLoggedIn()):?>
<a href=”<?php echo $block->getUrl(‘customer/account’); ?>”><?php echo __(‘My Account’); ?></a>
<?php else:?>
<div>
<a href=”<?php echo $block->getUrl(‘customer/account/login’); ?>”><?php echo __(‘Login’); ?></a>
<span class=”pipe”>|</span>
<a href=”<?php echo $block->getUrl(‘customer/account/create’); ?>”><?php echo __(‘Register’); ?></a>
</div>
<?php endif;?>

 

Hope this code may be helpfully for you thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *