1

I have the following code but it is not working.

vendorName/moduleName/etc/module.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="Vendorname_Modulename" setup_version="0.0.1" />
</config>

vendorName/moduleName/etc/frontend/events.xml

<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_resgister_observer" instance="Vendorname\Modulename\Observer\CustomerRegister" />
  </event>
</config> 

VendorName/ModuleName/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->getFirstname();
    echo $customer->getLastname();
    exit;
  }
}

VendorName/ModuleName/registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'VendorName_ModuleName',
__DIR__
);

Does anybody know why is not working?

1 Answer 1

0

Your code is correct but the namespace in your file VendorName/ModuleName/Observer/CustomerRegister.php is wrong

namespace Vendor\Module\Observer;

It should be -

namespace VendorName/ModuleName/Observer;

Your Answer

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

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