cancel
Showing results for 
Search instead for 
Did you mean: 

magento 2.3 create observer after successful account creation

magento 2.3 create observer after successful account creation

I have successfully created custom customer attributes in Magento 2.3.2 (like doctor name and doctor zipcode) and I can see/modify them in my adminhtml customer view. What I would like is to have them inserted into customer orders and order e-mails. Has anyone already done that ? Thank you for your pointers/hints/solutions ! Edit :

I tried using observers : 1 for customer signup, 1 on successful order. So I added 2 modules

One called CustomerRegister : registration.php :

<?php

\Magento\Framework\Component\ComponentRegistrar::register(    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'CustomerRegister_Emails',    __DIR__);

etc/frontend/events.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="customerregister" instance="CustomerRegister\Emails\Observer\AddCustomerData">
    </event>
</config>

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="CustomerRegister" setup_version="1.0.1">
<sequence>
<module name="Magento_CustomerRegister"/>
</sequence>
    </module>
</config>

Observer/AddCustomerData.php :

<?php

namespace Anshu\Emails\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddCustomerData implements ObserverInterface
{
 public function __construct(    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
  ){    $this->_customerRepositoryInterface = $customerRepositoryInterface;
  }

  public function execute(\Magento\Framework\Event\Observer $observer)
  {    $transport = $observer->getEvent()->getTransport();    $customer = $this->_customerRepositoryInterface->getById($transport->getOrder()->GetCustomerId());$transport->addData([
'nommedecin' => $customer->getCustomAttribute('nommedecin')->getValue()

]);
}
}

 It seems my module is not even called? 

Of course I did bin/magento setup:upgrade and all the required stuff...

3 REPLIES 3

Re: magento 2.3 create observer after successful account creation

Hello @alcyonis 

 

It seems that you have defined incorrect namespace in the Observer/AddCustomerData.php file.

In Observer/AddCustomerData.php file
Please change

namespace Anshu\Emails\Observer;

to

namespace CustomerRegister\Emails\Observer;

Now your observer will be called.

Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: magento 2.3 create observer after successful account creation

Hi Team,

 

Nice tutorial. Can you please help me out on this part?

In my project whenever any user visits the front-end & creates an account like https://docs.magento.com/user-guide/customers/account-company-create.html. Company form is displayed. After entering all valid information, an email is sent to the user to set a password. After this, the admin approves the account from the backend.

I want to modify this requirement i.e. I want to add new fields password/confirm password over the same form & want to set auto-approval of all the accounts i.e. I want all accounts to set passwords on the form. No email to set password & no approval will be required from admin. I want this as default Magento flow work. Is this possible? Please confirm.

Rashi

Re: magento 2.3 create observer after successful account creation

@Meetanshi   Why this is 

namespace CustomerRegister\Emails\Observer;


for modules, we need to define the vendor name the app folder then observer Directory Right