0

Magento 2 How to get customer info on customer/account/resetpasswordpost controller?

I am trying this code -

echo "<pre>";
print_r($this->session->getData());

and getting this response only -

Array
(
    [wishlist_item_count] => 0
    [rp_token] => XH3BujdKNmcJpc1GQbChU8oYQbwTtiYv
    [no_referer] => 1
)

I need to redirect to my custom pages according to customer needs.

For example - If customer group is 1 then redirect to xyz page.

If customer group is 2 then redirect to abc page.

0

You can get customer id in ResetPasswordPost controller by following code:

$this->session->getId();

And if you want customer data then use below code:

$this->session->getCustomerData();

You can load customer by id as below:

protected $_customer;

public function __construct(
    \Magento\Customer\Model\Customer $customer,
    \Magento\Backend\Block\Template\Context $context
)
{
    $this->_customer = $customer;
    parent::__construct($context);
}

 public function getCustomer()
{
    $customerId = '3'; //You customer ID
    $customer = $this->_customer->getCollection()->addAttributeToFilter('entity_id', array('eq' => '3'));
    print_r($customer->getData());//Customer data by customer ID
}
| improve this answer | |
  • $customerId = '3'; //You customer ID how to get this dynamically ? – sumeet bajaj Jun 29 '19 at 13:00
  • $this->session->getId(); it is printing 0 – sumeet bajaj Jun 29 '19 at 13:20
0

Load customer from Id

$customerId = (int)$this->getRequest()->getQuery('id');
$customer = $this->customerRepository->getById($customerId);
if($customer->getGroupId()==2){
    $this->_redirect('customer/account/');
}else{
    $this->_redirect('customer/account/');
}
| improve this answer | |
  • $customerId = (int)$this->getRequest()->getQuery('id'); it is nothing print – sumeet bajaj Jun 29 '19 at 13:20
  • that is customer id or you can get id from current customer session – MageCoder Jun 29 '19 at 13:39
  • $this->session->getId(); i tried this but it is print 0 – sumeet bajaj Jun 29 '19 at 13:40
0

Finally i solved myself for now using that code -

            $sessionData = $this->session->getData();
            $customRpToken = $sessionData['rp_token'];

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
            $connection = $resource->getConnection();
            $tableName = $resource->getTableName('customer_entity');

            $sql = "SELECT * FROM customer_entity WHERE rp_token = '".$customRpToken."'";
            $result = $connection->fetchAll($sql); 

            $groupId = $result[0]['group_id'];

            if($groupId == 4)
            {
                $resultRedirect->setPath('xyz');
            }
            else
            {
                $resultRedirect->setPath('*/*/login');
            }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.