We have below code to check user logged in or not.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action

I already have Customer Id. How to check on basis of that?


1) Inject the online customer collection factory in your constructor

public function __construct(
Magento\Customer\Model\ResourceModel\Online\Grid\CollectionFactory $onlineCustomerCollectionFactory
) {
   $this->onlineCustomerCollectionFactory = $this->onlineCustomerCollectionFactory;

2) now create a collection object and apply appropriate filters and load it in your function. Optimize the logic as per your need. I am providing a general code.

public function getOnlineCustomers() {

    $onlineCustomersCollection = $this->onlineCustomerCollectionFactory->create();

     * this will return all the customers that are online including 
     * registered and non-registered. So apply filters as per your
     * your requirement using addFieldToFilter() function on collection
    $onlineCustomers = $onlineCustomerCollection->load()->getItems();
    return $onlineCustomers;
  • Looking for Session')->load(<customer id>) – Jackson May 9 '17 at 7:20
  • I don't suppose magento 2 will allow this. Without the proper credentials how the customer can be loaded? It doesn't make sense and it will be totally unsecure. – Pankaj Bhope May 9 '17 at 7:21
  • Not working on Backend :( – Jackson May 11 '17 at 2:33
  • could you please mention clearly what your requirement is?, as your question is confusing. If you want to get the list of customers in backend which are online on frontend then I can help. – Pankaj Bhope May 11 '17 at 8:10
  • In the backend I want the list of customers which are currently Online on Frontend – Jackson May 11 '17 at 8:14

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.