Created
February 25, 2020 15:27
-
-
Save Alexander-Pop/7fa1b6f1b3db707bfb45aa5346864adf to your computer and use it in GitHub Desktop.
Magento 2 - Get Customer Information From Customer Id #magento2 #customer
This file contains hidden or 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 | |
//Note: use Objectmanager only for local dev. For real dev use constructor | |
//Get Customer Information From Customer ID | |
<?php | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create(); | |
$customerId = 1; | |
$customer = $customerFactory->load($customerId); | |
//fetch whole customer information | |
echo "<pre>"; | |
print_r($customer->getData()); | |
//fetch specific information | |
echo $customer->getEmail(); | |
echo $customer->getFirstName(); | |
echo $customer->getLastName(); | |
//Get Customer Default Billing Address | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create(); | |
$customerId = 1; | |
$customer = $customerFactory->load($customerId); | |
$billingAddressId = $customer->getDefaultBilling(); | |
$address = $objectManager->get('Magento\Customer\Model\AddressFactory')->create()->load($billingAddressId); | |
echo $address->getData('firstname')."<br>"; | |
echo $address->getData('lastname')."<br>"; | |
echo $address->getData('street')."<br>"; | |
echo $address->getData('city')."<br>"; | |
echo $address->getData('region')."<br>"; | |
echo $address->getData('postcode')."<br>"; | |
echo $address->getData('country_id')."<br>"; | |
//Get Customer Default Shipping Address | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create(); | |
$customerId = 1; | |
$customer = $customerFactory->load($customerId); | |
$shippingAddressId = $customer->getDefaultShipping(); | |
$address = $objectManager->get('Magento\Customer\Model\AddressFactory')->create()->load($shippingAddressId); | |
echo $address->getData('firstname')."<br>"; | |
echo $address->getData('lastname')."<br>"; | |
echo $address->getData('street')."<br>"; | |
echo $address->getData('city')."<br>"; | |
echo $address->getData('region')."<br>"; | |
echo $address->getData('postcode')."<br>"; | |
echo $address->getData('country_id')."<br>"; | |
//Get Customer All Addresses | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create(); | |
$customerId = 1; | |
$customer = $customerFactory->load($customerId); | |
$Addresses = $customer->getAddresses(); | |
foreach ($Addresses as $address){ | |
echo "<pre>"; | |
print_r($address->getData()); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment