I want to check customer group around the login process
and want to add some custom code over there.
Anyone have any idea?
Please tell me.
You can try creating a plugin for the method loginById
of class Magento\Customer\Model\Session
or
you can try creating the observer for the customer_login
event.
create module directory structure like the following:
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Namespace_CustomerLogin',
__DIR__
);
etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Namespace_CustomerLogin" setup_version="1.0.0"/>
</config>
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 instance="Namespace\CustomerLogin\Observer\Frontend\Customer\Login" name="namespace_customerlogin_observer_frontend_customer_login_customer_login"/>
</event>
</config>
Observer/Frontend/Customer/Login.php
<?php
namespace Namespace\CustomerLogin\Observer\Frontend\Customer;
class Login implements \Magento\Framework\Event\ObserverInterface
{
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
//Here you can get your customer data by following
echo $customerGroup=$this->_customerSession->getCustomer()->getGroupId();
$customer = $observer->getEvent()->getCustomer();
echo $customer->getName(); //Get customer name
echo $customer->getGroupId(); //Get customer group and place your Logic here what you want to Do.
}
}
Please follow the below code
1.Create a events.xml file under "Namespace/Module/etc/frontend"
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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="custom_login_data" instance="Namespace\Module\Observer\CustomerData" shared="false" />
</event>
</config>
2.Create a observer(CustomerData.php) under "Namespace/Module/Observer"
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Namespace\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class CustomerData implements ObserverInterface
{
protected $_request;
protected $groupRepository;
public function __construct(\Magento\Framework\App\RequestInterface $request,\Magento\Customer\Api\GroupRepositoryInterface $groupRepository)
{
$this->_request = $request;
$this->groupRepository = $groupRepository;
}
/**
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$data = $observer->getEvent()->getCustomer();
$groupName = $this->getGroupName($data->getGroupId());//will return you the Group Name
/*you can add here your custom code */
return $this;
}
public function getGroupName($groupId){
$group = $this->groupRepository->getById($groupId);
echo $group->getCode();
}
}
I hope it will help you!
some code over there
? .you cannot get customer data until customer set in session,