1

How to get customer address(company,telephone,country,state,city etc.) after registration in observer?

I am using observer.

Code -

$customer = $observer->getData('customer');

first name and email is working -

$customer->getFirstName()
$customer->getEmail()

Below code is not working -

$customer->getDefaultShippingAddress()->getCompany()
$customer->getDefaultShippingAddress()->getTelephone()
$customer->getDefaultShippingAddress()->getCountry()
$customer->getDefaultShippingAddress()->getRegion()
$customer->getDefaultShippingAddress()->getCity()
1

As $customer is an object of service contact data interface \Magento\Customer\Api\Data\CustomerInterface .So you cannot get default address using $customer->getDefaultShippingAddress().

This CustomerInterface does not have any method like getDefaultShippingAddress() which give default address.

If you want to get default from $customer then you can use $customer->getAddresses() but its give list of address of that customer. So, you need run a foreach loop for getting default address.

 if ($customer instanceof \Magento\Customer\Api\Data\CustomerInterface) {
        $addresses = $customer->getAddresses();

        foreach ($addresses as $address) {
            if ($address->isDefaultShipping()) {
                // Default Shipping Address
                $address->getCompany();
                $address->getTelephone();
                $address->getCountryId();
                $region = $address->getRegion();
                if($region !== null && ($region instanceof \Magento\Customer\Api\Data\RegionInterface)){
                     $region->getRegionCode();
                     $region->getRegionId();
                     $region->getRegion();
                }
                $address->getCity();
            }
            if ($address->isDefaultBilling()) {
                // Default Shipping Address
                $address->getCompany();
                $address->getTelephone();
                $address->getCountryId();
                $region = $address->getRegion();
                if($region !== null && ($region instanceof \Magento\Customer\Api\Data\RegionInterface)){
                     $region->getRegionCode();
                     $region->getRegionId();
                     $region->getRegion();
                }
                $address->getCity();
            }
        }
    }
  • Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Address::getDefaultShipping() – sumeet bajaj Jun 3 at 11:00
  • wrong method was there. Now it fix – Amit Bera Jun 3 at 11:01
  • Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Address::getCountry() – sumeet bajaj Jun 3 at 11:06
  • Modified... it is working – Amit Bera Jun 3 at 11:11
  • before mail is not coming,now mail receive but with error - Error filtering template: Recoverable Error: Object of class Magento\Customer\Model\Data\Region could not be converted to string in /var/www/html/gbdistro_new/vendor/magento/framework/Filter/Template.php on line 173 – sumeet bajaj Jun 3 at 11:12

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.