Magento 2.x – How to lock and unlock customers programatically
If you want to lock a customer programatically, you can perform the following steps.
- You can Lock it directly over the database changing the following fields:
- Go to customer_entity table.
- Set failures_num = 10 (something more than 3 which is the max failure number)
- Set ‘lock_expires’ = ‘2029-11-06 10:17:50’ (any datetime with something where you know its going to be fair enough, like 10 years later)
- Go to customer_grid_flat table.
- Set ‘lock_expires’ as well as on customer_entity table.
- If you need to perform the same thing programatically, you can use the following snippet:
123456789101112131415161718192021222324252627282930313233343536373839404142public function __construct(\Magento\Customer\Model\ResourceModel\Customer $customerResourceModel) {$this->_customerResourceModel = $customerResourceModel;}public function yourLockFunction () {>>>$this->_customerResourceModel->getConnection()->update($this->_customerResourceModel->getTable('customer_entity'),['failures_num' => 10,//'first_failure' => $customerSecure->getData('first_failure'),'lock_expires' => '2029-11-06 10:17:50',],$this->_customerResourceModel->getConnection()->quoteInto('entity_id = ?', $customer->getId()));<<<}public function yourUnlockFunction () {>>>$this->_customerResourceModel->getConnection()->update($this->_customerResourceModel->getTable('customer_entity'),['failures_num' => 0,'lock_expires' => null,],$this->_customerResourceModel->getConnection()->quoteInto('entity_id = ?', $customerId));$this->_customerResourceModel->getConnection()->update($this->_customerResourceModel->getTable('customer_grid_flat'),['lock_expires' => null,],$this->_customerResourceModel->getConnection()->quoteInto('entity_id = ?', $customerId));<<<}