简体   繁体   中英

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.

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.

<event name="controller_action_predispatch_customer_account_createpost">
        <observer name="customer_resgister_observer" instance="Comlitix\ComlitixInfo\Observer\GetCustomerDetails" />
    </event>

namespace Hoop\Util\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterCustomerRegistration implements ObserverInterface
{
    protected $request;

    public function __construct(\Magento\Framework\App\Request\Http $request
    )
    {
        $this->request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        var_dump($this->request->getPost());
        die('test');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM