Magento 2 call observer after customer login


Magento 2 call observer after customer login Success – In magento 2 you need to create events.xml and observer file. Here in this tutorial we are going to explain how you can create observer after customer login.


Magento 2 call observer after customer login Example | Get Data | Update

You can create magento observer event customer login after using the steps below-

  • 1. Create events.xml
  • 2. Create Observer File

1. Create events.xml

First create events.xml located at –

app/code/Vendor/Module/etc/events.xml

Magento 2 call observer after customer login 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_login">
        <observer name="customer_login_observer" instance="Vendor\Module\Observer\CustomerLogin" />
    </event>
</config>

2. Create Customer Login File

Now you need to create observer file –

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

Magento 2 call observer after customer login get Data:

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->getEmail(); //Get customer Email
        echo $customer->getName(); //Get customer Name
        exit;
    }
}

So as in the above example you can get the customer data after login success. So using the above data you can update the customer attribute value or anything that you want on run time.


Advertisements

Add Comment

📖 Read More