I am looking for code how the customer can be enabled and disabled using customer Id in Magento 2.

When customer is disabled we should not allow customer to login from front end.

I tried setting is_active column as 0 in Database then also i can able to login from frontend.

I am using below code

  public function execute(){
     $user = $this->customerFactory->create();
        $userId = $this->getRequest()->getParam('user_id', false);
        $status = 0;
      try {     
         }catch (\Exception $e){
           $this->_messageManager->addError(__('Can\'t save user'));

is that can be done pro grammatically?

  • there is little big process for doing this batter use free module for ithttps://github.com/amitshree/magento2-account-approval Aug 9, 2018 at 12:47
  • It is not correct if we set is_active to 0 in customer_entity table? This module is not my requirement Aug 9, 2018 at 12:55
  • it will not work with only set that attribute , you will have to use observer in registration time to check customer approved then login other wise logout it and redirect in same page with not active message same is for login condition you will have to create observer to check customer approved then continue login other wise logout and display message for not logged in like that process you will have to do Aug 9, 2018 at 13:02
  • How to check customer is approved or not using customer Id? Because in my controller i get customer id with drop down value enable or disable, Using id i need to implement that functionality, Aug 9, 2018 at 13:06
  • magento.stackexchange.com/questions/132577/… use this code to load customer by id then get attribute value $is_active=$customer->getCustomAttribute('is_active'); Aug 9, 2018 at 13:15

1 Answer 1


Create customer attribute Is blocked? (can only edit from admin,setby default NO) http://prntscr.com/n2yn78 , http://prntscr.com/n2yq5r then use observer event after login check it's value if it is blocked youcan logged out him and redirect to any page

Use customer_login event observer for customer login action

1) Create 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="Vendor\Module\Observer\CustomerLogin" />

2) Now create observer CustomerLogin.php



namespace Vendor\Module\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();

            $this->messageManager->addErrorMessage(__('You are Blocked'));
            $redirectionUrl = $this->url->getUrl('set-your-redirect-url');

            return $this;

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.