Magento 2: Get Customer Data after Login with Observer

In this tutorial, you will go through how to get customer data after login using observer in magento2. This trick will be very useful and handy when you want to get customer data after login with observer.

First of all, You need to create customer_login event observer for customer login action.

Step-1: Create events.xml

First, you need to create your event file: app/code/Vendor/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="Vendor\Module\Observer\CustomerLogin" />
</event>
</config>

Step-2: Create Observer (CustomerLogin.php)

After event file, you need to create your observer file: app/code/Vendor/Module/Observer/CustomerLogin.php

<?php
namespace Vendor\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;
}
}

I hope you like this post, Please feel free to comment below, your suggestion and problems!

(Visited 26 times, 1 visits today)
Chandra Kumar

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

0 Comment

Leave a Comment

Your email address will not be published. Required fields are marked *