I want to get customer_id, name and email of logged in customer in product view. How to do it?
Asked
Active
Viewed 2,392 times
2
Mohit Kumar Arora
- 9,951
- 7
- 27
- 55
xanka
- 2,114
- 6
- 33
- 65
-
3Possible duplicate of How to check if customer is logged in or not in magento 2? – Alex Paliarush Dec 07 '15 at 09:58
1 Answers
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(); }
-
4You should never use
\Magento\Framework\App\ObjectManager::getInstance(), instead injectMagento\Customer\Model\Sessionas 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