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

Now you need to create one more file named “Customerlogin.php” file at the below path.

app\code\Vendor\Extension\Observer\CustomerLogin.php

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!

Click to rate this post!
[Total: 7 Average: 5]