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

  • author-img Nidhi Arora
  • 5 years
  • 517 views
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.

Download Blog

ENQUIRY

Ready to Get started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    1250 Pittsford Victor Road Ste 310
    Pittsford, NY 14534

    Development Center

    Plot no. 10, Rajiv Gandhi
    Chandigarh Technology Park