Skip to content

Instantly share code, notes, and snippets.

@petemcw
Created July 13, 2018 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petemcw/22da14fa7c7c7e012d22b87068b9a36e to your computer and use it in GitHub Desktop.
Save petemcw/22da14fa7c7c7e012d22b87068b9a36e to your computer and use it in GitHub Desktop.
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