Question

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

Was it helpful?

Solution

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();
    }
}

OTHER TIPS

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) {
    //
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top