cancel
Showing results for 
Search instead for 
Did you mean: 

Disable a customer account

SOLVED

Disable a customer account

We are trying to find a way to disable a customers account without deleting it. We are running open source 2.2.6. We don’t want to be able to anything more than just make it so that select customers can not log into their accounts.

We have made a new customer attribute but we are stuck in what to do to trigger magento to not allow the login.

We recently migrated from 1.9.x and are still figuring things out.

Management wants “to ensure that customer can not continue to place orders” after some sort of violation or problem. We know customers can simply create a new account with a new email address but this is more of a “we need to make management feel better about having a functional thing they can do”.
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Disable a customer account

Hello @bcadevteam

 

You can manage the customer login using a custom attribute.

Suppose there is a custom attribute code "allow_login" with the options of "Yes (1)" and "No (0)"

Now you can create a plugin to check if the customer is allowed to login or not.

Declare the plugin in your custom module.

 

app/code/Vendor/Module/etc/frontend/di.xml

 

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	
    <type name="Magento\Customer\Controller\Account\LoginPost">
        <plugin name="check_customer_login_permission" type="Vendor\Module\Plugin\CheckCustomerLoginPermission" />
    </type>

</config>

Create your plugin class

 

app/code/Vendor/Module/Plugin/CheckCustomerLoginPermission.php

<?php

namespace Vendor\Module\Plugin;

class CheckCustomerLoginPermission
{	
	protected $_customerRepository;

	public function __construct(
		\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
	) {
		$this->_customerRepository = $customerRepository;
	}

	public function beforeExecute(\Magento\Customer\Controller\Account\LoginPost $subject)
	{
		$loginData = $subject->getRequest()->getPost('login');
		$email = $loginData['username'];

		$customer = $this->_customerRepository->get($email);

		if ($customer->getId()) {
			$customerData = $customer->__toArray();
			$isCustomerAllowedToLogin = $customerData['custom_attributes']['allow_login']['value'];
			if ($isCustomerAllowedToLogin == 0) {
				// do whatever you want when the customer is not allowed to login
				// for example:
				// return the customer to login page and show an error message that you are now allowed to login.
			}
		}
	}
}

 

 

 

 

If you find my answer useful, Please click Kudos & Accept as Solution.

View solution in original post

1 REPLY 1

Re: Disable a customer account

Hello @bcadevteam

 

You can manage the customer login using a custom attribute.

Suppose there is a custom attribute code "allow_login" with the options of "Yes (1)" and "No (0)"

Now you can create a plugin to check if the customer is allowed to login or not.

Declare the plugin in your custom module.

 

app/code/Vendor/Module/etc/frontend/di.xml

 

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	
    <type name="Magento\Customer\Controller\Account\LoginPost">
        <plugin name="check_customer_login_permission" type="Vendor\Module\Plugin\CheckCustomerLoginPermission" />
    </type>

</config>

Create your plugin class

 

app/code/Vendor/Module/Plugin/CheckCustomerLoginPermission.php

<?php

namespace Vendor\Module\Plugin;

class CheckCustomerLoginPermission
{	
	protected $_customerRepository;

	public function __construct(
		\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
	) {
		$this->_customerRepository = $customerRepository;
	}

	public function beforeExecute(\Magento\Customer\Controller\Account\LoginPost $subject)
	{
		$loginData = $subject->getRequest()->getPost('login');
		$email = $loginData['username'];

		$customer = $this->_customerRepository->get($email);

		if ($customer->getId()) {
			$customerData = $customer->__toArray();
			$isCustomerAllowedToLogin = $customerData['custom_attributes']['allow_login']['value'];
			if ($isCustomerAllowedToLogin == 0) {
				// do whatever you want when the customer is not allowed to login
				// for example:
				// return the customer to login page and show an error message that you are now allowed to login.
			}
		}
	}
}

 

 

 

 

If you find my answer useful, Please click Kudos & Accept as Solution.