-1

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
    $customerSession->getCustomerId();
}

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

1 Answer 1

2

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;
}
6
  • Looking for Session')->load(<customer id>)
    – Jackson
    May 9, 2017 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. May 9, 2017 at 7:21
  • Not working on Backend :(
    – Jackson
    May 11, 2017 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. May 11, 2017 at 8:10
  • In the backend I want the list of customers which are currently Online on Frontend
    – Jackson
    May 11, 2017 at 8:14

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.