0

I have created the custom link in customer account left navigation. I am successfully created it.

I am trying to get current customer Data in template file. I am getting 500 server error. Below is the code i used in my block file

 class Lists extends \Magento\Framework\View\Element\Template
 {
  protected $helperData;
  public function __construct(
    \[vendor]\[Module]\Helper\Data $helperData,
  ) {
        $this->helperData = $helperData;
  }
  public function _prepareLayout()
   {
    return parent::_prepareLayout();
   }

  public function currentCustomer(){
    return $this->helperData->getCurrentCustomer();
  }

}

Used like below in template file.

$customerObj = $this->currentCustomer();
echo '<pre>';
print_r($customerObj);die;

Helper file is like below.

  class Data extends \Magento\Framework\App\Helper\AbstractHelper
   {
    protected $customerSession;
    protected $customerRepository;
     public function __construct(
    \Magento\Framework\App\Helper\Context $context,        
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository

) {
    $this->customerSession = $customerSession;
    $this->customerRepository = $customerRepository;        
    parent::__construct($context);
}
    public function getCurrentCustomer() {
      $customer = $this->customerSession->getCustomer();
      $customerId = $customer->getId();
      $customer = $this->customerRepository->getById($customerId);

      return $customer;
    }
 }

Please anyone look the code and suggest me.Thanks

1
  • Did you checked it..? updated code. Commented Aug 3, 2018 at 10:41

2 Answers 2

1

Try to use this code :

/**
 * @var \Magento\Customer\Model\Session
 * @var \Magento\Customer\Model\Customer
 */
protected $_customerSession;
protected $_customer;
/**
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Customer\Model\Customer $customer
 */
public function __construct(
    ...........
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Model\Customer $customer,
    ...........
){
    ...........
    $this->_customerSession = $customerSession;
    $this->_customer = $customer;
}

public function getCurrentCustomer()
{
    if ($this->_customerSession->isLoggedIn()) {
        $customer_id = $this->_customerSession->getCustomer()->getId();
        $customerData = $this->_customer->load($customer_id);

    }
}
10
  • Hello, where to add this code? in my Block file? can we use execute in block file? Commented Aug 3, 2018 at 7:19
  • Yes. you can use this code in your block. Add execute() function code in that function where you want to get. Commented Aug 3, 2018 at 7:24
  • Already the above code i am using in helper file method is getCurrentCustomer, that function i am calling in Block file, why i need to construct here again? Commented Aug 3, 2018 at 7:26
  • You just need to add code From execute funtion to getCurrentCustomer function and inject \Magento\Customer\Model\Customer in your construct. Then, remove generate folder and clean cache. Commented Aug 3, 2018 at 7:29
  • I did, but same again, Please read my Block file, i am declaring a function to get current customer. Commented Aug 3, 2018 at 7:31
0

Follow below steps: 1. Change constructor of block file(Lists) like below:

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \[vendor]\[Module]\Helper\Data $helperData,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->helperData = $helperData;
    }

2. Clear var/di/* and generated/*. Flush your cache and test again.

2
  • hello, i tried this code. Its not working Commented Aug 3, 2018 at 7:18
  • Check var/log/system.log file for any error. If there is not any error in log file then put error_reporting(E_ALL) ;ini_set('display_errors',1); in magento's root index.php file.
    – mighty_hk
    Commented Aug 3, 2018 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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