/** * Update (Merge) customer data from visitor * After Login process * * @param \Magento\Catalog\Model\Product\Compare\Item $object * @return $this */ public function updateCustomerFromVisitor($object) { if (!$object->getCustomerId()) { return $this; } // collect visitor compared items $select = $this->_getWriteAdapter()->select()->from($this->getMainTable())->where('visitor_id=?', $object->getVisitorId()); $visitor = $this->_getWriteAdapter()->fetchAll($select); // collect customer compared items $select = $this->_getWriteAdapter()->select()->from($this->getMainTable())->where('customer_id = ?', $object->getCustomerId())->where('visitor_id != ?', $object->getVisitorId()); $customer = $this->_getWriteAdapter()->fetchAll($select); $products = []; $delete = []; $update = []; foreach ($visitor as $row) { $products[$row['product_id']] = ['store_id' => $row['store_id'], 'customer_id' => $object->getCustomerId(), 'visitor_id' => $object->getVisitorId(), 'product_id' => $row['product_id']]; $update[$row[$this->getIdFieldName()]] = $row['product_id']; } foreach ($customer as $row) { if (isset($products[$row['product_id']])) { $delete[] = $row[$this->getIdFieldName()]; } else { $products[$row['product_id']] = ['store_id' => $row['store_id'], 'customer_id' => $object->getCustomerId(), 'visitor_id' => $object->getVisitorId(), 'product_id' => $row['product_id']]; } } if ($delete) { $this->_getWriteAdapter()->delete($this->getMainTable(), $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . ' IN(?)', $delete)); } if ($update) { foreach ($update as $itemId => $productId) { $bind = $products[$productId]; $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $itemId)); } } return $this; }
/** * Add visitor and customer data to compare item * * @param \Magento\Catalog\Model\Product\Compare\Item $item * @return $this */ protected function _addVisitorToItem($item) { $item->addVisitorId($this->_customerVisitor->getId()); if ($this->_customerSession->isLoggedIn()) { $item->setCustomerId($this->_customerSession->getCustomerId()); } return $this; }
/** * Catalog Product Compare Items Clean * after plugin for clean method * * @param \Magento\Customer\Model\ResourceModel\Visitor $subject * @param \Magento\Customer\Model\ResourceModel\Visitor $logResourceModel * * @return \Magento\Customer\Model\ResourceModel\Visitor * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterClean(\Magento\Customer\Model\ResourceModel\Visitor $subject, $logResourceModel) { $this->_productCompareItem->clean(); return $logResourceModel; }
/** * Catalog Product Compare Items Clean * after plugin for clean method * * @param \Magento\Log\Model\Resource\Log $subject * @param \Magento\Log\Model\Resource\Log $logResourceModel * * @return \Magento\Log\Model\Resource\Log * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterClean(\Magento\Log\Model\Resource\Log $subject, $logResourceModel) { $this->_productCompareItem->clean(); return $logResourceModel; }
public function testGetIdentities() { $id = 1; $this->model->setId($id); $this->assertEquals([\Magento\Catalog\Model\Product\Compare\Item::CACHE_TAG . '_' . $id], $this->model->getIdentities()); }