Magento2 Tips & Tricks

Magento 2: Get Customer Data after Login with Observer

Magento 2: Get Customer Data after Login with Observer December 28, 2017Leave a comment

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

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!

Magento 2: Get Customer Data after Login with Observer
5 (100%) 1 vote

(Visited 14 times, 1 visits today)

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

Leave a Reply

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