2 Answers
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.
-
I used this in rest api and it returned me empty array, do you know why? Commented Dec 19, 2018 at 11:49
-
@YomnaMansour, can you post a code snippet here of your rest api ? Commented Dec 20, 2018 at 9:37
-
$visitor = $this->session->getVisitorData(); return $visitor;
Commented Dec 20, 2018 at 10:19 -
@YomnaMansour did you find any solution I am also using API Commented Aug 14, 2023 at 17:50
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" } }