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.
<?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!
Also, please share the tutorial with the Magento community via social media.
Thank you.