2

I want to get customer_id, name and email of logged in customer in product view. How to do it?

Mohit Kumar Arora
  • 9,951
  • 7
  • 27
  • 55
xanka
  • 2,114
  • 6
  • 33
  • 65

1 Answers1

2

You can get it directly from customer session:

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

if ($customerSession->isLoggedIn()) { $customerSession->getCustomerId(); $customerSession->getCustomerGroupId(); $customerSession->getCustomer(); $customerSession->getCustomerData(); $customerSession->getCustomer()->getEmail(); }

davideghz
  • 683
  • 2
  • 11
  • 27
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
  • 4
    You should never use \Magento\Framework\App\ObjectManager::getInstance(), instead inject Magento\Customer\Model\Session as a dependency to the construct. – Alex Paliarush Dec 07 '15 at 09:54
  • have you check this: http://foggyline.net/magento-2-get-current-logged-in-customer/ – Amit Bera Dec 07 '15 at 09:55