2

Customer details are available only on customer page and checkout page. It must be a duplicate task but can someone tell how can we retrieve customer session on category pages.

$context = $om->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

we can check is logged or not but not getting any other detail for logged in user.

2

You can Set and get Customer session by using \Magento\Customer\Model\Session.

First, inject the dependency into your class's constructor:

protected $customerSession;

public function __construct(
    ...
    \Magento\Customer\Model\Session $customerSession, 
    ...
){
    ...
    $this->customerSession = $customerSession;
}

Then, call functions like this in your code:

$this->customerSession->getMyValue();

For example, to get customer name, call:

$this->customerSession->getName();
| improve this answer | |
  • its not working on category page – Daniel_12 Sep 3 '18 at 9:22
  • Is customer logged in at the time when you are trying to get the data? – Mohit Kumar Arora Sep 3 '18 at 9:23
0

You can create helper or your custom helper add following code and call these getCustomer();

 <?php 

   protected $_customerSession;

   protected $_customerRepositoryInterface;

   public function __construct(
    ...
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Api\CustomerRepositoryInterface 
     $customerRepositoryInterface,
    ...
  ){
   $this->_customerSession = $customerSession; 
   $this->_customerRepositoryInterface = $customerRepositoryInterface;
  }

  public function getCustomerId()
  {
    //return current customer ID
    return $this->_customerSession->getId();
  }

  public function getCustomer()
  {
    //return current customer data

  return $this->_customerRepositoryInterface->getById( 
  $this->getCustomerId());

  }
| 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.