in this blog, we are going to how to get customer data by id using API repository and Factory Method
There are two ways to get customer data that you can use. You can also get its product data through customer data.
⇒ Using API Repository to Get Customer Data:
1 2 3 4 5 6 7 8 9 10 11 12 |
protected $_customerRepositoryInterface; public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $_customerRepositoryInterface, ) { $this->_customerRepositoryInterface = $_customerRepositoryInterface; } $_customerId = 12; $_customer = $this->_customerRepositoryInterface->getById($_customerId); $_customerFirstName = $_customer->getFirstName(); //$customerData = $_customer->getData(); |
⇒ Using Factory Method to Get Customer Data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected $_customer; public function __construct( \Magento\Customer\Model\Customer $_customer ) { $this->_customer = $_customer; } public function getCustomerData() { $_customerId=22; $_customer = $this->_customer->create()->load($_customerId); //$customerData = $_customer->getData(); return $_customer; } } |
⇒ Call Phtml File:
1 2 3 4 5 |
$customerId = 1; //pass dynamic customer id $Customer = $block->getCustomerData($customerId); echo $Customer ->getFirstname(); // result customer first name echo $Customer ->getEmail(); // result as customer email echo $Customer ->getLastname(); // customerr lastname |
⇒ Using Object Manager:
1 2 3 |
$customerId=1; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerData = $objectManager->create(‘Magento\Customer\Model\Customer’)->load($customerId); |
use this code and nicely get Customer Data. if you have any questions put them in below comment section
Thank You!!..