Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
I am busying with some big projects. So I didn’t make the new Magento 2 tutorial videos for more than one past year. This week, I have a little free time, so I will share with everybody some best practices. Today’s lesson, How to lock a customer account in Magento 2.
When you work on a project, the client requires you to block a customer account from the backend, the client means preventing them from using their account. Then the customer can’t be able to log in to their account. By default, Magento hasn’t this functionality. You don’t know how to complete this task, you are searching for a good solution. Today I show you the best codes to complete your task.
We have the scenario:
We will create a new customer attribute called “Blocked”, it will have two options (Yes/No). If this attribute is set to “Yes” when a customer tries to log in, they will get an inform similar to this “Your account is blocked for the security reason, please contact us for details.”
So what will we do in this practice?
1. We will create a new module called PHPCuong_CustomerLogin
2. We will create a new customer attribute called “Blocked” with the type is boolean
3. We will use the Plugins feature (Before) in Magento 2 for modifying the behavior of a public function called “execute” in the class MagentoCustomerControllerAccountLoginPost
Okie, let’s go.
Let’s complete this practice you need to follow steps by step:
Step 1: Declaring the new module called PHPCuong_CustomerLogin
– Create the namespace PHPCuong in the path appcode
– Create the module named CustomerLogin in the path appcodePHPCuong
– Create the file named registration.php in the path appcodePHPCuongCustomerLogin
– Create the file named module.xml in the path appcodePHPCuongCustomerLoginetc
End of step #1, I have been completed the codes to create the new module called PHPCuong_CustomerLogin.
Step 2: Adding a new customer attribute called “Blocked”
– Create the new file named InstallData.php in the path appcodePHPCuongCustomerLoginSetup
Step 3: Using the Plugins in Magento 2
– Create the new file named di.xml in the path appcodePHPCuongCustomerLoginetcfrontend
– When a customer logs in, Magento will call the method execute() in the class MagentoCustomerControllerAccountLoginPost for checking.
We will use the method beforeExecute() for checking the value of “Blocked” customer attribute.
– Create the new file named LoginPost.php in the path appcodePHPCuongCustomerLoginPluginCustomerControllerAccount
Step 4: Display the “Blocked” customer attribute in the customer form
– Create the new file named customer_form.xml in the path appcodePHPCuongCustomerLoginviewadminhtmlui_component
Step 5: Test and see the results
1. Run the following command lines:
php bin/magento setup:upgrade –keep-generated
– You can see the new module is installed in the table called setup_module
– You can see the new customer attribute is added in the table named eav_attribute
– I am using the developer mode, so I don’t need to run the command lines such as: “compile” or “deploy static content”
– I can log into my account now.
2. Go to the backend, then set the block attribute of a customer to Yes
3. Go to the storefront, then try to log in
Thank you for watching my video. If you have any questions about this practice please feel free to leave a comment below.
Don’t forget to like, comment, share and subscribe to my channel for getting the latest video.
Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.