/** * Update Customer from visitor (Customer logged in) * * @param Mage_Reports_Model_Product_Index_Abstract $object * @return Mage_Reports_Model_Resource_Product_Index_Abstract */ public function updateCustomerFromVisitor(Mage_Reports_Model_Product_Index_Abstract $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(), array('index_id = ?' => $row['index_id'])); $where = array('index_id = ?' => $idx['index_id']); $data = array('visitor_id' => $object->getVisitorId(), 'store_id' => $object->getStoreId(), 'added_at' => Varien_Date::now()); } else { $where = array('index_id = ?' => $row['index_id']); $data = array('customer_id' => $object->getCustomerId(), 'store_id' => $object->getStoreId(), 'added_at' => Varien_Date::now()); } $adapter->update($this->getMainTable(), $data, $where); } return $this; }
/** * Update Customer from visitor (Customer loggin) * * @param Mage_Reports_Model_Product_Index_Abstract $object * @return Mage_Reports_Model_Mysql4_Product_Index_Abstract */ public function updateCustomerFromVisitor(Mage_Reports_Model_Product_Index_Abstract $object) { /** * Do nothing if customer not logged in */ if (!$object->getCustomerId()) { return $this; } $select = $this->_getWriteAdapter()->select()->from($this->getMainTable())->where('visitor_id=?', $object->getVisitorId()); $rowSet = $select->query()->fetchAll(); foreach ($rowSet as $row) { $select = $this->_getWriteAdapter()->select()->from($this->getMainTable())->where('customer_id=?', $object->getCustomerId())->where('product_id=?', $row['product_id']); $idx = $this->_getWriteAdapter()->fetchRow($select); if ($idx) { $this->_getWriteAdapter()->delete($this->getMainTable(), $this->_getWriteAdapter()->quoteInto('index_id=?', $row['index_id'])); $this->_getWriteAdapter()->update($this->getMainTable(), array('visitor_id' => $object->getVisitorId(), 'store_id' => $object->getStoreId(), 'added_at' => now()), $this->_getWriteAdapter()->quoteInto('index_id=?', $idx['index_id'])); } else { $this->_getWriteAdapter()->update($this->getMainTable(), array('customer_id' => $object->getCustomerId(), 'store_id' => $object->getStoreId(), 'added_at' => now()), $this->_getWriteAdapter()->quoteInto('index_id=?', $row['index_id'])); } } return $this; }