/** * Update Customer from visitor (Customer logged in) * * @param \Magento\Reports\Model\Product\Index\AbstractIndex $object * @return $this */ public function updateCustomerFromVisitor(\Magento\Reports\Model\Product\Index\AbstractIndex $object) { /** * Do nothing if customer not logged in */ if (!$object->getCustomerId() || !$object->getVisitorId()) { return $this; } $adapter = $this->_getWriteAdapter(); $select = $adapter->select()->from($this->getMainTable())->where('visitor_id = ?', $object->getVisitorId()); $rowSet = $select->query()->fetchAll(); foreach ($rowSet as $row) { /* We need to determine if there are rows with known customer for current product. */ $select = $adapter->select()->from($this->getMainTable())->where('customer_id = ?', $object->getCustomerId())->where('product_id = ?', $row['product_id']); $idx = $adapter->fetchRow($select); if ($idx) { /** * If we are here it means that we have two rows: one with known customer, but second just visitor is set * One row should be updated with customer_id, second should be deleted */ $adapter->delete($this->getMainTable(), ['index_id = ?' => $row['index_id']]); $where = ['index_id = ?' => $idx['index_id']]; $data = ['visitor_id' => $object->getVisitorId(), 'store_id' => $object->getStoreId(), 'added_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)]; } else { $where = ['index_id = ?' => $row['index_id']]; $data = ['customer_id' => $object->getCustomerId(), 'store_id' => $object->getStoreId(), 'added_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)]; } $adapter->update($this->getMainTable(), $data, $where); } return $this; }
/** * Update Customer from visitor (Customer logged in) * * @param \Magento\Reports\Model\Product\Index\AbstractIndex $object * @return $this */ public function updateCustomerFromVisitor(\Magento\Reports\Model\Product\Index\AbstractIndex $object) { /** * Do nothing if customer not logged in */ if (!$object->getCustomerId() || !$object->getVisitorId()) { return $this; } $connection = $this->getConnection(); $select = $connection->select()->from($this->getMainTable())->where('visitor_id = ?', $object->getVisitorId()); $rowSet = $select->query()->fetchAll(); foreach ($rowSet as $row) { /* We need to determine if there are rows with known customer for current product. */ $select = $connection->select()->from($this->getMainTable())->where('customer_id = ?', $object->getCustomerId())->where('product_id = ?', $row['product_id']); $idx = $connection->fetchRow($select); if ($idx) { /** * If we are here it means that we have two rows: one with known customer and second with guest visitor * One row should be updated with customer_id, second should be deleted */ $connection->delete($this->getMainTable(), ['index_id = ?' => $row['index_id']]); $where = ['index_id = ?' => $idx['index_id']]; $data = ['visitor_id' => $object->getVisitorId(), 'store_id' => $object->getStoreId()]; } else { $where = ['index_id = ?' => $row['index_id']]; $data = ['customer_id' => $object->getCustomerId(), 'store_id' => $object->getStoreId()]; } $connection->update($this->getMainTable(), $data, $where); } return $this; }