In this tutorial, Today I will explain to you how to check if customer account is confirmed or not in Magento 2. Sometimes, you need to check programmatically that customer account is confirmed or not. For that you need to inject \Magento\Customer\Api\AccountManagementInterface this class.
You may also like this :
- How to add new customer address by REST API in Magento 2
- Magento 2 : Get Default Billing and Shipping Address by Customer ID
Let’s assume that you have created simple module. After that, You can add this below code in app/code/RH/Helloworld/Block/Helloworld.php block file and paste the below code :
<?php
/**
* Created By : Rohan Hapani
*/
namespace RH\Helloworld\Block;
class Helloworld extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Customer\Api\AccountManagementInterface
*/
protected $accountManagement;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
array $data = []
) {
$this->accountManagement = $accountManagement;
parent::__construct($context, $data);
}
/**
* Check account confired or not
*
* @param int $customerId
* @return string
*/
public function checkAccountConfired($customerId)
{
return $this->accountManagement->getConfirmationStatus($customerId);
}
}
Now, you can use checkAccountConfired($customerId) function in your template file or any other file. You need to pass customer id when use this function.
You can use this above code in any other files also based on your requirements.
In last, You will receive output like :
- account_confirmation_required
- account_confirmed
- account_confirmation_not_required
That’s it !!!
I hope this blog is easy to understand about how to check if customer account is confirmed or not in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.
Stay Safe and Stay Connected !!