0

I have to write code in Magento 2 model class's method with a condition that

if (the customer is loggedin){
    //fetch customer detail
}else{
    // else do something else
}

I followed many posts from this site and other sites but nothing worked for me.

Method 1: Fetch details from your customer session in your model class method.

File: app/code/Your_Vendor/Your_Module/Model/Product.php

<?php
namespace Your_Vendor\Your_Module\Model;
use Your_Vendor\Your_Module\Api\ProductInterface;

class Product extends \Magento\Framework\Model\AbstractModel implements ProductInterface{
    public function __construct(
        ....
        \Magento\Customer\Model\Session $_customerSession
    ){
        ....
        $this->_customerSession = $_customerSession;
    }

    public function checkCustomer(){
        /*
        //The below did't work
        if ($this->_customerSession->isLoggedIn()) {
            $data = ' Customer is logged in ';
        } else {
            $data = ' Customer is not logged in';
        }
        */


        /*
        //Then I tried the below. But it also did't work.
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if($customerSession->isLoggedIn()) {
            $data =  'Customer is logged in ';
            //$customerSession->getCustomer()->getName(); //Loggedin customer Name
            //$customerSession->getCustomer()->getEmail(); //Loggedin customer Email
            //print_r($customerSession->getCustomer()->getData());//Loggedin customer detail data
        } else {
            $data = 'Customer is not logged in';
        }
        */


        /*
        //Then I tried the below. But it also did't work.
        $om = \Magento\Framework\App\ObjectManager::getInstance();
        $context = $om->get('Magento\Framework\App\Http\Context');
        $this->_logger->info(json_encode($context));
        $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        if($isLoggedIn){
            $data = 'Yes Customer loggedin';
            //print_r($context->getData());
        }else{
            $data = 'No Customer loggedin';
        }       
        */
    }
}
?>

But the above didn't work

Method 2: Wrote an observer for customer_login event. Now set logged in customer detail in your session. Then in your model class, check your session whether has set or not. If set then retrieve detail from the session and do your task if not then do something else.

<?php
namespace Your_Vendor\Your_Module\Observer;

use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;

class CustomerAfterLogin implements ObserverInterface {
    public function __construct(
        \Psr\Log\LoggerInterface $_logger,
    ){
        $this->_logger = $_logger;
    }

    public function execute(Observer $observer) {
        $customer = $observer->getCustomer();
        $res = $customer->getId();
        $this->_logger->info($res);$this->_logger->debug($res);// customer id is logged successfully.
        // Can anyone share the code to set logged in customer detail in the session here?
    }
}


// Code to fetch that session detail in the model?
0

Try This its work

protected $isCustomerLoggedIn;
public function __construct(
    \Magento\Framework\App\Http\Context $httpContext
) {
    $this->isCustomerLoggedIn = $httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}
public function getcustomerSession()
    {
        return $this->isCustomerLoggedIn;
    }
  • Thanks for your response. But it didn't work for me. Though the customer is logged in, but $this->isCustomerLoggedIn gives NULL. – Ipsita Rout Jul 28 '16 at 12:48

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.