Skip to content

Instantly share code, notes, and snippets.

@petemcw
Created July 13, 2018 15:03

Revisions

  1. petemcw created this gist Jul 13, 2018.
    50 changes: 50 additions & 0 deletions CustomerSessionCheck.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    <?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(),
    ];
    }
    }