Search code examples
phpeventsmagento2observers

Print the data of customer after registration in magento 2


I want to print the customer data after registration in magento 2. I have done following code.

In app\code\Cloudways\Newmodule\etc\events.xml I have written following code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="customer_register_success_observer" instance="Cloudways\Newmodule\Observer\CustomerRegister" />
    </event>
</config>

and in app\code\Clowdways\Newmodule\Observer\CustomerRegister.php I have written this code

<? php

namespace Cloudways\Newmodule\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerRegister implements ObserverInterface
{
    public function execute(Magento\Framework\Event\Observer $observer)
    {
        echo "Customer Registered";
        $customer=$observer->getEvent()->getCustomer();
        echo $customer->getFirstName();
        exit;
    }
}

Customers are registered and stored in database successfully. But I am not able to see echo result in frontend. Where should I see this result or please tell me what's going wrong in code.


Solution

  • You have to override create post controller for the customer registration.

    di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
         <preference for="Magento\Customer\Controller\Account\CreatPost" type="YourCompanyName\YourModule\Controller\Account\CreatPost" />
    </config> 
    

    In the controller's execute() method you will get customer data.