1

I need to retrieve id and email from the new customer after registration success. I imagine I have to bind the "customer_register_success" event, but my attempt is not working:

etc/frontend/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_register_success">
        <observer name="customer_register_success_observer" instance="<Mynamespace>\<Mymodule>\Model\Observer\CustomerRegister" />
    </event>
</config>

Model/Observer/CustomerRegister.php

<?php

namespace Vendor\Module\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->getName();
    exit;
  }
}

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '<Mynamespace_Mymodulename>',
    __DIR__
);

config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="<Mynamespace_Mymodulename>" setup_version="1.0.0" />
</config>

Now when a new customer registers himself he sees the error: "We can't save the customer." (though the registration goes well and the new user is saved on the db), plus I don't see any "echo" message and I don't know. if the error message depends on my changes.

How to debug it.

1
  • Please let me know magento version is 2.3.X? Apr 29, 2019 at 13:08

3 Answers 3

0

Try below code

<?php

namespace Vendor\Module\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();
    echo $customer->getLastname();
    exit;
  }
}
7
  • The main problem is that I don't even know where that "echo" should be shown. I don't see any result at all except for that error (but I'm sure it depends on my code)
    – frabis
    Dec 6, 2017 at 14:58
  • That echo should be shown at createPost action when you register Dec 6, 2017 at 15:00
  • I meant where can I visually see the values printed (in the frontend) ?
    – frabis
    Dec 6, 2017 at 15:10
  • once you register as customer, you can see white screen with all the "echo" statement. Dec 6, 2017 at 15:11
  • Ok, that is definitely not happening :), I just continue to see the registration form with the error message at the top. I think that the event observer is not even triggered, in fact I'm searching a way to log something in order to understand what it's not working
    – frabis
    Dec 6, 2017 at 15:15
0

You need update your namespace:

namespace Mynamespace\Mymodule\Model\Observer

use Magento\Framework\Event\ObserverInterface;

0

Echo is not a way to debug in the observer.

You should create a log file and check if the observer is writing in that log file or not.

e.g file_put_conents("/var/www/html/mymagento/var/log/test.log", $customer->getFirstname())

Use this line instead of echo and check if there is something written in the test.log file. If there is than your observer is working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.