11

How to get customer address/billing address by customer ID? here's what I've done so far:

$customerId = $_POST["customer_id"];
$customer = $this->_customerRepository->getById($customerId);
$address = $this->_addressRepository->getByCustomerId($customerId);//error

4 Answers 4

12

You cannot retrieve an address based on the customer id so this code will never work:

$address = $this->_addressRepository->getByCustomerId($customerId);//error

Because the getByCustomerId method does not exist in the service contract classes.

What you can do though, is use the data service contract customer class with the following code:

$customerId = $_POST["customer_id"];
$customer = $this->_customerRepository->getById($customerId);
$addresses = $customer->getAddresses();

Please note that getAddresses will return an array of Magento\Customer\Api\Data\AddressInterface.

If you need the default billing address you can call:

$billingAddress = $customer->getDefaultBilling(); 
5
  • but when i use $customer->getDefaultBilling(); it return NULL
    – simple guy
    Commented Feb 14, 2017 at 8:58
  • @simpleguy that's because the customer you're testing does not have a default billing address probably Commented Feb 14, 2017 at 8:59
  • @RaphaelatDigitalPianism It is not working for me. Can you please let men know how I can get this details. Commented Oct 31, 2017 at 6:07
  • How to get addressID? Commented Aug 31, 2018 at 6:50
  • getDefaultBillingAddress
    – Mark Lopez
    Commented Nov 6, 2020 at 13:39
7

For get customer adderess using order id in .phtml file

$customerId = 3;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
$customerAddress = array();

foreach ($customerObj->getAddresses() as $address)
{
    $customerAddress[] = $address->toArray();
}

foreach ($customerAddress as $customerAddres) {

    echo $customerAddres['street'];
    echo $customerAddres['city'];
}
3
protected $_address;

public function __construct(
    ...
    \Magento\Customer\Model\Address $address,
    ...     
)
{
    ...
    $this->_address = $address;
    ...
}

Use in your function:-

$addressObj = $this->_address;
$addressObj->load($addressid); 

you can get address collection as below:-

$addresses = $addressObj->getCollection();
0

$ private $customerFactory;

public function __construct(
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    Context $context
) {
    parent::__construct($context);
    $this->customerFactory = $customerFactory;
}

public function execute()
{
    $customerId = 1;
    $customer = $this->customerFactory->create();
    echo "<pre>";
    var_dump($customer->getAddressCollection()->addFieldToFilter('parent_id',$customerId)->getData());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.