Example #1
0
 /**
  * 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;
 }