1

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.

1
  • 2
    Please explain where your some code over there? .you cannot get customer data until customer set in session,
    – Amit Bera
    Commented Sep 27, 2018 at 9:28

3 Answers 3

3

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.

3
  • Can I use before login event ?
    – Ravi Soni
    Commented Sep 27, 2018 at 8:02
  • I am not sure if there is any before login event, but you can try using customer_login event. Commented Sep 27, 2018 at 8:05
  • usefull list for the events in M2 and where each is dispatched mageplaza.com/magento-2-module-development/…
    – M.Elwan
    Commented Sep 27, 2018 at 8:10
2

create module directory structure like the following: enter image description here

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.
    }
}
1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.