I want to display value based on customer if a customer is online on fronted, then it shows online in custom column in customer grid and if it is offline or logged out then shows offline in same custom column in customer grid
in below picture in Customer Online status it should display online is a customer is online and offline if a customer is offline

- 462
- 2
- 5
- 30
-
any solution or hint is appreciated, thanks in advance? – Afzal Arshad Jul 14 '22 at 12:14
-
for which magento version. – Divyarajsinh Barad Jul 21 '22 at 09:10
-
@DivyarajsinhBarad i already find the solution to this question, but i have another question, will you take a look? – Afzal Arshad Jul 21 '22 at 10:40
-
sure please tell me. – Divyarajsinh Barad Jul 21 '22 at 10:43
-
@DivyarajsinhBarad this is my question link, https://magento.stackexchange.com/questions/358076/how-to-add-sort-by-size-options-in-back-end-grid-as-shown-in-pictures – Afzal Arshad Jul 21 '22 at 12:21
-
can you please share what you do for customer grid online show section – Divyarajsinh Barad Jul 22 '22 at 07:12
-
@DivyarajsinhBarad i answered below – Afzal Arshad Jul 22 '22 at 07:19
1 Answers
\Vendor\Module\Setup\Patch\Data\CustomerOnlineStatus.php
<?php
declare(strict_types=1);
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class CustomerOnlineStatus implements DataPatchInterface
{
/**
* @var CustomerSetupFactory
*/
protected CustomerSetupFactory $customerSetupFactory;
/**
* @var ModuleDataSetupInterface
*/
protected ModuleDataSetupInterface $setup;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param ModuleDataSetupInterface $setup
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
ModuleDataSetupInterface $setup
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->setup = $setup;
}
/**
* @inheritDoc
*/
public static function getDependencies(): array
{
return [];
}
/**
* @inheritDoc
*/
public function getAliases(): array
{
return [];
}
/**
* @inheritDoc
* @throws \Exception
*/
public function apply()
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->setup]);
try {
$customerSetup->addAttribute(
Customer::ENTITY,
'customer_online_status',
[
'type' => 'text',
'backend' => '',
'label' => 'Customer Online Status Attribute',
'input' => 'text',
'source' => '',
'visible' => true,
'required' => false,
'default' => '',
'frontend' => '',
'unique' => false,
'note' => '',
]
);
} catch (LocalizedException|\Zend_Validate_Exception $e) {
}
$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,
'customer_online_status'
);
$attribute->setData('used_in_forms', ['customer_account_edit'])
->setData('is_used_for_customer_segment', true)
->setData('is_system', 0)
->setData('is_user_defined', 1)
->setData('is_visible', 1)
->setData('sort_order', 110)
->setData("is_used_in_grid", 1)
->setData("is_visible_in_grid", 1)
->setData("is_filterable_in_grid", 1)
->setData("is_searchable_in_grid", 1);
$attribute->save();
}
}
\Vendor\Module\Ui\Component\Listing\Column\CustomerOnlineStatus.php
<?php
declare(strict_types=1);
namespace Vendor\Module\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory as VisitorCollectionFactory;
use Magento\Customer\Model\Visitor;
class CustomerOnlineStatus extends Column
{
/**
* @var Visitor
/
protected Visitor $visitorModel;
/*
* @var VisitorCollectionFactory
*/
protected VisitorCollectionFactory $visitorCollectionFactory;
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param Visitor $visitorModel
* @param VisitorCollectionFactory $visitorCollectionFactory
* @param array $components
* @param array $data
*/
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);
}
/**
* @param array $dataSource
* @return array
*/
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']]) ? __('Logged In') : __('Offline');
}
}
return $dataSource;
}
/**
* @param array $customerIds
* @return array
*/
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;
}
}
Vendor\Module/view/adminhtml/ui_component/customer_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
<column name="customer_online_status" class="Vendor\Module\Ui\Component\Listing\Column\CustomerOnlineStatus" sortOrder="500">
<settings>
<label translate="true">Customer Online Status</label>
</settings>
</column>
</columns>
</listing>
- 462
- 2
- 5
- 30
-
@ Pyaray Afzel , this code is not working when i logged in for same customer in public window and also in private window , it is consider diff for too – Divyarajsinh Barad Jul 22 '22 at 08:24
-
-
-
-
-
https://magento.stackexchange.com/questions/358076/how-to-add-sort-options-in-back-end-grid-as-shown-in-pictures – Afzal Arshad Jul 22 '22 at 12:51
-
did you mean , you want to add those your custom attributes in drop-down and work filter according to them ? – Divyarajsinh Barad Jul 22 '22 at 12:54
-
From this you will get an idea https://magento.stackexchange.com/questions/358034/magento2-4-how-to-sort-product-by-best-selling-and-best-reviewed-produc – Afzal Arshad Jul 22 '22 at 13:11
-