0

I want to show all shipping addresses of a customer. I am getting customer_id only. I am using $customer->getAddress() but it's giving all addresses from customer_address_entity table according to that customer_id. But I want only shipping addresses from these addresses. How can I solve it..

3
  • Do you want to get default shipping address? – KKR Jul 26 '19 at 6:40
  • no i want all shipping addresses saved by that customer and want to show those list of shipping addresses on frontend – user81911 Jul 26 '19 at 6:42
  • 1
    There is only 1 address stored as Default Shipping in Magento. you can not able to save one address as shipping address. There is no option to save multiple shipping address. You can use any address to shipping address while checkout but there is only one Default Shipping address. you can change it from your account and also from backend. – KKR Jul 26 '19 at 6:55
0

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

If you need the default Shipping address you can call:

$shipingAddress = $customer->getDefaultShippingAddress(); 
0

You can get the customer shipping address by using customer Id from below code.

In your block file:

public function __construct(
       ...........
        \Magento\Customer\Api\AccountManagementInterface $accountManagement,
       ........
) {
      ........
      $this->accountManagement = $accountManagement;
      ........
}

public function getDefaultShippingAddress($customerId)
{
        try {
            $address = $this->accountManagement->getDefaultBillingAddress($customerId);
        } catch (NoSuchEntityException $e) {
            return __('You have not set a default shipping address.');
        }
        return $address;
}

In your template file:

<?php
$customerId = 1;

$shippingAddress = $block->getDefaultShippingAddress($customerId);
//echo "<pre>";print_r($shippingAddress->__toArray());
?>
0

Not a good option but you can use it.

Get all the $customer->getAddress() of customer. Get all the Orders of Customer. From this orders fetch the shipping address ids. The collection of addresses of customer filter it using this ids.

You will get all the addresses used for shipping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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