0

How i can get Current Visitor id Form visitor collection? enter image description here

2 Answers 2

1

try this code in your class -

protected $session;

 public function __construct(
  ...
  \Magento\Framework\Session\SessionManagerInterface $session,
  ...
 )
 {
   ...
   $this->session = $session;
   ...
 }

 public function yourFunction()
 {
    $visitor = $this->session->getVisitorData();
    print_r($visitor);
 }

Hope it will help.

4
  • I used this in rest api and it returned me empty array, do you know why? Dec 19, 2018 at 11:49
  • @YomnaMansour, can you post a code snippet here of your rest api ? Dec 20, 2018 at 9:37
  • $visitor = $this->session->getVisitorData(); return $visitor; Dec 20, 2018 at 10:19
  • @YomnaMansour did you find any solution I am also using API Aug 14 at 17:50
0

The visitor_id is stored in core/session object.

You can reach this way.

Of course ObjectManager is only for testing purposes. You must code properly via Block, Model, etc, as you prefer.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$session = $objectManager->get('\Magento\Framework\Session\SessionManager');
var_dump($session->getData());

Will return something like:

array(2) { ["_form_key"]=> string(16) "qvg5frewPzNpfD1I" ["visitor_data"]=> array(3) { ["last_visit_at"]=> string(19) "2022-06-17 22:44:05" ["session_id"]=> string(26) "vltiv89hgg77v0nlho51v5eevn" ["visitor_id"]=> string(7) "3890947" } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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