1

Magento 2:- How to get customers now online?

Want to get all customers data current login.

0

i think you get this via magento admin Logged in admin

Go to Customer > Click on Now online

  • thanks for the reply, Visiter Show not current login customer – Nitesh Kumar Singh Jun 6 '18 at 8:17
0

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:

  • customer_visitor
  • customer_log

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 glad you had it working. Please upvote my answer if it helped you in any way. Otherwise, just let it go. – Gideon Jun 12 '18 at 12:07
0

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 }?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.