0

How can I create my own event and observer for customer sign up, From this link,

Magento2 : customer_account_create event is not triggering

I found that We can use customer_register_success for customer register success.

So I found that events.xml in /module-persistent/etc/webapi_rest/events.xml

<event name="customer_register_success">
    <observer name="persistent" instance="Magento\Persistent\Observer\RemovePersistentCookieOnRegisterObserver" />
</event>

So observer file is RemovePersistentCookieOnRegisterObserver.php I want to make my custom event and observer for customer sign up, how to achieve that and where should I write my custom events and observer.

This is my code

 namespace YX\Customer\Observer

 class RegisterSuccess implements \Magento\Framework\Event\ObserverInterface
{

protected  $customlogger;
protected  $tempblock;
protected  $customerHelper;


public function __construct(
    \Rage\Base\Helper\CustomLogger $loggerHelper
     \YX\Catalog\Block\OrderHelper $orderHelper,
    \YX\Customer\Helper\CustomerHelper $customerHelper

)
{
    $this->customlogger = $loggerHelper->getLogger('custom');
    $this->tempblock = $this->getLayout()->createBlock('YX\Catalog\Block\OrderHelper');
    $this->customerHelper = $this->helper('YX\Customer\Helper\CustomerHelper');
}
public function execute(
    \Magento\Framework\Event\Observer $observer
) {

 \Magento\Framework\App\ObjectManager::getInstance()
        ->get(\Psr\Log\LoggerInterface::class)->debug("here in customer 
 login before func " .$this->customerHelper->isCustomer());

    if (!($this->customerHelper->isCustomer())){
        $customerID = $this->customerHelper->getCustomerId();
        $orderId = $this->customerHelper->getOrderId();
        $this->tempblock->guestToCustomer($orderId,$customerID);

    }
}

}

I have put dubgger logs but still, on the user sign up it doesn't go inside execute() function

4

Try this,

Create this file in the path app/code/vendor/module/etc/events.xml For example ,

<?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 instance="vendor\modulename\Observer\Customer\RegisterSuccess" name="customer_register_succes"/>
</event>

then

Place the respective observer file in the folder defined like app/code/vendor/module/Observer/Customer/RegisterSuccess.php

<?php                                                         
 namespace vendor\module\Observer\Customer;                        
 class RegisterSuccess implements \Magento\Framework\Event\ObserverInterface                                
 {

public function __construct(
    ......
    ......
    )
{
    ......
}
public function execute(
    \Magento\Framework\Event\Observer $observer
) {

    $customer = $observer->getEvent()->getData('customer');

}}

hope this helps.

  • @G Prathap it didnt work, the events will call automatically on customer registration ? – summu Feb 18 at 11:17
  • Yes it does working. I have checked and given answer – Prathap Gunasekaran Feb 18 at 11:30
  • Dont you think we have to Dispatch events so that observer can catch it . devdocs.magento.com/guides/v2.3/extension-dev-guide/… – summu Feb 18 at 11:43
  • It's for creating new events but in our case customer registration success event is an core one. So we don't require that. Check again if it is done correctly or update a question with you code, I'll figure it out – Prathap Gunasekaran Feb 18 at 11:45
  • @G Prathap I have updated the question – summu Feb 18 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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