How to Programmatically Check if Customer Account is Confirmed or not in Magento 2
Magento 2 store owners tend to collect customers’ data for various purposes such as marketing, understanding customers’ behaviour and shopping pattern, or simply offer a better user experience.
Apart from collecting customers’ data is to collect their Email ID while registering with Magento 2 store. Every store owner usually confirms if the given Email ID is correct or not.
However, when you programmatically login customer without a password, in that case even if the email is not confirmed, the customer can log in. In this situation, you need to programmatically check if customer account is confirmed or not in Magento 2.
The below code is the method for the same:
Method to Programmatically Check if Customer Account is Confirmed or not in Magento 2
Use the below code in the helper file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; class Data extends AbstractHelper { protected $accountManagement; public function __construct( Context $context, \Magento\Customer\Api\AccountManagementInterface $accountManagement ) { $this->accountManagement = $accountManagement; parent::__construct($context); } public function isAccountConfirmed($customerId) { return $this->accountManagement->getConfirmationStatus($customerId); } } |
isAccountConfirmed() function returns result as :
- account_confirmation_required
- account_confirmed
- account_confirmation_not_required
That’s all!
Use the above solution and stop the unauthenticated user in your store or else, you could just use the Magento 2 Restrict Fake Registration extension!
Any doubts about the topic can be mentioned in the Comments section below. I’d be happy to help.
Also, please share the tutorial with the Magento community via social media.
Thank you.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
2 Comments
Sir ,
how we can create the custom_order increment id like (abc0000001bbb)?
how we can create the custom_invoice increment id like (abc0000001bbb)?
how we can create the custom_shipment increment id like (abc0000001bbb)?
Hello,
Please have a look at Meetanshi’s Magento 2 Custom Order Number for your requirements.
Thank you.