0

I have tried this but this is giving empty log file. What should I do to get output for this:

  public function isAdmin() {
        $writer = new \Zend\Log\Writer\Stream( BP . '/var/log/myfile.log' );
        $logger = new \Zend\Log\Logger();
        $logger->addWriter( $writer );
        $this->_coreSession->start();
        $logger->info( 'aaaa', $this->_coreSession->getMessage() );
    }
0

You can use class Magento\Customer\Model\Session which will give you customer session object.

For example:

protected $customerSession;

public function _construct(
    ...
    \Magento\Customer\Model\Session $customerSession
    ...
) {
    ...
    $this->customerSession = $customerSession;
    ...
}
public function getName()  {
  return $this->customerSession->getCustomer()->getName();
} 

In above example, you will get customer's name from customer session if you can getName() function.

Now, to get a custom session value, you must set it first (if it is not already set somewhere else). Here is how you can set it.

$testValue = 'this is test';
$this->customerSession->setTestValue($testValue);

Now, you have a custom session value available in customer session. You can get this value from your module using below code:

$testValue = $this->customerSession->getTestValue();
2
  • but i need admin session on frontend customlly
    – h.khalid
    Oct 13 '20 at 7:55
  • I have also added code on how to set/get custom value on customer session.
    – amitshree
    Oct 14 '20 at 9:54

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.