How to Get Customer Data from an ID in Magento 2?

  • Posted by Envision Ecommerce
  • /
  • December 2, 2016
HOW TO GET CUSTOMER DATA FROM AN ID IN MAGENTO 2?

If you are a Magento developer and come from Magento 1.x world, you possibly aware about the method to access customer data. However, it is quite different to access customer data from the ID in Magento 2 than Magento 1.x. Here in this blog we have compiled some easy steps to explain how you can get customer details from ID in Magento 2. Read on to learn this simple & effective method:

Step 1: At the beginning of this method, you need to create a block in your custom module as shown below:

namespace Envision\Eecom\Block;
use Magento\Customer\Model\Session;
class CustomerAccount extends \Magento\Framework\View\Element\Template {

    public function __construct(
        Session $customerSession,
        \Magento\Framework\View\Element\Template\Context $context
    )
    {
        parent::__construct($context);
        $this->_customerSession = $customerSession;
    }
    public function getCustomerName(){
        $this->_customerSession->getCustomer()->getName();
    }
}

Step 2: The next step is to utilize the function that is created in your block in any of your .phtml files to access the customer details.

Hope it helps you. If you still need a hand, Envision Ecommerce’s team of certified Magento developers are here to help. Contact us today for any Magento related query.