Created
July 13, 2018 15:03
-
-
Save petemcw/22da14fa7c7c7e012d22b87068b9a36e to your computer and use it in GitHub Desktop.
Magento 2.x Is Customer Logged In?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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