0

I want to get Customer default shipping address in Magento 2.

0

3 Answers 3

3

Here is the sample code of how you get the default shipping address information of the customer

<?php

namespace Vendor\Module\Model;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Directory\Model\CountryFactory;

class MyClass
{

    /**
     * @var CustomerRepositoryInterface
     */
    private $customerRepository;

    /**
     * @var AddressRepositoryInterface
     */
    private $addressRepository;

    /**
     * @var CountryFactory
     */
    private $countryFactory;

    /**
     * MyClass constructor.
     * @param CustomerRepositoryInterface $customerRepository
     * @param AddressRepositoryInterface $addressRepository
     * @param CountryFactory $countryFactory
     */
    public function __construct(
        CustomerRepositoryInterface $customerRepository,
        AddressRepositoryInterface $addressRepository,
        CountryFactory $countryFactory
    ) {
        $this->customerRepository = $customerRepository;
        $this->addressRepository = $addressRepository;
        $this->countryFactory = $countryFactory;
    }


    public function getShippingInfo()
    {
        $customerId = 1;
        $customer = $this->customerRepository->getById($customerId);
        $shippingAddressId = $customer->getDefaultShipping();

        $shippingAddress = $this->addressRepository->getById($shippingAddressId);

        echo $shippingAddress->getRegion()->getRegion(); // Get region
        echo $shippingAddress->getCity(); // Get city
        echo $shippingAddress->getPostcode(); // Get postcode

        //Get country name
        $countryCode = $shippingAddress->getCountryId();
        $country = $this->countryFactory->create()->loadByCode($countryCode);
        echo $country->getName();
    }
}
1
  • 1
    Prince bro Thank you it's working fine. May 3, 2021 at 5:36
0

Try the below code and let me know if you need more details.

$customerRepository = Magento\Customer\Api\CustomerRepositoryInterface
$addressRepository = Magento\Customer\Api\AddressRepositoryInterface
//define above variables in constructor.
 $customer = $this->customerRepository->getById($customerId);
 $shippingAddressId = $customer->getDefaultShipping();

//get default billing address
 try {
    $shippingAddress = $this->addressRepository->getById($shippingAddressId);
} catch (\Exception $e) {
    //
}
2
  • Thanks but how to get city and street address all filed value. May 1, 2021 at 5:30
  • or try this $shippingAddress = $customer->getDefaultShippingAddress(); echo $shippingAddress->getCountryId(); echo '<br>'; echo $shippingAddress->getCity(); echo '<br>'; echo $shippingAddress->getRegionId(); echo '<br>'; echo $shippingAddress->getRegion();
    – Nits
    May 1, 2021 at 17:52
0

try below

$customerRepository = Magento\Customer\Api\CustomerRepositoryInterface
$addressRepository = Magento\Customer\Api\AddressRepositoryInterface
//define above variables in constructor.
 $customer = $this->customerRepository->getById($customerId);
 $shippingAddressId = $customer->getDefaultShipping();

//get default billing address
 try {
    $shippingAddress = $this->addressRepository->getById($shippingAddressId);
    $shippingAddress->getCity();
    $shippingAddress->getStreet();
} catch (\Exception $e) {
    //
}

accept as a solution if works so other can find out right code.

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.