First You need to create a object instance:


$om = \Magento\Framework\App\ObjectManager::getInstance();

After That:

<?php
    $customerSession = $om->get('Magento\Customer\Model\Session');
    if($customerSession->isLoggedIn()) {
        echo   $customerSession->getCustomer()->getId()."<br/>";  // get Customer Id
        echo   $customerSession->getCustomer()->getName()."<br/>";  // get  Full Name
        echo   $customerSession->getCustomer()->getEmail()."<br/>"; // get Email Name
        echo   $customerSession->getCustomer()->getGroupId()."<br/>";  // get Customer Group Id
      }  
?>

Categories: Magento2

9 Comments

Jonas · June 1, 2016 at 11:33 pm

Hi

Can you explain how can I get the group name form the group id?

Thanks
Jonas

Ratnesh · July 2, 2016 at 2:57 pm

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create(‘Magento\Customer\Model\Session’);

if ($customerSession->isLoggedIn()) {
}

Ratnesh · July 2, 2016 at 2:58 pm

Please replace get to create

PRAKASH · September 1, 2016 at 5:51 pm

This not working in home page.

Santosh Sharma · December 7, 2016 at 2:35 pm

$om = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $om->create(‘Magento\Customer\Model\Session’);
if ($customerSession->isLoggedIn()) {
echo $customid = $customerSession->getCustomer()->getId();
echo $customgp = $customerSession->getCustomer()->getGroupId();
}

its will show on all pages

Arles · January 11, 2017 at 9:05 pm

Thanks for the info!

Jeff Monschke · February 3, 2017 at 2:38 am

Using the object manager directly is not the recommended way to create objects in Magento 2, as you should be utilizing the dependency injection capabilities that are baked in.

Leave a Reply to Santosh Sharma Cancel reply

Avatar placeholder

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