We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Step :1 Creat a custom attribute of boolean type which will be used to block customer login.

    <?php
    
    declare(strict_types=1);
    
    namespace VendorModuleSetupPatchData;
    
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetup;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSet;
    use MagentoEavModelEntityAttributeSetFactory;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoFrameworkSetupPatchDataPatchInterface;
    use MagentoFrameworkSetupPatchPatchRevertableInterface;
    
    class AddBlockedCustomerAttribute implements DataPatchInterface, PatchRevertableInterface
    {
    
        /**
         * @var ModuleDataSetupInterface
         */
        private $moduleDataSetup;
        /**
         * @var CustomerSetup
         */
        private $customerSetupFactory;
        /**
         * @var SetFactory
         */
        private $attributeSetFactory;
    
        /**
         * Constructor
         *
         * @param ModuleDataSetupInterface $moduleDataSetup
         * @param CustomerSetupFactory $customerSetupFactory
         * @param SetFactory $attributeSetFactory
         */
        public function __construct(
            ModuleDataSetupInterface $moduleDataSetup,
            CustomerSetupFactory $customerSetupFactory,
            SetFactory $attributeSetFactory
        ) {
            $this->moduleDataSetup = $moduleDataSetup;
            $this->customerSetupFactory = $customerSetupFactory;
            $this->attributeSetFactory = $attributeSetFactory;
        }
    
        /**
         * {@inheritdoc}
         */
        public function apply()
        {
            $this->moduleDataSetup->getConnection()->startSetup();
            /** @var CustomerSetup $customerSetup */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
            $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();
            
            /** @var $attributeSet Set */
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
            
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'blocked',
                [
                    'label' => 'Blocked',
                    'input' => 'boolean',
                    'type' => 'static',
                    'source' => '',
                    'required' => false,
                    'position' => 333,
                    'visible' => true,
                    'system' => false,
                    'is_used_in_grid' => true,
                    'is_visible_in_grid' => true,
                    'is_filterable_in_grid' => true,
                    'is_searchable_in_grid' => false,
                    'backend' => ''
                ]
            );
            
            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'blocked');
            $attribute->addData([
                'used_in_forms' => [
                    'adminhtml_customer',
                    'adminhtml_checkout',
                    'customer_account_create',
                    'customer_account_edit'
                ]
            ]);
            $attribute->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId
            
            ]);
            $attribute->save();
    
            $this->moduleDataSetup->getConnection()->endSetup();
        }
    
        public function revert()
        {
            $this->moduleDataSetup->getConnection()->startSetup();
            /** @var CustomerSetup $customerSetup */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
            $customerSetup->removeAttribute(MagentoCustomerModelCustomer::ENTITY, 'blocked');
    
            $this->moduleDataSetup->getConnection()->endSetup();
        }
    
        /**
         * {@inheritdoc}
         */
        public function getAliases()
        {
            return [];
        }
    
        /**
         * {@inheritdoc}
         */
        public static function getDependencies()
        {
            return [
            
            ];
        }
    }
    
    Step 2: Observer to restrict customer from login depending upon the custom attribute created in Step 1.
    <?php
    
    declare(strict_types=1);
    
    namespace VendorModuleObserverFrontendCustomer;
    
    class Login implements MagentoFrameworkEventObserverInterface
    {
    
        /**
         * @var MagentoFrameworkAppResponseFactory
         */
        private $responseFactory;
    
        /**
         * @var MagentoFrameworkUrlInterface
         */
    
        private $url;
    
        /**
         * @var MagentoCustomerModelSessionFactory
         */
    
        private $customerSession;
    
        /**
         * @var MagentoFrameworkMessageManagerInterface
         */
    
        private $messageManager;
    
        /** 
         * @var MagentoCustomerApiCustomerRepositoryInterface 
         */
        private $customerRepositoryInterface;
    
        /**
         * Observer Constructor
         *
         * @param MagentoFrameworkAppResponseFactory $responseFactory
         * @param MagentoFrameworkUrlInterface $url
         * @param MagentoCustomerModelSession $customerSession
         * @param MagentoFrameworkMessageManagerInterface $messageManager
         * @param MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
         */
        public function __construct(
            MagentoFrameworkAppResponseFactory $responseFactory,
            MagentoFrameworkUrlInterface $url,
            MagentoCustomerModelSession $customerSession,
            MagentoFrameworkMessageManagerInterface $messageManager,
            MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
        ) {
            $this->responseFactory = $responseFactory;
            $this->url = $url;
            $this->customerSession= $customerSession;
            $this->messageManager = $messageManager;
            $this->customerRepositoryInterface = $customerRepositoryInterface;
        }
        /**
         * Execute observer
         *
         * @param MagentoFrameworkEventObserver $observer
         * @return void
         */
        public function execute(
            MagentoFrameworkEventObserver $observer
        ) {
            $customer = $observer->getEvent()->getCustomer();
            $blocked = $customer->getData('blocked');       
            if($blocked==1){
                $this->customerSession->logout();
                $this->messageManager->addErrorMessage(__('Your account has been blocked'));
                $redirectionUrl = $this->url->getUrl('customer/account/login');
                $this->responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
                return $this;
            }
        }
    }

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote