Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Magento 2.x Is Customer Logged In?
<?php
namespace Petemcw;
use Magento\Customer\Model\Session;
class Customer
{
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* Constructor.
*
* Initialize class dependencies.
*
* @param \Magento\Customer\Model\Session $customerSession
*/
public function __construct(
Session $customerSession
) {
$this->customerSession = $customerSession;
}
/**
* Returns current customer's logged in status.
*
* @return bool
*/
public function isCustomerLoggedIn()
{
return $this->customerSession->isLoggedIn();
}
/**
* Returns an array of current customer information.
*
* return array
*/
public function getCurrentCustomerData()
{
return [
'id' => $this->customerSession->getCustomer()->getId(),
'name' => $this->customerSession->getCustomer()->getName(),
'email' => $this->customerSession->getCustomer()->getEmail(),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.