---
title: "How to Lock Customer Account if They belong to Specific Customer Group in Magento 2"
url: "https://magecomp.com/blog/lock-customer-account-of-specific-magento-2-customer-group/"
date: "2019-09-16T17:46:58+00:00"
modified: "2025-02-21T09:54:51+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "General"
  - "How To"
  - "Magento 2"
word_count: 485
reading_time: "3 min read"
summary: "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 t..."
description: "Requirments differs from business to business and Magento never failed. Recently one of our customers wanted to create one customer group & when he adds any ..."
keywords: "General, How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Create Attribute Set Programmatically &#038; Assign Attributes to It in Magento 2"
    url: "https://magecomp.com/blog/how-create-attribute-set-programmatically-assign-attributes-to-it-in-magento-2/"
  - title: "How to Create a Directory Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/create-directory-programmatically-magento-2/"
  - title: "How to Send Magento 2 Generated Dynamic PDF in Email to Customers"
    url: "https://magecomp.com/blog/send-magento-2-generated-dynamic-pdf-in-email-to-customers/"
---

# How to Lock Customer Account if They belong to Specific Customer Group in Magento 2

_Published: September 16, 2019_  
_Author: Dhiren Vasoya_  

![How to lock customer account when it falls into a specific group of Magento 2](https://magecomp.com/blog/wp-content/uploads/2019/09/How-to-lock-customer-account-when-it-falls-into-a-specific-group-of-Magento-2-1024x332.webp)

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.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-6.webp)](https://magecomp.com/magento-malware-removal.html)
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**

```
<?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="VendorModuleObserverCustomerLogin" />
    </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**

```
<?php
namespace VendorExtensionObserver;
use MagentoFrameworkEventObserverInterface;
class CustomerLogin implements ObserverInterface
{
    /**
     * @var MagentoFrameworkAppResponseFactory
     */
    private $responseFactory;
    /**
     * @var MagentoFrameworkUrlInterface
     */
    private $url;
    private $customerSession;
    private $messageManager;

    public function __construct(
        MagentoFrameworkAppResponseFactory $responseFactory,
        MagentoFrameworkUrlInterface $url,
        MagentoCustomerModelSession $customerSession,
        MagentoFrameworkMessageManagerInterface $messageManager
    ) {
        $this->responseFactory = $responseFactory;
        $this->url = $url;
        $this->customerSession= $customerSession;
        $this->messageManager = $messageManager;
    }
    public function execute(MagentoFrameworkEventObserver $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;
        }
    }
}
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

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.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-6.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/lock-customer-account-of-specific-magento-2-customer-group/](https://magecomp.com/blog/lock-customer-account-of-specific-magento-2-customer-group/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-01 18:34:38 UTC_  
