cancel
Showing results for 
Search instead for 
Did you mean: 

how to observe event of customer_login?

SOLVED

how to observe event of customer_login?

Hi there.

this is first time to create extension and observer.

just finished reading 

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/events-and-observers.html

 

my module directory is like this.

.<module_root>
├── MyModule.php
├── Observer
│   └── MyModuleObserver.php
├── etc
│   ├── frontend
│   │   └── events.xml
│   └── module.xml
└── registration.php

 

i want to create an event that fires after customer_login. (so this is not original event)

 

this is 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_login">
        <observer name="MyObserver" instance="ModuleRoot/MyModule/MyModule" />
    </event>
</config>

MyObserver is

<?php                                                                                                                                                                                             
namespace ModuleRoot\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
    public function __construct()
    {
        die('HelloWorld'); // how this construct is called?
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        die('HelloWorldExection'); // this code is not callable because arleady died in constructor
    }
}

on the end, MyModule.php is 

 

<?php                                                                                                                                                                                              

namespace MyModule\MyModule;

use Magento\Framework\Event\ManagerInterface as EventManager;

class MyModule
{
    /**
     * @var EventManager
     */
    private $eventManager;

    /*
     * @param \Magento\Framework\Event\ManagerInterface as EventManager
     */
    public function __construct(EventManager $eventManager)
    {
        $this->eventManager = $eventManager;
    }

    /**
     * set admin flag after login
     */
    public function something() // what is something???????? how is this called????
    {
        $this->eventManager->dispatch('customer_login');
    }
}

Yes. I DO NOT understand.

what is a relationship between MyModule and MyObserver?

 

I expect when customer login, the redirect stops by die of MyObserver code.

but ofcourse the login continues.

 

lots of magento tutorial pages gives information but all of them are not enough(because magent1 and 2 are mixed in)

 

heeeeelp me i already tired.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to observe event of customer_login?

Hi there.

still I have an autoloader problem.

 

[2020-09-28 08:25:55] main.CRITICAL: Class ModuleRoot/MyModule/Observer/CustomerLogin does not exist {"report_id":"2f147ab16344b81443732bf80271bdcd03d9b7733ad233124a1070d53a283378","exception":"[object] (ReflectionException(code: -1): Class ModuleRoot/MyModule/Observer/CustomerLogin does not exist at /home/kuro/proj/magento/vendor/magento/framework/Code/Reader/ClassReader.php:26)"} []

 

CustomerLogin.php is located 

 

app/code/ModuleRoot/MyModule
├── Observer
│   └── CustomerLogin.php
├── etc
│   ├── events.xml
│   └── module.xml
└── registration.php

 

and class is 

<?php                                                                                                                                                                                              
namespace ModuleRoot\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getName(); //Get customer name
        exit;
    }
}

this error message shows on exception log after i clicked login button.

 

I already did these bin/magento command as you showed me(thanks lots)

and also did composer dump-autoload (I do not know wether it helps or not)

 

is there any other solution?

View solution in original post

9 REPLIES 9

Re: how to observe event of customer_login?

@reikokurose240 what i found is something is wrong with the folder structure which you are using.

Try to use it as below.

Use customer_login event observer for customer login action.

 

1. Create events.xml

app/code/MyModule/Module/etc/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_login">
        <observer name="customer_login_observer" instance="MyModule\Module\Observer\CustomerLogin" />
    </event>
</config>

 

2.Now create observer CustomerLogin.php

app/code/MyModule/Module/Observer/CustomerLogin.php

 

<?php

namespace MyModule\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getName(); //Get customer name
        exit;
    }
}

Let me know if you are still getting any issue.

Hope it helps!

 

Thanks

Re: how to observe event of customer_login?

Thanks for reply! i really appriciate you.

well, for now i copy and paste the code that you gave me, cache:flush then log in again.

the result was same.

 

i 'm not sure my code executed or not. 

 

i put logger for CustomerLogin.php

    public function __construct(\Psr\Log\LoggerInterface $logger)                                                                                                                                  
    {
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $this->logger->critical('executed!');
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getName(); //Get customer name
        die();
    }

php bin/magento setup:config:set --enable-debug-logging=true

php bin/magento cache:flush

tail -f var/log/system.log

tail -f var/log/exception.log

tail -f var/log/debug.log

 

nothing comes up. 

 

when i do customer login (http://MAGENT_URL/customer/account/login/referer/FORM_KEY),

after login process, the message "an unspecified error occurred. Please contact us for assistance." show on the display. log in action was succeed.

Re: how to observe event of customer_login?

from the beginning, my module code is not exist in generated/code/. is it normal?

other modules made by third party are in the directory. i think thats why my code does not work .

i may need to re-consider the structure...

Re: how to observe event of customer_login?

Hi @reikokurose240 ,

 

You can check your module code using below link which explains proper module structure.

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

 

Later add the event observer code and run deployment commands.

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: how to observe event of customer_login?

Thanks!

I can create a new module (just hello world) did work well but observer does not work yet.

 

Re: how to observe event of customer_login?

Hi @reikokurose240,

 

Can you try using below event

<event name="controller_action_postdispatch_customer_account_loginpost">

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: how to observe event of customer_login?

thanks!

maybe this event name works.

debug message said "Class MyModule/Module/Observer/CustomerLogin does not exist"

i did not this message when i put event name "customer_login".

so something proceeds.(event though there is another probelm

 

.MyModule
├── Module
│   ├── Observer
│   │   └── CustomerLogin.php
│   ├── etc
│   │   ├── events.xml
│   │   └── module.xml
│   └── registration.php

 

after i changed event name, i did

cache:flush

setup:upgrade

setup:di:compile

 

i checked observer's namespace and instance in events.xml, there is no problem.

is there any autoloader?

 

thanks to all about your kindness

Re: how to observe event of customer_login?

Hi @reikokurose240 ,

 

Can you try below commands before your commands 

Move your events .xml in frontend folder.

1. rm -rf generated/* pub/static/frontend

2. setup:upgrade

3. setup:di:compile

4. setup:static-content:deploy

5. cache:flush

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: how to observe event of customer_login?

Hi there.

still I have an autoloader problem.

 

[2020-09-28 08:25:55] main.CRITICAL: Class ModuleRoot/MyModule/Observer/CustomerLogin does not exist {"report_id":"2f147ab16344b81443732bf80271bdcd03d9b7733ad233124a1070d53a283378","exception":"[object] (ReflectionException(code: -1): Class ModuleRoot/MyModule/Observer/CustomerLogin does not exist at /home/kuro/proj/magento/vendor/magento/framework/Code/Reader/ClassReader.php:26)"} []

 

CustomerLogin.php is located 

 

app/code/ModuleRoot/MyModule
├── Observer
│   └── CustomerLogin.php
├── etc
│   ├── events.xml
│   └── module.xml
└── registration.php

 

and class is 

<?php                                                                                                                                                                                              
namespace ModuleRoot\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getName(); //Get customer name
        exit;
    }
}

this error message shows on exception log after i clicked login button.

 

I already did these bin/magento command as you showed me(thanks lots)

and also did composer dump-autoload (I do not know wether it helps or not)

 

is there any other solution?