Requirements differ from business to business and the one who fulfills every need can be known as ultimate. When it comes to eCommerce, Magento does provide a one-stop solution to the store owner to fulfill customer needs and serve a seamless shopping experience to their shoppers. And definitely store owner’s requirement is endless but Magento never failed.
Recently one of our customers wanted to create one customer group inside his Magento 2 store and when he adds any customer to that group, their account should be automatically get locked and it should not be accessible anymore. He got this idea to prevent spamming inside his store. To end his search he came to us and being helping hands to our customers is a core value. Maybe you have come across a situation in the past or looking for the same. So here is code that will help you to lock your customer account when it falls in a specific group of Magento 2.
For this purpose, we have to make use of the customer_login event observer to collect customer group after login and then logout.
To do the same first you need to open the “Events.xml” file located at the below path.
app\code\Vendor\Extensionetc\events.xml
1 2 3 4 5 6 |
<?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> |
Now you need to create one more file named “Customerlogin.php” file at the below path.
app\code\Vendor\Extension\Observer\CustomerLogin.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\ObserverInterface; class CustomerLogin implements ObserverInterface { /** * @var \Magento\Framework\App\ResponseFactory */ private $responseFactory; /** * @var \Magento\Framework\UrlInterface */ private $url; private $customerSession; private $messageManager; public function __construct( \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\UrlInterface $url, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Message\ManagerInterface $messageManager ) { $this->responseFactory = $responseFactory; $this->url = $url; $this->customerSession= $customerSession; $this->messageManager = $messageManager; } public function execute(\Magento\Framework\Event\Observer $observer) { $customer = $observer->getEvent()->getCustomer(); $customerGroup = $customer->getGroupId(); if($customerGroup==22) { $this->customerSession->logout(); $this->messageManager->addErrorMessage(__('You are Locked')); $redirectionUrl = $this->url->getUrl('[SET YOUR URL HERE]'); $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse(); return $this; } } } |
That’s it. Now extension works flawlessly to lock customer account if they fall into a specified customer group and they will get the message that “You are Locked” if they try to log in and get redirected to a specified URL.
Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issues while implementing this code.
Happy Coding!