Magento 2:- How to get customers now online?
Want to get all customers data current login.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityMagento 2:- How to get customers now online?
Want to get all customers data current login.
Yes, You can view this information from the store admin Customer > Now Online
If you would like to retrieve the online customers from your custom programming knowledge from the database, there are two tables store this information. They are:
In this, customer_log table is especially for customer and customer_visitor is for anonymous user/visitor.
Please check these tables and let me know if this helps. Please accept my answer and upvote if you find it useful.
Thank you.
i think you get this via magento admin Logged in admin
Go to Customer > Click on Now online
Got a solution.
public function getUserStatus(
Post $post
) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$entity_id=$post->getEntityId();
$values = $connection->fetchAll('SELECT * FROM `customer_log` LEFT JOIN`customer_visitor` ON `customer_log`.`customer_id` = `customer_visitor`.`customer_id` WHERE customer_log.customer_id="'.$entity_id.'" ORDER BY `customer_visitor`.`last_visit_at` DESC');
if($values){
$this->lastLoginTime = $values[0]['last_login_at'];
$this->lastLogoutTime = $values[0]['last_logout_at'];
$this->lastVisitTime = $values[0]['last_visit_at'];
}
if (!$this->lastLoginTime) {
return false;
}
if ($this->lastLogoutTime && strtotime( $this->lastLogoutTime) > strtotime( $this->lastLoginTime)) {
return false;
}
// Predefined interval has passed since customer's last activity.
$interval = $this->getOnlineMinutesInterval();
$currentTimestamp = (new \DateTime())->getTimestamp();
if ($this->lastVisitTime && $currentTimestamp - strtotime($this->lastVisitTime) > $interval * 60) {
return false;
}
return True;
}
template page
<?php if($block->getUserStatus($post)) { ?>
<div class="active-user">online</div>
<?php }?>