1

I created a custom module that call a remote API service.

I have to call this API on some events, like: customer create, customer edit, order create etc...

For customer edit all works fine with:

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_account_edited">
        <observer name="my_customer_account_edited" instance="Vendor\Module\Observer\EditCustomer" />
    </event>
</config>

app/code/Vendor/Module/Observer/EditCustomer.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class EditCustomer implements ObserverInterface{
    public function execute(Observer $observer){
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylog.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $editedCustomerEmail = $observer->getEvent()->getEmail();

        // Ref: app/code/Magento/Customer/Controller/Account/EditPost.php
        // $this->_eventManager->dispatch(
        //     'customer_account_edited',
        //     ['email' => $customerCandidateDataObject->getEmail()]
        // );

        $logger->info($editedCustomerEmail);

        // load customer by email and do my things...
    }
}

Now I need to do the same thing when a new customer is registered.

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_account_edited">
        <observer name="my_customer_account_edited" instance="Vendor\Module\Observer\EditCustomer" />
    </event>
    <event name="customer_register_success">
        <observer name="my_customer_register_success" instance="Vendor\Module\Observer\NewCustomer" />
    </event>
</config>

app/code/Vendor/Module/Observer/NewCustomer.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class EditCustomer implements ObserverInterface{
    public function execute(Observer $observer){
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylog.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $editedCustomerEmail = $observer->getEvent()->getEmail();

        $logger->info($editedCustomerEmail);

        // load customer by email and do my things...
    }
}

In this case the variable $editedCustomerEmail is empty...

Already tried with:

// Ref: app/code/Magento/Customer/Controller/Account/CreatePost.php
// $this->_eventManager->dispatch(
//     'customer_register_success',
//     ['account_controller' => $this, 'customer' => $customer]
// );
$customer = $observer->getEvent()->getCustomer();
// or
$customer = $observer->getEvent()->getData('customer');

But Customer Object is always Null...

If this can be useful: I'm using the 3rd party extension Amasty_Checkout, and I found this code in 2 files:

$this->eventManager->dispatch(
    'customer_register_success',
    [
        'customer' => $account,
        'amasty_checkout_register' => true
    ]
);

Where I wrong? This is a conflict? How can I solve?

1

At Vendor\Module\Observer\NewCustomer, observer you need to use

$editedCustomerEmail = $observer->getEvent()->getCustomer()->getEmail();

instead of

$editedCustomerEmail = $observer->getEvent()->getEmail();

at customer_register_success event's Observer, you can get Customer data using $observer->getEvent()->getCustomer().

At customer_account_edited event you only get customer email.

And where customer_register_success provide full customer info means all customer all fields.

2
  • Thank you! As I wrote, "I already tried with: $customer = $observer->getEvent()->getCustomer();" but $customer was null... Now it works, don't know why... probably was a cache problem. Feb 5 '20 at 16:45
  • oah.Bad luck was :)
    – Amit Bera
    Feb 5 '20 at 16:52

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.