0

I'm trying to get customer Id, first name, last name , email and Address info after customer_account_edited event is triggered, but I'm getting error 500 and the error is Uncaught Error: Call to a member function getId() on null in

Here is my code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $customer = $observer->getEvent()->getCustomer();

        $customer_id_temp = $customer->getId();
        $customer_id = $customer_id_temp;

        $customer_email=$customer->getEmail();
        $first_name= $customer->getFirstname();
        $last_name= $customer->getLastname();
        $customer_name = $first_name." ".$last_name;

        $session = $this->customerSession->getMyValue();
        $referrer_user_id = $customer_id."Test";
        //$customer_agent = $_SERVER ['HTTP_USER_AGENT'];

        //Billing Address variables

        $billingID = $customer->getDefaultBilling();
        $billingAddress = $objectManager->create('Magento\Customer\Model\Address')->load($billingID);
        $billingCompany = $billingAddress->getCompany();
        $billingTelephone = $billingAddress->getTelephone();
        $billingZipcode = $billingAddress->getPostcode();
        $billingCity = $billingAddress->getCity();
        $billingRegion = $billingAddress->getRegion();
        $billingStreet = $billingAddress->getStreet();
        $billingAddress1 = $billingStreet[0];
        $billingAddress2 = "";
        if(isset($billingStreet[1])){
            $billingAddress2 = $billingStreet[1];
        }   

        //Shipping Address variables
        $shippingId = $customer->getDefaultShipping();
        $shippingAddress = $objectManager->create('Magento\Customer\Model\Address')->load($shippingId);
        $shippingCompany = $shippingAddress->getCompany();
        $shippingTelephone = $shippingAddress->getTelephone();
        $shippingZipcode = $shippingAddress->getPostcode();
        $shippingCity = $shippingAddress->getCity();
        $shippingRegion = $shippingAddress->getRegion();
        $shippingStreet = $shippingAddress->getStreet();
        $shippingAddress1 = $shippingStreet[0];
        $shippingAddress2 = "";
        if(isset($shippingStreet[1])){
            $shippingAddress2 = $shippingStreet[1];
        }


        // If customer data is empty then doesn't need to process
        if (!$customer) {
            return $this;
        }

I want to get this data after the user edit his account. Also, is there another way to get Billing and Shipping address without using Object Manager? like with Dependency Injection.

0

My error was the way to get $customer object. This worked for me

$customer = $observer->getCustomerDataObject();


$customer_id = $customer->getId();

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.