Magento 2: Get Current Customer

If you want to tailor your customer's experience to their needs you have to gather as much information about them as possible. Displaying unique pricing details or offering different shipping services based on customer groups required you to get current customer data in Magento. 

Besides, customer age, gender and location are valuable marketing insights you can use to tweak your strategy.

Since you already know how to get current product and current category to customize them, in this guide you'll learn everything about getting logged in customers details.

Get Current Customer in Magento 2 

You can get current customer in Magento via \Magento\Customer\Model\Session model using two methods.

Using Construct

<?php

namespace VendorName\ModuleName\Folder;

class Example
{
private $customerSession;

public function __construct(
\Magento\Customer\Model\Session $customerSession
) {
$this->customerSession = $customerSession;
}

public function getCurrentCustomer()
{
return $this->customerSession->getCustomer();
}
}
?>

Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get(\Magento\Customer\Model\Session::class);
$customer = $customerSession->getCustomer();

Note: you should use the ObjectManager carefully, since though it is the easy way, it is not the best. 

Get Current Customer ID in Magento

<?php
$customerId = $this->customerSession->getCustomerId();
?>

Get Current Customer Group ID in Magento 2

<?php
$customerGroupId = $this->customerSession->getCustomerGroupId();
?>