- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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,
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get shipping address for customer
Hello @Ankit Jasani
I'm try to get Customer name from below function:
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get shipping address for customer
Not yet
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get shipping address for customer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get shipping address for customer
@salghernas833e wrote:I found a solution in the below link:
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.