0

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 enter image description here

Afzal Arshad
  • 462
  • 2
  • 5
  • 30

1 Answers1

0

\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-&gt;customerSetupFactory = $customerSetupFactory;
    $this-&gt;setup = $setup;
}

/**
 * @inheritDoc
 */
public static function getDependencies(): array
{
    return [];
}

/**
 * @inheritDoc
 */
public function getAliases(): array
{
    return [];
}

/**
 * @inheritDoc
 * @throws \Exception
 */
public function apply()
{
    $customerSetup = $this-&gt;customerSetupFactory-&gt;create(['setup' =&gt; $this-&gt;setup]);

    try {
        $customerSetup-&gt;addAttribute(
            Customer::ENTITY,
            'customer_online_status',
            [
                'type' =&gt; 'text',
                'backend' =&gt; '',
                'label' =&gt; 'Customer Online Status Attribute',
                'input' =&gt; 'text',
                'source' =&gt; '',
                'visible' =&gt; true,
                'required' =&gt; false,
                'default' =&gt; '',
                'frontend' =&gt; '',
                'unique' =&gt; false,
                'note' =&gt; '',
            ]
        );
    } catch (LocalizedException|\Zend_Validate_Exception $e) {
    }
    $attribute = $customerSetup-&gt;getEavConfig()-&gt;getAttribute(
        Customer::ENTITY,
        'customer_online_status'
    );
    $attribute-&gt;setData('used_in_forms', ['customer_account_edit'])
        -&gt;setData('is_used_for_customer_segment', true)
        -&gt;setData('is_system', 0)
        -&gt;setData('is_user_defined', 1)
        -&gt;setData('is_visible', 1)
        -&gt;setData('sort_order', 110)
        -&gt;setData(&quot;is_used_in_grid&quot;, 1)
        -&gt;setData(&quot;is_visible_in_grid&quot;, 1)
        -&gt;setData(&quot;is_filterable_in_grid&quot;, 1)
        -&gt;setData(&quot;is_searchable_in_grid&quot;, 1);
    $attribute-&gt;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-&gt;visitorModel = $visitorModel;
    $this-&gt;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-&gt;getOnlineCustomers($entityIds);
        foreach ($dataSource['data']['items'] as &amp;$item) {
            $item[$this-&gt;getName()] = isset($online[$item['entity_id']]) ? __('Logged In') : __('Offline');
        }
    }
    return $dataSource;
}

/**
 * @param array $customerIds
 * @return array
 */
protected function getOnlineCustomers(array $customerIds): array
{
    $collection = $this-&gt;visitorCollectionFactory-&gt;create();
    $lastDate = gmdate('U') - $this-&gt;visitorModel-&gt;getOnlineInterval() * 60;
    $collection-&gt;addFieldToFilter('last_visit_at', [
        'from' =&gt; $collection-&gt;getConnection()-&gt;formatDate($lastDate),
    ]);
    $collection-&gt;addFieldToFilter('customer_id', [
        'in' =&gt; $customerIds,
    ]);

    $online = [];
    foreach ($collection as $visitor) {
        $online[$visitor-&gt;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>
Afzal Arshad
  • 462
  • 2
  • 5
  • 30