I want to check if a customer is logged-In or logged-out on fronted store, and save that customer status in a custom table and that status update whenever a customer logged-In and logged-out in that table.

enter image description here

  • something like that if a customer is logged in on frontend then the value should be like offline or logged in and if he is logged out of not using frontend store then value should be loggged out , the customer loggin status should be updated whenever we open grid ? Commented Jul 14, 2022 at 12:13

1 Answer 1


1. Add custom field to customer listing


<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
        <column name="online_status" class="Acme\StackExchange\Ui\Component\Listing\Column\CustomerOnline" sortOrder="300">
                <label translate="true">Online Status</label>

2. Create custom Online Column



namespace Acme\StackExchange\Ui\Component\Listing\Column;

use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory as VisitorCollectionFactory;
use Magento\Customer\Model\Visitor;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class CustomerOnline extends Column
    protected Visitor                  $visitorModel;
    protected VisitorCollectionFactory $visitorCollectionFactory;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        Visitor $visitorModel,
        VisitorCollectionFactory $visitorCollectionFactory,
        array $components = [],
        array $data = []
    ) {
        $this->visitorModel             = $visitorModel;
        $this->visitorCollectionFactory = $visitorCollectionFactory;

        parent::__construct($context, $uiComponentFactory, $components, $data);

    protected function getOnlineCustomers(array $customerIds): array
        $collection = $this->visitorCollectionFactory->create();
        $lastDate = gmdate('U') - $this->visitorModel->getOnlineInterval() * 60;
        $collection->addFieldToFilter('last_visit_at', [
            'from' => $collection->getConnection()->formatDate($lastDate),
        $collection->addFieldToFilter('customer_id', [
            'in' => $customerIds,

        $online = [];
        foreach ($collection as $visitor) {
            $online[$visitor->getCustomerId()] = true;

        return $online;

    public function prepareDataSource(array $dataSource): array
        if (isset($dataSource['data']['items'])) {
            $entityIds = [];
            foreach ($dataSource['data']['items'] as $item) {
                $entityIds[] = $item['entity_id'];
            $online = $this->getOnlineCustomers($entityIds);
            foreach ($dataSource['data']['items'] as &$item) {
                $item[$this->getName()] = isset($online[$item['entity_id']]) ? __('Online') : __('Offline');

        return $dataSource;
  • Thanks man, you saved my day. Commented Jul 14, 2022 at 17:57

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.