cancel
Showing results for 
Search instead for 
Did you mean: 

Get shipping address for customer

Get shipping address for customer

Hello 

How can get the Shipping Address for the customer?

I need to get and save this information in metadata

 

I use Magento 2.4

 

Thank you,

 
10 REPLIES 10

Re: Get shipping address for customer

Hi @salghernas833e,

 

If you have customer object already available then you can use below ways.

 

1) All addresses information from customer object

$addresses = $customer->getAddresses();
if(count($addresses)) {
foreach($addresses as $addresse) {
echo $addresse->getCountryId();
echo '<br>';
echo $addresse->getCity();
echo '<br>';
echo $addresse->getRegionId();
echo '<br>';
echo $addresse->getRegion();

echo '<br>';
echo $addresse->getId();
}
}

 

2) Default Billing Address information

$billingAddress = $customer->getDefaultBillingAddress();
echo $billingAddress->getCountryId();
echo '<br>';
echo $billingAddress->getCity();
echo '<br>';
echo $billingAddress->getRegionId();
echo '<br>';
echo $billingAddress->getRegion();

echo '<br>';
echo $billingAddress->getId();

 

3) Default Shipping Address information

$shippingAddress = $customer->getDefaultShippingAddress();
echo $shippingAddress->getCountryId();
echo '<br>';
echo $shippingAddress->getCity();
echo '<br>';
echo $shippingAddress->getRegionId();
echo '<br>';
echo $shippingAddress->getRegion();

echo '<br>';
echo $shippingAddress->getId();

 

You will get address id also from above. If you want to update any address then you
need to get address id from above code and use below code to update.

 

/**
* @var \Magento\Customer\Api\AddressRepositoryInterface
*/
protected $addressRepository;

/**
* Sync constructor.
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
*/
public function __construct(
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository
) {
$this->addressRepository = $addressRepository;
}

public function changeAddress($addressId){
/** @var \Magento\Customer\Api\Data\AddressInterface $address */
$address = $this->addressRepository->getById($addressId);
$address->setCity('customCity'); // Update city
$address->setCountryId('UK'); // Update country id
// update what ever you want
$this->addressRepository->save($address);
}

 

Problem Solved? Accept as Solution!

 

Hope it helps!
Thanks

Ankit Jasani

Re: Get shipping address for customer

Hello @Ankit Jasani 

Thank you for your reply 

The value $customer what you mean? and how can get a current customer shipping addresses? Also need full information about customer first, last name and phone number

 

I hope understand me

 

 

Re: Get shipping address for customer

Hi @salghernas833e ,

 

You can get current logged in customer details by below way.

 

public function __construct(
\Magento\Customer\Model\Session $customer
) {
$this->customer = $customer;
}

public function yourMethodName()
{
$customer = $this->customer;
$customerName = $customer->getName();
$customerId = $customer->getId();
}

 

You can see $customer object above which is responsible for getting address details as well.

 

Problem Solved? Accept as Solution!

 

Hope it helps!

Thanks

 

Ankit Jasani

Re: Get shipping address for customer

Hello @Ankit Jasani 

 

I'm try to get Customer name from below function:

 

public function customerName()
{
$customer = $this->customer;
$name = $customer->getName();
return $name;
}
But didn't work for me, will be return name = PHPSESSID
 
Any advice!

Re: Get shipping address for customer


@salghernas833e wrote:

Hello 

How can get the Shipping Address for the customer?

I need to get and save this information in metadata

 

I use Magento 2.4

 

Thank you,


I think I am also facing this issue. Did you get any proper solution?

Re: Get shipping address for customer

Not yet Smiley Sad

Re: Get shipping address for customer

Hi @juanneuman3554 

I found a solution in the below link:

https://magento.stackexchange.com/questions/338045/magento-2-how-to-get-firstname-and-lastname-of-ab...

 

Good luck,

 

Hi @Ankit Jasani  Thank you a lot for your support

 

Re: Get shipping address for customer


@salghernas833e wrote:

Hi @juanneuman3554 

I found a solution in the below link:

https://magento.stackexchange.com/questions/338045/magento-2-how-to-get-firstname-and-lastname-of-ab...

 

Good luck,

 

Hi @Ankit Jasani  Thank you a lot for your support

 


thank you so much for this solution i was also facing same issue.

Re: Get shipping address for customer


@salghernas833e wrote:

Hello 

How can get the Shipping Address for the customer?

I need to get and save this information in metadata in web.

 

I use Magento 2.4

 

Thank you,

 

I use wordpress and this step can be easily done by Wo-commerce.