Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Alexander-Pop/7fa1b6f1b3db707bfb45aa5346864adf to your computer and use it in GitHub Desktop.
Save Alexander-Pop/7fa1b6f1b3db707bfb45aa5346864adf to your computer and use it in GitHub Desktop.
Magento 2 - Get Customer Information From Customer Id #magento2 #customer
<?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