Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerLoginObserver.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  protected $_eventFactory;
19 
24 
29 
33  protected $_customerSession;
34 
38  protected $_customerVisitor;
39 
47  public function __construct(
48  \Magento\Reports\Model\EventFactory $event,
49  \Magento\Reports\Model\Product\Index\ComparedFactory $productCompFactory,
50  \Magento\Reports\Model\Product\Index\ViewedFactory $productIndexFactory,
51  \Magento\Customer\Model\Session $customerSession,
52  \Magento\Customer\Model\Visitor $customerVisitor
53  ) {
54  $this->_eventFactory = $event;
55  $this->_productCompFactory = $productCompFactory;
56  $this->_productIndexFactory = $productIndexFactory;
57  $this->_customerSession = $customerSession;
58  $this->_customerVisitor = $customerVisitor;
59  }
60 
68  public function execute(\Magento\Framework\Event\Observer $observer)
69  {
70  if (!$this->_customerSession->isLoggedIn()) {
71  return $this;
72  }
73 
74  $visitorId = $this->_customerVisitor->getId();
75  $customerId = $this->_customerSession->getCustomerId();
76  $eventModel = $this->_eventFactory->create();
77  $eventModel->updateCustomerType($visitorId, $customerId);
78 
79  $this->_productCompFactory->create()->updateCustomerFromVisitor()->calculate();
80  $this->_productIndexFactory->create()->updateCustomerFromVisitor()->calculate();
81 
82  return $this;
83  }
84 }
__construct(\Magento\Reports\Model\EventFactory $event, \Magento\Reports\Model\Product\Index\ComparedFactory $productCompFactory, \Magento\Reports\Model\Product\Index\ViewedFactory $productIndexFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Visitor $customerVisitor)
execute(\Magento\Framework\Event\Observer $observer)