3

below is my block file code

class View extends \Magento\Framework\View\Element\Template 
{ 
protected $_registry;
protected $_customerSession; 
protected $_customer; 
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Customer\Model\Session $customerSession, 
    \Magento\Customer\Model\Customer $customer      
) {
    $this->_registry = $registry;
    $this->_customerSession = $customerSession; 
    $this->_customer = $customer; 
    parent::__construct($context);
}

public function getUser()
{
    if (!$this->_loadValidUser()) {
        return;
    }
    return $this->registry->registry('current_user');
}   

protected function _loadValidUser($userId = null)
{
     $userId = (int) $this->getRequest()->getParam('user_id');
    try {
        $user = $this->_customer->load($userId);
        $this->registry->register('current_user', $user);            
        return $user;
    }catch (Exception $e) {
       print_r($e->getMessage());
         return false;
    }

}

}

What is the mistake in above construction? I am getting the error like

PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Framework\View\Element\Template

1

Use this code in your block file :

protected $_customerSession;

public function __construct(
    ......
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\SessionFactory $customerSession,
    array $data = []
    ......
)
{
    ......
    $this->_customerSession = $customerSession->create();
    parent::__construct($context, $data);
    ......
}

public function getCustomerData() {
    if ($this->_customerSession->isLoggedIn()) {
        return $this->_customerSession->getCustomerData();
    }
    return false;
}

and display in your phtml file :

$customerData = $block->getCustomerData();
echo "<pre/>";
print_r($customerData);
  • I am getting userId in param and i need to register that user instead of current logged in customer. – jafar pinjar Aug 7 '18 at 5:11
  • Sorry ? I'm not getting exactly what you want to explain. – Rohan Hapani Aug 7 '18 at 5:41
  • can you explain in some more detail? – Rohan Hapani Aug 7 '18 at 5:41
  • In my code i used $userId = (int) $this->getRequest()->getParam('user_id'); to get parameter. from the parameter i am registering customer data. parameter is customer id, – jafar pinjar Aug 7 '18 at 5:44
  • Try to use this code $this->_registry->register('current_user', $user); in _loadValidUser function. – Rohan Hapani Aug 7 '18 at 5:47
3

Try this:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Model\Customer $customer       
){
    $this->_registry = $registry;
    $this->_customerSession = $customerSession;
    $this->_customer = $customer;
    parent::__construct($context);
}

after that changes please run below commands

php bin/magento cache:flush and php bin/magento setup:di:compile

  • hi kunj, used above code no error but i am not getting current customer data. – jafar pinjar Aug 6 '18 at 12:22

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.